#include<iostream> usingnamespace std; intmain() { int i = 1; int n; cin >> n; int sum = 0; while (i <= n) { sum += i; i++; } cout << sum << endl; return0; }
1404求阶乘
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream> usingnamespace std; intmain() { int n, s = 1; cin >> n; for(int i = 1; i <= n; i++) { s *= i; } cout << s << endl; return0; }
int n; cin >> n; int i = 1; int odd = 0, even = 0; while (i <= n) { if (i % 2 == 0) { even += i; } else { odd += i; } i++; } cout << even << " " << odd << endl; return0; }
#include<iostream> usingnamespace std; intmain() { int left = 0; // 上个月 / 剩余的钱 int cost; int mom; for (int i = 1; i <= 12; i++) { cin >> cost; // 计算开销 left = left + 300 - cost; if (left < 0) { cout << -i << endl; return0; } // += -= ... a += 1 a = a + 1 mom += left / 100 * 100; left -= left / 100 * 100; } cout << left + mom * 1.2 << endl; return0; }
课堂练习
1416人口增长
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream> usingnamespace std; intmain() { double x, n; cin >> x >> n; for (int i = 1; i <= n; i++) { x *= 1.001; } printf("%.4lf", x); return0; }
1324判断闰年
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream> usingnamespace std; intmain() { // 1.四年一闰,百年不闰 // 2.四百年一闰 int year; cin >> year; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { cout << "Y" << endl; } else { cout << "N" << endl; } return0; }
609求1000到2000内的闰年
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream> usingnamespace std; intmain() { // 1.四年一闰,百年不闰 // 2.四百年一闰 for (int year = 1000; year <= 2000; year += 4) { if (year % 100 != 0 || year % 400 == 0) { cout << year << " "; } }
return0; }
1430幂的末尾
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream> usingnamespace std; intmain() { int a, b; cin >> a >> b; int ans = 1, m = 1000;//一定要定义一个ans,否则会多乘一个a for (int i = 1; i <= b; i++) { ans *= a; ans %= 1000;
// ans = ans * a % m; } printf("%03d", ans); return0; }