繁体   English   中英

C 程序使用 2 计算数字中的每个数字 Arrays

[英]C Program To Count Each Digit In A Number using 2 Arrays

我需要编写一个名为void digits (int arr [], int size, int statistics [])的 function,它接收一个整数数组及其大小,以及另一个数组,称为统计数据,大小为 10。function更改数组统计信息,使其包含数组数组的数字中每个告诉的印象数。 也就是说,statistics [i]包含了故事 i 的出现次数。 function 不会更改“ arr ”数组,也不会打印任何内容。 例如,如果arr [] = {438,439,440,441,442,443,444}那么在 function 运行的末尾statistics [] = {1,1,1,3,13,0,0,0,1,1} 即数字0在“arr”数组中出现一次,数字1在“arr”数组中出现一次,以此类推。

您应该使用 mod 和除法来过滤掉数字。 然后使用该数字作为统计数组中的索引并递增相关元素。

像这样的东西:

void digits(int arr [], int size, int statistics []) {
  for(int i=0 ; i<10 ; i++) {  
    statistics[i] = 0
  }
  for (int i=0 ; i< size ; i++) {
     int remainder = arr[i];
     do{
        int digit = remainder % 10;
        remainder = remainder / 10;
        statistics[i]++;
     }while(remainder) ;
  }
}

为您的代码编写测试用例也是一个好习惯,对于像这样的快速破解,您不需要花哨的设置来运行测试用例。 只需设置几个 function 从main()调用到您 function 并在 for 循环中打印结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM