Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在向量中找到具有重复数字的数字?_C++_Vector_Digits - Fatal编程技术网

C++ 如何在向量中找到具有重复数字的数字?

C++ 如何在向量中找到具有重复数字的数字?,c++,vector,digits,C++,Vector,Digits,给定a和b之间的数字范围,我需要找到所有具有重复数字的数字。它们不必是连续的重复数字——例如,121可以算作这些数字之一。 我已经对向量列表本身的输入进行了编码——我只是不知道如何分析向量中每个单独项目的数字 #include <iostream> #include <vector> using namespace std; int main(){ //variables int a, b, i = 0; // a & b inputs, i f

给定a和b之间的数字范围,我需要找到所有具有重复数字的数字。它们不必是连续的重复数字——例如,121可以算作这些数字之一。 我已经对向量列表本身的输入进行了编码——我只是不知道如何分析向量中每个单独项目的数字

#include <iostream>
#include <vector>
using namespace std;

int main(){
    //variables
    int a, b, i = 0; // a & b inputs, i for iterating

    //entering number range
    cout << "Enter the first number" << endl;
    cin >> a;
    cout << "Enter the second number" << endl;
    cin >> b;


    //making a vector to contain numbers between a and b
    vector<int> listofnums((b-a)+1); 
    int initialvalue = a;
    while (i <= (b-a)) {  
        listofnums[i] = initialvalue;                                 
        initialvalue++; 
        i++;
    }

    //printing the completed vector
    for ( const auto &item : listofnums ){
        std::cout << item << ' ';
    }

    std::cout << '\n';

    //analyzing the digits of each item in the vector
    //code for finding repeating digits here

    return 0;
}

