计算 1+2+3+……+100 结果的程序
int i, sum = 0, n = 100;
for (i = 1; i <= n; i++) {
sum = sum + i;
}
printf("%d", sum);
高斯
int sum = 0, n = 100;
sum = (1 + n) * n/2;
printf("%d", sum);
计算 1+2+3+……+100 结果的程序
int i, sum = 0, n = 100;
for (i = 1; i <= n; i++) {
sum = sum + i;
}
printf("%d", sum);
高斯
int sum = 0, n = 100;
sum = (1 + n) * n/2;
printf("%d", sum);
1 * 8 + 1 = 9
12 * 8 + 2 = 98
123 * 8 + 3 = 987
1234 * 8 + 4 = 9876
12345 * 8 + 5 = 98765
123456 * 8 + 6 = 987654
1234567 * 8 + 7 = 9876543
12345678 * 8 + 8 = 98765432
123456789 * 8 + 9 = 987654321