★计算1/1-1/2+1/3-1/4+1/5 -- + 1/99 - 1/100的值 #include <stdio.h> int main() { int i; double x = 1; double sum = 0; for (i = 1; i<101; i++) { sum = sum + x / i; x = x*(-1); } printf("1-1/2+1/3-1/4....-1/100=%f\n", sum); return 0; }
// 求 1+2+3+4+...+100 #include <stdio.h> int main() { int i; int sum = 0; for( i = 1; i <= 100; i++ ) { sum = sum + i; } printf("1+2+3+4+...+100的和是:%d\n",sum); return 0; }
题目描述: In a given integer array nums, there is always exactly one largest element. Find whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, otherwise
'''python实现任意数到任意数相加,如:1加到100''' #第一种是for循环# def sum(start, end):# sum = 0# for one in range(start, end+1):# sum = one + sum# one += 1# return sum## result = sum(1, 100)# print(result) #第二种是while循环def sum2(start,end): i = start sum = 0 while i <= end