#包括
#包括
使用名称空间std;
int main(){
//变数
int a,b,i=0;//a&b输入,i用于迭代
//输入数字范围
库塔;
cout b;
//使向量包含a和b之间的数字
向量表((b-a)+1);
int initialvalue=a;

而(i您可以通过将小数除以10后的余数得到小数中最低(最低有效)位数的值;在
C++
中,这可以使用模运算符(
%
)轻松实现因此,给定一个值为
123
的整数变量
a
,语句
int b=a%10;
3
赋值给
b

现在,通过运行一个循环,我们将“测试”数字按顺序除以10(直到它达到零),我们可以得到每个数字的值。如果我们保留一个
bool
值数组,当我们找到一个给定的数字时,将每个值设置为
true
,我们可以快速检测到重复的数字

因此,此功能将完成以下工作:

bool HasRepeatDigit(int x)
{
布尔hasDigit[10]={false,false,false,false,false,false,false,false,false,false};
而(x>0){
整数位数=x%10;
如果(hasDigit[digit])返回true;//我们已经有了这个数字!
hasDigit[digit]=true;//标记我们刚刚找到的数字
x/=10;//除以10,移到下一位
}
return false;//如果我们到了这里,就没有重复的数字了!
}
然后,您可以在第二个循环中对每个“测试”数字调用此函数,将具有重复数字的数字添加到新向量:

带重复的向量;
对于(auto i:listofnums){//测试listofnums中每个元素的“i”
if(HasRepeatDigit(i))with repeats.push_back(i);//将其附加到新向量
}
请随时要求进一步解释和/或澄清

另外,您可以使用函数(在
标题中定义)减少填充
列表的nums
向量的代码:

#包括
//...
标准::物联网(开始(列表编号)、结束(列表编号)、a);

您可以通过将小数除以10后的余数取其最小(最低有效)位数的值;在
C++
中,这可以使用模运算符(
%
)轻松实现因此,给定一个值为
123
的整数变量
a
,语句
int b=a%10;
3
赋值给
b

现在,通过运行一个循环,我们将“测试”数字按顺序除以10(直到它达到零),我们可以得到每个数字的值。如果我们保留一个
bool
值数组,当我们找到一个给定的数字时,将每个值设置为
true
,我们可以快速检测到重复的数字

因此,此功能将完成以下工作:

bool HasRepeatDigit(int x)
{
布尔hasDigit[10]={false,false,false,false,false,false,false,false,false,false};
而(x>0){
整数位数=x%10;
如果(hasDigit[digit])返回true;//我们已经有了这个数字!
hasDigit[digit]=true;//标记我们刚刚找到的数字
x/=10;//除以10,移到下一位
}
return false;//如果我们到了这里,就没有重复的数字了!
}
然后,您可以在第二个循环中对每个“测试”数字调用此函数,将具有重复数字的数字添加到新向量:

带重复的向量;
对于(auto i:listofnums){//测试listofnums中每个元素的“i”
if(HasRepeatDigit(i))with repeats.push_back(i);//将其附加到新向量
}
请随时要求进一步解释和/或澄清

另外,您可以使用函数(在
标题中定义)减少填充
列表的nums
向量的代码:

#包括
//...
标准::物联网(开始(列表编号)、结束(列表编号)、a);
给你

#include <iostream>
#include <bitset>
#include <tuple>
#include <vector>
#include <algorithm>

bool duplicated_digits( int value )
{
    const int Base = 10;
    std::bitset<Base> b;

    bool is_duplicate = false;

    do
    {
        int digit = value % Base;

        if ( digit < 0 ) digit = -digit;

        b.flip( digit );
        is_duplicate = not b.test( digit );
    } while ( not is_duplicate && ( value /= Base ) != 0 );

    return is_duplicate;
}

int main()
{
    int a = 0, b = 0; 

    std::cout << "Enter the first number: ";
    std::cin >> a;
    std::cout << "Enter the second number: ";
    std::cin >> b;

    std::tie( a, b ) = std::minmax( { a, b } );

    std::vector<int> v; 

    int current_value = a;
    do
    {
        if ( duplicated_digits( current_value ) ) v.push_back( current_value );
    } while ( current_value++ != b );

    if ( not v.empty() )
    {
        std::cout << "There are the following numbers with duplicated digits "
                     "in the range [ "
                  << a << ", " << b << " ]\n";                   
        for ( const auto &item : v )
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }
    else
    {
        std::cout << "There are no numbers with duplicated digits in the range [ "
                  << a << ", " << b << " ]\n";                   
    }

    return 0;
}
或另一个可能的程序输出示例

Enter the first number: -100
Enter the second number: -130
There are the following numbers with duplicated digits in the range [ -130, -100 ]
-122 -121 -119 -118 -117 -116 -115 -114 -113 -112 -111 -110 -101 -100 
给你

#include <iostream>
#include <bitset>
#include <tuple>
#include <vector>
#include <algorithm>

bool duplicated_digits( int value )
{
    const int Base = 10;
    std::bitset<Base> b;

    bool is_duplicate = false;

    do
    {
        int digit = value % Base;

        if ( digit < 0 ) digit = -digit;

        b.flip( digit );
        is_duplicate = not b.test( digit );
    } while ( not is_duplicate && ( value /= Base ) != 0 );

    return is_duplicate;
}

int main()
{
    int a = 0, b = 0; 

    std::cout << "Enter the first number: ";
    std::cin >> a;
    std::cout << "Enter the second number: ";
    std::cin >> b;

    std::tie( a, b ) = std::minmax( { a, b } );

    std::vector<int> v; 

    int current_value = a;
    do
    {
        if ( duplicated_digits( current_value ) ) v.push_back( current_value );
    } while ( current_value++ != b );

    if ( not v.empty() )
    {
        std::cout << "There are the following numbers with duplicated digits "
                     "in the range [ "
                  << a << ", " << b << " ]\n";                   
        for ( const auto &item : v )
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }
    else
    {
        std::cout << "There are no numbers with duplicated digits in the range [ "
                  << a << ", " << b << " ]\n";                   
    }

    return 0;
}
或另一个可能的程序输出示例

Enter the first number: -100
Enter the second number: -130
There are the following numbers with duplicated digits in the range [ -130, -100 ]
-122 -121 -119 -118 -117 -116 -115 -114 -113 -112 -111 -110 -101 -100 

有一百万种解决方案,每个人都可以做他想做的事

C++中对于这些类型问题的标准方法是使用<代码> STD::MAP< /COD>作为计数器。STD::MAP有索引运算符。如果存在密钥,则索引操作符返回对值的引用。如果该密钥不存在,它将创建新条目,然后返回引用到默认初始化值。 然后我们增加引用

在此之前,我们将整数转换为字符串,然后对字符(数字)进行处理。然后,计数与以下操作一样简单:

for (const char& digit : numberAsString) counter[digit]++;

我们把所有的运算都放在函数中,然后在C++算法中使用。 请参阅:

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>

bool check(const int& i) {
    // Convert int to string
    std::string numberAsString{ std::to_string(i) };

    // We will use a map as counter
    std::map<char, size_t> counter{};

    // Count the digits
    for (const char& digit : numberAsString) counter[digit]++;

    // Search for a digit count > 1
    bool result{};
    for (const auto& [digit, count] : counter)  if (count > 1) { result = true; break; };

    return result;
}


using namespace std;

int main() {
    //variables
    int a, b, i = 0; // a & b inputs, i for iterating

    //entering number range
    cout << "Enter the first number" << endl;
    cin >> a;
    cout << "Enter the second number" << endl;
    cin >> b;


    //making a vector to contain numbers between a and b
    vector<int> listofnums((b - a) + 1);
    int initialvalue = a;
    while (i <= (b - a)) {
        listofnums[i] = initialvalue;
        initialvalue++;
        i++;
    }

    //printing the completed vector
    for (const auto& item : listofnums) {
        std::cout << item << ' ';
    }

    cout << '\n';

    //analyzing the digits of each item in the vector
    //code for finding repeating digits here

    // Example 1
    // print out all values in the vector having this property
    cout << "\n\n Result:\n";
    copy_if(listofnums.begin(), listofnums.end(), ostream_iterator<int>(cout,"\n"), check);

    // Example 2
    // copy all thos values to a new vector
    vector<int> result;
    copy_if(listofnums.begin(), listofnums.end(), back_inserter(result), check);

    return 0;
}

#包括
#包括
#包括
#包括
#包括
#包括
布尔检查(常数输入和输入){
//将整型转换为字符串
字符串编号字符串{std::to_string(i)};
//我们将使用地图作为计数器
std::映射计数器{};
//数一数数字
对于(常量字符和数字:numbersString)计数器[数字]+;
//搜索大于1的数字计数
布尔结果{};
如果(计数>1){result=true;break;},则为(const auto&[digit,count]:计数器){result=true;break;};
返回结果;
}
使用名称空间std;
int main(){
//变数
int a,b,i=0;//a&b输入,i用于迭代
//输入数字范围
库塔;
cout b;
//使向量包含a和b之间的数字
向量表((b-a)+1);
int