【发布时间】:2019-11-22 16:54:23
【问题描述】:
我正在做的任务是输入 5 个 5 位数的随机数,压缩它们并得到它们的总和。那么我们如何压缩它们呢?我们去掉了第二个和第四个数字。例如 12345 到 10305。
这是我的代码。
int main()
{
int n=5,i,j,number5,num1,num3,numb5,sum;
for(i=0;i<n;i++) // 5 times we read next for loop right ?
for(j=0;j<i;j++){ // this loop read 5 times 5 digits number
scanf("%d",&number5); // scanf 1 number
while(number5){ // while number isn't 0 ( false )
num1=number5/10000;
num1*=10000;
num3=(number5%10000)%1000/100;
num3*=100;
numb5=(number5%10000)%1000%100%10;
numb5*=1;// mathematic operations to get to 1st third and 5th number
number5=0; // set the number5 to 0 so we can go out of while right ?
}
sum=num1+num3+numb5; // we get the sum of the first 5 digits and we get it on the second when j++ right ?
}
printf("%d",sum);// on the end of all five number with 5 digits we get the sum right ?
}
那么为什么我的for 循环只运行了两次而不是五次?
【问题讨论】:
-
压缩?这是什么意思?
-
@EugeneSh。 OP 表示删除数字。 “压缩”在这里并不合适。
-
为什么
j<i是第二个for循环中的条件? -
声明
numb5=(number5%10000)%1000%100%10;在数学上等同于numb5=number5 % 10;。 -
如果你想输入 5 个数字,那么你只需要一个运行 5 次迭代的循环。不需要 3 个嵌套循环。
标签: c for-loop while-loop breakpoints