Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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_For Loop_Modulo - Fatal编程技术网

C 关于循环和模算子

C 关于循环和模算子,c,for-loop,modulo,C,For Loop,Modulo,编写一个计算整数位数和的程序。例如,数字2155的数字之和为2+1+5+5或13。程序应该接受用户输入的任意整数 我可以使用while循环让它工作,但是如果我为循环使用子循环,程序将计算除最后一个之外的所有数字。为什么会这样 #include <stdio.h> int main(void) { int i, num, sum = 0, temp; printf("Enter the number:\n"); scanf("%i", &num);

编写一个计算整数位数和的程序。例如,数字2155的数字之和为2+1+5+5或13。程序应该接受用户输入的任意整数

我可以使用while循环让它工作,但是如果我为循环使用
子循环,程序将计算除最后一个之外的所有数字。为什么会这样

#include <stdio.h>

int main(void)
{
    int i, num, sum = 0, temp;

    printf("Enter the number:\n");
    scanf("%i", &num);

    printf("Test for num: %i\n", num); //both num & temp return same number
    temp = num;
    printf("Test for temp: %i\n", temp);

    //while (num > 0)
    for (i = 0; i <= temp; i++)  //replacing temp w/ num will skip last num
    {
        sum += num % 10;
        //sum += right;
        num /= 10;
    }

    printf("Given number = %i\n", temp);
    printf("Sum of digits of %i = %i", temp, sum);

    return (0);
}
#包括
内部主(空)
{
int i,num,sum=0,temp;
printf(“输入编号:\n”);
scanf(“%i”和&num);
printf(“num的测试:%i\n”,num);//num和temp都返回相同的数字
温度=数值;
printf(“临时测试:%i\n”,临时);
//while(num>0)

对于(i=0;i且num在for循环中,正如您已经注释掉的,您是根据原始数字的红利来计算i,而不是在
num>0

例如,如果您有num=158,则循环将执行,然后将num设置为15。i将递增为1。因此i 如果您的最高位数大于或等于位数,则在for循环中使用num的代码可以工作。否则,它将无法工作

您可以去掉i,只需在for循环中使用num

for(;num > 0; num /= 10)
    sum += num%10;
注:

对于相对较小的数字,也就是说在
int
范围内*)工作正常,例如,我测试了它

但是,例如

您可能会看到
scanf()
函数将“大”数字
1000000001
读取为
1410065409

但是
循环的逻辑没有问题,而
循环的编号
1410065409
的结果是正确的


(*)-
int
最常见的
int
实现(作为32位数字)的范围是


为此,在for循环中打印出变量
i
,并查看它运行的频率。这既低效又明显浪费资源

您还应该考虑以下几点?

时间复杂度是多少?while循环与使用temp的for循环有什么不同


当您更改为for循环时,您没有考虑while循环中变量num发生了什么。考虑,n个数在<代码> 10 ^(n-1)< /代码>包容和<代码> 10 ^ n < /代码>独占之间。如果我们让<代码> n <代码>为n中的位数,则不等式为代码>10 ^(n-1)。用几个数字手工检查你的算法的while版本。然后手工检查你的算法的for版本。你应该会清楚发生了什么以及两个isHey imabug之间的区别,感谢你花时间回答我的问题。我仍然不清楚为什么for循环会将所有数字加在exc上ept最后一个数字(即2465=17,但返回15)。我的理解是,将数字改为10将留下一个余数,该余数被分配给总和。为什么最后一个数字(2)不受此限制?
for (i = 0; i <= temp; i++)
while (num > 0)
{
    sum += num % 10;
    num /= 10;
}
Enter the number:
1234
Test for num: 1234
Test for temp: 1234
Given number = 1234
Sum of digits of 1234 = 10
Enter the number:
123456789
Test for num: 123456789
Test for temp: 123456789
Given number = 123456789
Sum of digits of 123456789 = 45
Enter the number:
10000000001
Test for num: 1410065409
Test for temp: 1410065409
Given number = 1410065409
Sum of digits of 1410065409 = 30
              from  -2.147.483.648  to  +2.147.483.647.
 for (i = temp ; i !=0; i /= 10)