[C/Python]计算并输出1~100之间的奇数之和与偶数之和
Python:
#计算并输出1~100之间的奇数之和与偶数之和 from functools import reduce print("evenSum=",end='') print(reduce(lambda a, b: a + b, filter(lambda i: i%2 == 0, [i for i in range(1,101)]))) print('oddSum=', end='') print(reduce(lambda a, b: a + b, filter(lambda i: i%2 == 1, [i for i in range(1,101)])))
输出结果:
evenSum=2550
oddSum=2500
C:
//计算并输出1~100之间的奇数之和与偶数之和 #include <stdio.h> int main() { int i, evenSum, oddSum; evenSum = oddSum = 0; for (i = 1; i <= 100; i++) { if (i%2 == 0) evenSum += i; else oddSum += i; } printf("evenSum=%d, oddSum=%d\n", evenSum, oddSum); return 0; }