幸运数字7
幸运数字7
https://ac.nowcoder.com/acm/problem/22162
两种方法
1、利用if做判断
用if条件每次对变量做判断
#include<bits/stdc++.h> using namespace std; int main() { int n,sum = 0; cin >> n; for(int i = 1; i <= n; i++){ if(i % 7 == 0){ sum += i; } } cout << sum; return 0; }
2、变量每次自增7
从最小的数7开始每次都自增7
#include<bits/stdc++.h> using namespace std; int main() { int n,sum = 0; cin >> n; for(int i = 7; i <= n; i += 7){ sum += i; } cout << sum; return 0; }