C++程序设计:编写程序,求1-3+5-7+...-99+101的值.

2024-12-04 13:45:09
推荐回答(2个)
回答(1):

#include
using namespace std;
int main()
{
int Sum=0;
for (int i=1,j=0;i<=101;i+=2,++j)
(j%2==0) ? (Sum+=i) : (Sum-=i) ;
cout<<"Sum="< return 0;
}

问题都很基础,LZ要多加练习

回答(2):

int m = 1;
int n = 3;
int sum1 = 0,sum2 = 0;
int answer;
for(m = 1,n = 3;m<=99&n<=101;m += 2,n += 2)
{
sum1 = sum1+m;
sum2 = sum2+n;
}
answer = sum1-sum2;