使用C ++中的计数器查找特定数字的出现次数

时间:2017-02-19 10:05:20

标签: c++ numbers counter digit

我有一个很大的数字,我想找到指定数字的出现次数。我不知道使用计数器会起作用。我的代码如下:

#include <iostream>
using namespace std;

int main( )
{
int number;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int digit;
int digitCounter = 0;


    cout << "Please enter a number between 100 to 2000000000." << endl; 
    cin >> number;

    cout << "Please enter a digit that you want to find the number of occurances." << endl;
    cin >> digit;

    if (number > 0)
    { 
     n1 = number % 10;
     n2 = (number/10) % 10;
     n3 = (number/100) % 10;
     n4 = (number/1000) % 10;
     n5 = (number/10000) % 10;
     n6 = (number/100000) % 10;
     n7 = (number/1000000) % 10;
     n8 = (number/10000000) % 10;
     n9 = (number/100000000) % 10;
     n10 = (number/100000000) % 10;

     if (n1 == digit)
     { digitCounter++;}

     else if (n2 == digit)
     { digitCounter++;}

     else if (n3 == digit)
     { digitCounter++;}

     else if (n4 == digit)
     { digitCounter++;}

     else if (n5 == digit)
     { digitCounter++;}

     else if (n6 == digit)
     { digitCounter++;}

     else if (n7 == digit)
     { digitCounter++;}

     else if (n8 == digit)
     { digitCounter++;}

     else if (n9 == digit)
     { digitCounter++;}

     else if (n10 == digit)
     { digitCounter++;}

     cout<< "The total number of occurances of " << digit << " in " << number <<" is "<<digitCounter<< endl;

     }
        else

        cout<< "You have entered an invalid number."<<endl;



system("pause");
return 0;

}

然而,计数器无效。有人可以提出什么湿错吗? 非常感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:0)

你的其他如果需要,如果有的话。现在,您只需通过一个决策声明。一旦找到匹配,你就会退出。

#include <iostream>
using namespace std;

int main()
{
    int number;
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int n6;
    int n7;
    int n8;
    int n9;
    int n10;
    int digit;
    int digitCounter = 0;


    cout << "Please enter a number between 100 to 2000000000." << endl;
    cin >> number;

    cout << "Please enter a digit that you want to find the number of occurances." << endl;
    cin >> digit;

    if (number > 0)
    {
        n1 = number % 10;
        n2 = (number / 10) % 10;
        n3 = (number / 100) % 10;
        n4 = (number / 1000) % 10;
        n5 = (number / 10000) % 10;
        n6 = (number / 100000) % 10;
        n7 = (number / 1000000) % 10;
        n8 = (number / 10000000) % 10;
        n9 = (number / 100000000) % 10;
        n10 = (number / 100000000) % 10;

        if (n1 == digit)
        {
            digitCounter++;
        }

        if (n2 == digit)
        {
            digitCounter++;
        }

        if (n3 == digit)
        {
            digitCounter++;
        }

        if (n4 == digit)
        {
            digitCounter++;
        }

        if (n5 == digit)
        {
            digitCounter++;
        }

        if (n6 == digit)
        {
            digitCounter++;
        }

        if (n7 == digit)
        {
            digitCounter++;
        }

        if (n8 == digit)
        {
            digitCounter++;
        }

        if (n9 == digit)
        {
            digitCounter++;
        }

        if (n10 == digit)
        {
            digitCounter++;
        }

        cout << "The total number of occurances of " << digit << " in " << number << " is " << digitCounter << endl;

    }
    else

        cout << "You have entered an invalid number." << endl;



    system("pause");
    return 0;

}

答案 1 :(得分:0)

您可以在搜索字符串中的数字之后将数字转换为字符串,这样更简单。 或者你可以读取一个字符串(数字)和一个字符(数字)然后你喜欢这样:

char number[20], digit;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &number);

printf("\nEnter the character to be searched : ");
scanf("%c", &digit);

for (i = 0; number[i] != '\0'; i++) {
  if (number[i] == digit)
     count++;
}

if (count == 0)
  printf("\nCharacter '%c'is not present", digit);
else
  printf("\nOccurence of character '%c' : %d", digit, count);`

答案 2 :(得分:0)

试试这个

#include <iostream>

int main(int argc, char **argv) {

  unsigned long long large(0);

  int digitToFind(0);

  std::cout << "enter a large number [100 to 2000000000]" << std::endl;
  std::cin >> large;

  if (large < 100 || large > 2000000000) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::cout << "enter the digit to find" << std::endl;
  std::cin >> digitToFind;

  if (digitToFind < 0 || digitToFind > 9) {
    std::cout << "invalid input." << std::endl;
    return -1;
  }

  std::size_t counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  while (large > 0) {
    int rem = large % 10;

    counts[rem]++;

    large /= 10;
  }

  std::cout << "number of occurrances of " << digitToFind << " is "
            << counts[digitToFind] << std::endl;

  std::cout << "press enter to continue" << std::endl;
  std::cin.get();
  return 0;
}