C 一个数字在一个数字中出现多少次

C 一个数字在一个数字中出现多少次,c,arrays,C,Arrays,嗯,我写了代码,除了一件事之外,一切都很好。 当我输入该数字时,它必须最多为10位,我在arr[0]中接收各种值,例如,如果我输入“12345”,我得到20,1,1,1,1,0,0,0 从arr[1]到arr[9]这很好,但在arr[0]中非常奇怪 有什么想法吗 #include <stdio.h> #include <conio.h> #include <math.h> void main() { int i,j,p=0, temp,index

嗯,我写了代码,除了一件事之外,一切都很好。 当我输入该数字时,它必须最多为10位,我在arr[0]中接收各种值,例如,如果我输入“12345”,我得到20,1,1,1,1,0,0,0

从arr[1]到arr[9]这很好,但在arr[0]中非常奇怪

有什么想法吗

#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();
}
#包括
#包括
#包括
void main()
{
inti,j,p=0,temp,indexNum,arr[10]={0},num,level=10,level2=1,maxIndex;
printf(“请输入一个数字(最多10位)\n”);
scanf(“%d”和&num);
温度=数值;
而(温度>0)
{
p++;
温度/=10;
}
对于(i=0;i0)
{
indexNum=num%level/level2;
arr[indexNum]++;
级别*=10;
二级*=10;
温度/=10;
}
}
对于(j=0;j<10;j++)
{
printf(“%d\n”,arr[j]);
}
getch();
}

以下是您程序的简化版本:

#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;

}
#包括
#包括
int main()
{
int i=0,j=0,temp=0,indexNum=0,num=0,level=10;
int arr[10]={0};
num=7766123;
温度=数值;
如果(0==temp)arr[0]=1;//以这种方式处理0输入
而(温度>0)
{
indexNum=温度%水平;
arr[indexNum]++;
温度/=10;
}
对于(j=0;j<10;j++)
{
printf(“%d\n”,arr[j]);
}
返回0;
}

以下是您程序的简化版本:

#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;

}
#包括
#包括
int main()
{
int i=0,j=0,temp=0,indexNum=0,num=0,level=10;
int arr[10]={0};
num=7766123;
温度=数值;
如果(0==temp)arr[0]=1;//以这种方式处理0输入
而(温度>0)
{
indexNum=温度%水平;
arr[indexNum]++;
温度/=10;
}
对于(j=0;j<10;j++)
{
printf(“%d\n”,arr[j]);
}
返回0;
}

一些提示可以帮助您:

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

  • 当您计算indexNum时,您正在除以整数。当模数是一位数字,级别2大于1时会发生什么情况


  • 以下几点提示可帮助您:

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

  • 当您计算indexNum时,您正在除以整数。当模数是一位数字,级别2大于1时会发生什么情况


  • 将输入读入字符串并计算数字字符可能更容易。类似于此(未经测试):

    std::映射计数;
    std::字符串输入;
    std::cin>>输入;
    对于(自动iter=input.begin();iter!=input.end();++iter){
    if(*iter<0 | |*iter>9)
    打破
    其他的
    ++计数[*iter];
    }
    对于(自动iter=count.begin();iter!=count.end();++iter){
    
    std::cout将输入读入字符串并计算数字字符可能更容易。类似这样的内容(未测试):

    std::映射计数;
    std::字符串输入;
    std::cin>>输入;
    对于(自动iter=input.begin();iter!=input.end();++iter){
    if(*iter<0 | |*iter>9)
    打破
    其他的
    ++计数[*iter];
    }
    对于(自动iter=count.begin();iter!=count.end();++iter){
    
    std::cout您需要摆脱第一个
    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;
    }
    
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    int j;
    内部温度;
    int indexNum;
    int arr[10]={0};
    int-num;
    智力水平=10;
    int level2=1;
    printf(“请输入一个数字(最多10位)\n”);
    scanf(“%d”和&num);
    温度=数值;
    而(温度>0)
    {
    indexNum=num%level/level2;
    arr[indexNum]++;
    级别*=10;
    二级*=10;
    温度/=10;
    }
    对于(j=0;j<10;j++)
    {
    printf(“%d\n”,arr[j]);
    }
    返回0;
    }
    
    您需要摆脱第一个
    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;
    }
    
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    int j;
    内部温度;
    int indexNum;
    int arr[10]={0};
    int-num;
    智力水平=10;
    int level2=1;
    printf(“请输入一个数字(最多10位)\n”);
    scanf(“%d”和&num);
    温度=数值;
    而(温度>0)
    {
    indexNum=num%level/level2;
    arr[indexNum]++;
    级别*=10;
    二级*=10;
    温度/=10;
    }
    对于(j=0;j<10;j++)
    {
    printf(“%d\n”,arr[j]);
    }
    返回0;
    }
    
    检查下面的程序

    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;
    }
    
    void count_位(无符号整数a,整数计数[])
    {
    无符号整数最后一位=0;
    如果(a==0){
    计数[0]=1;
    }
    while(a!=0)
    {
    最后一位=a%10;
    计数[最后一位]+;
    a=a/10;
    }
    }
    int main()
    {
    整数计数[10]={0};
    unsigned int num=1122345;/*这是输入,请根据需要进行更改*/
    int i=0;
    数字计数(num,count);
    对于(i=0;i<10;i++)
    {
    printf(“%d:-%d\n”,i,计数[i]);
    }
    返回0;
    }
    
    检查下面的程序

    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;
    }
    
    void count_位(无符号整数a,整数计数[])
    {
    无符号整数最后一位=0;
    如果(a==0){
    计数[0]=1;
    }
    while(a!=0)
    {
    最后一位=a%10;
    计数[最后一位]+;
    a=a/10;
    }
    }
    int main()
    {
    整数计数[10]={0};
    unsigned int num=1122345;/*这是输入,请根据需要进行更改*/
    int i=0;
    数字计数(num,count);
    对于(i=0;i<10;i++)
    {
    printf(“%d:-%d\n”,i,计数[i]);
    }
    返回0;
    }
    
    为什么要使用嵌套循环(您最终会多次遍历该数字)?为什么两者都会增加级别并移动数字(它们中的任何一个都可以工作,但两者都不能工作)?您是否尝试过使用调试器或添加日志来找出哪里出了问题?为什么嵌套循环(您最终会多次遍历该数字)不起作用