题解 | #求 1 - n 之间偶数的和#
求 1 - n 之间偶数的和
https://www.nowcoder.com/practice/03df7fa0950046a9ba564b4438f918a3
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
// write your code here......
for(int i=1;i<=n;i++)//由于一个个数药列出来,我们就进行一个for循环
{
if(i%2==0) //判断偶数
{
sum+=i; //如果是,就加入sum,不是就do nothing继续循环下一个
}
}
cout << sum << endl;
return 0;
}
查看1道真题和解析
阿里云工作强度 702人发布