提问,为什么这3个程序的输出不一样?
#include <stdio.h>
int main(void)
{
int n, k, T, i = 1, j;
int sum = 1, count = 1, num = 1;
scanf("%d %d %d", &n, &k, &T);
for(j = 1; j <= n; ++j)
{
if(i == T)
{
break;
}
num += count;
count++;
if(num > k)
{
num = num + count - k;
}
if(j == n)
{
sum += num;
i++;
j = 1;
}
}
printf("%d", sum);
return 0;
}
#include <stdio.h>
int main(void)
{
//int n, k, T, i = 1, j;
int n, k, T, i = 0, j;
//int sum = 1, count = 1, num = 1;
int sum = 0, count = 1, num = 1;
scanf("%d %d %d", &n, &k, &T);
//for(j = 1; j <= n; ++j)
for(j = 0; j <= n; ++j)
{
if(i == T)
{
break;
}
//num += count;
//count++;
if(num > k)
{
//num = num + count - k;
num = num - k;
}
//if(j == n)
if(j == 0 || j == n)
{
sum += num;
i++;
//j = 1;
j = 0;
}
num += count;
count++;
}
printf("%d", sum);
return 0;
}
#include <stdio.h>
#define LL long long int
int main(void) {
int i = 1;
LL sum = 1, x, add = 1, n, k, t;
scanf("%lld%lld%lld",&n,&k,&t);
while (i < t) {
x = add + ((i-1)*n + 1 + i*n)*n/2; // 得到这次这个人所报的数字
i++; // 过这个人的次数加1
x = x % k; //当大于k的部分需要返回
sum += x; //数字和
add = x; // 返回
}
printf("%lld",sum);
return 0;
}
|