一个数字出现在一个数字的次数

时间:2023-01-06 19:15:40

Well, I wrote the code and everything is fine except one thing. When I enter that digit number, which has to be upto 10 digits, I recieve in arr[0] various values, for example, if I enter "12345" I get 20, 1 , 1 , 1 , 1 , 1 , 0 ,0 ,0 ,0.

好吧,我写了代码,一切都很好,除了一件事。当我输入该数字时,必须达到10位数,我收到arr [0]各种值,例如,如果我输入“12345”,我得到20,1,1,1,1,1,0, 0,0,0。

Which is fine from arr[1] to arr[9], but pretty odd in arr[0].

从arr [1]到arr [9]哪个好,但在arr [0]中很奇怪。

Any ideas?

#include <stdio.h>
#include <conio.h>
#include <math.h>



void main()
{
    int i,j,p=0, temp,indexNum, arr[10] = { 0 }, num, level = 10, level2 = 1,maxIndex;
    printf("Please enter a digit number (upto 10 digits) \n");
    scanf("%d", &num);
    temp = num;
    while (temp > 0)
    {
        p++;
        temp /= 10;
    }
    for (i = 0;i < p;i++)
    {
        temp = num;
        while (temp > 0)
        {
            indexNum = num % level / level2;
            arr[indexNum]++;
            level *= 10;
            level2 *= 10;
            temp /= 10;
        }
    }
    for (j = 0; j < 10; j++)
    {
        printf("%d\n", arr[j]);
    }
    getch();
}

5 个解决方案

#1


3  

Here is simplified version of your program:

这是您的程序的简化版本:

#include <stdio.h>
#include <math.h>

int  main()
{
    int i = 0, j = 0, temp = 0, indexNum = 0, num = 0, level = 10;
    int arr[10] = {0};

    num = 7766123;

    temp = num;
    if(0 == temp) arr[0] = 1; // Handle 0 input this way
    while (temp > 0)
    {
        indexNum = temp % level;
        arr[indexNum]++;

        temp /= 10;
    }

    for (j = 0; j < 10; j++)
    {
        printf("%d\n", arr[j]);
    }

  return 0;

}

#2


1  

A few hints to help you:

一些提示可以帮助您:

  1. What does arr[10] = { 0 } actually do?

    arr [10] = {0}实际上做了什么?

  2. When you calculate indexNum, you are dividing integers. What happens when the modulus is a one-digit number, and level2 is greater than 1?

    计算indexNum时,您将除以整数。当模数是一位数,而level2大于1时,会发生什么?

#3


1  

It's probably easier to read the input into a string and count digit characters. Something like this (not tested):

将输入读入字符串并计算数字字符可能更容易。像这样的东西(未经测试):

std::map<char, int> count;
std::string input;
std::cin >> input;
for (auto iter = input.begin(); iter != input.end(); ++iter) {
    if (*iter < 0 || *iter > 9)
        break;
    else
        ++count[*iter];
}
for (auto iter = count.begin(); iter != count.end(); ++iter) {
    std::cout << *iter << '\n';
}

#4


0  

You need to get rid of your first for loop. Something more like:

你需要摆脱你的第一个for循环。更像是:

#include <stdio.h>
#include <math.h>

using namespace std;

int main()
{
    int j;
    int  temp;
    int  indexNum;
    int  arr[10] = { 0 };
    int  num;
    int  level = 10;
    int  level2 = 1;
    printf("Please enter a digit number (upto 10 digits) \n");
    scanf("%d", &num);
    temp = num;
    while (temp > 0)
    {
        indexNum = num % level / level2;
        arr[indexNum]++;
        level *= 10;
        level2 *= 10;
        temp /= 10;
    }
    for (j = 0; j < 10; j++)
    {
        printf("%d\n", arr[j]);
    }
    return 0;
}

#5


0  

Check the program below.

检查下面的程序。

void count_digits(unsigned int a, int count[])
{
     unsigned int last_digit = 0;

     if (a == 0) {
         count[0] = 1;
     }
     while (a != 0)
     {
         last_digit = a%10;
         count[last_digit]++;
         a = a/10;
     }

}

int main()
{
    int count[10]= {0};
    unsigned int num = 1122345; /* This is the input, Change it as per your need */
    int i = 0;
    count_digits(num, count);

    for (i = 0; i < 10; i++)
    {
        printf ("%d: -- %d\n", i, count[i]);
    }
    return 0;
}

#1


3  

Here is simplified version of your program:

这是您的程序的简化版本:

#include <stdio.h>
#include <math.h>

int  main()
{
    int i = 0, j = 0, temp = 0, indexNum = 0, num = 0, level = 10;
    int arr[10] = {0};

    num = 7766123;

    temp = num;
    if(0 == temp) arr[0] = 1; // Handle 0 input this way
    while (temp > 0)
    {
        indexNum = temp % level;
        arr[indexNum]++;

        temp /= 10;
    }

    for (j = 0; j < 10; j++)
    {
        printf("%d\n", arr[j]);
    }

  return 0;

}

#2


1  

A few hints to help you:

一些提示可以帮助您:

  1. What does arr[10] = { 0 } actually do?

    arr [10] = {0}实际上做了什么?

  2. When you calculate indexNum, you are dividing integers. What happens when the modulus is a one-digit number, and level2 is greater than 1?

    计算indexNum时,您将除以整数。当模数是一位数,而level2大于1时,会发生什么?

#3


1  

It's probably easier to read the input into a string and count digit characters. Something like this (not tested):

将输入读入字符串并计算数字字符可能更容易。像这样的东西(未经测试):

std::map<char, int> count;
std::string input;
std::cin >> input;
for (auto iter = input.begin(); iter != input.end(); ++iter) {
    if (*iter < 0 || *iter > 9)
        break;
    else
        ++count[*iter];
}
for (auto iter = count.begin(); iter != count.end(); ++iter) {
    std::cout << *iter << '\n';
}

#4


0  

You need to get rid of your first for loop. Something more like:

你需要摆脱你的第一个for循环。更像是:

#include <stdio.h>
#include <math.h>

using namespace std;

int main()
{
    int j;
    int  temp;
    int  indexNum;
    int  arr[10] = { 0 };
    int  num;
    int  level = 10;
    int  level2 = 1;
    printf("Please enter a digit number (upto 10 digits) \n");
    scanf("%d", &num);
    temp = num;
    while (temp > 0)
    {
        indexNum = num % level / level2;
        arr[indexNum]++;
        level *= 10;
        level2 *= 10;
        temp /= 10;
    }
    for (j = 0; j < 10; j++)
    {
        printf("%d\n", arr[j]);
    }
    return 0;
}

#5


0  

Check the program below.

检查下面的程序。

void count_digits(unsigned int a, int count[])
{
     unsigned int last_digit = 0;

     if (a == 0) {
         count[0] = 1;
     }
     while (a != 0)
     {
         last_digit = a%10;
         count[last_digit]++;
         a = a/10;
     }

}

int main()
{
    int count[10]= {0};
    unsigned int num = 1122345; /* This is the input, Change it as per your need */
    int i = 0;
    count_digits(num, count);

    for (i = 0; i < 10; i++)
    {
        printf ("%d: -- %d\n", i, count[i]);
    }
    return 0;
}