#include<iostream> usingnamespacestd; intmain(){ int num; cin >> num; if (num <= 1) cout << "Today, I ate " << num << " apple." << endl; elsecout << "Today, I ate " << num << " apples." << endl; return0; }
【深基3.例5】洛谷团队系统
1 2 3 4 5 6 7 8 9
#include<iostream> usingnamespacestd; intmain(){ int a; cin >> a; if (a <= 5) cout << "Local" << endl; elsecout << "Luogu" << endl; return0; }
#include<iostream> usingnamespacestd; intmain(){ int y,m; cin >> y >> m; if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) cout << "31" << endl; elseif (m == 4 || m == 6 || m == 9 || m == 11) cout << "30" << endl; else { if (y % 100 != 0){ if (y % 4 == 0) cout << "29" << endl; elsecout << "28" << endl; } else { if (y % 400 == 0) cout << "29" << endl; elsecout << "28" << endl; } } return0; }
不高兴的津津
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int a[7][2]; int res[7]; int m = 0; for(int i = 0; i < 7; i++){ cin >> a[i][0] >> a[i][1]; res[i] = a[i][0] + a[i][1]; } for(int i = 0; i < 7; i++){ if (res[i] > res[m]) m = i; } cout << m + 1 << endl; }
stri = raw_input() res = 0 m = 1 for i in range(0, 12): if stri[i] != "-": res = res + int(stri[i]) * m m = m + 1 res = res % 11
if res == 10and stri[12] == "X": print("Right") elif str(res) == stri[12]: print("Right") else: newstr = list(stri) if res == 10: newstr[12] = "X" else: newstr[12] = str(res) print(''.join(newstr))
小玉家的电费
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<stdio.h> intmain(){ int num; float res; scanf("%d", &num); if (num <= 150) res = num * 0.4463; elseif (num > 150 && num <= 400) res += 150 * 0.4463 + (num - 150) * 0.4663; else res += 150 * 0.4463 + 250 * 0.4663 + (num - 400) * 0.5663; printf("%.1f", res); return0; }
小鱼的航程(改进版)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream> usingnamespacestd;
intmain(int argc, char** argv) { int x, n; cin >> x >> n; int d = n / 7 * 5; int r = n % 7; if (r > 0) { if (r + x == 7 || x == 7) r -= 1; elseif (r + x >= 8) r -= 2; } cout << (d + r) * 250 << endl; return0; }
#include<iostream> usingnamespacestd; intgcd(int a, int b){ int r; while(1){ r = a % b; if (r == 0) break; a = b; b = r; } return b; } intmain(){ int a[3]; cin >> a[0] >> a[1] >> a[2]; int max = a[0],min = a[0]; for(int i = 0; i < 3; i++){ if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } int g = gcd(min, max); min /= g; max /= g; cout << min << "/" << max << endl; return0; }
注:求最大公约数的主要操作方法是:
1 2 3 4 5 6 7 8 9 10
intgcd(int a, int b){ int r; while(1){ r = a % b; if (r == 0) break; a = b; b = r; } return b; }
陶陶摘苹果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream> usingnamespacestd; intmain(){ int a[10]; int h; int res = 0; for(int i = 0; i < 10; i++){ cin >> a[i]; } cin >> h; for(int i = 0; i < 10; i++){ if (h + 30 >= a[i]) res++; } cout << res << endl; return0; }
#include<bits/stdc++.h> usingnamespacestd; boolcheckPrime(longlongint n){ for(int i = 2; i <= sqrt(n); i++){ if (n % i == 0) returnfalse; } returntrue; }
boolcheckPalin(int n){ string s = to_string(n); int m = 0; int q = s.size() - 1; while(m <= q){ if (s[m] == s[q]) { m++; q--; } elsereturnfalse; } returntrue; }
intmain(){ int a, b; cin >> a >> b; if (b <= 10000000){ // 回文质数不存在偶数的情况,除了11. for(int i = a; i <= b; i++){ if (i % 2 == 0) continue; if (checkPalin(i)){ if (checkPrime(i))cout << i << endl; } } } else { for(int i = a; i <= 10000000; i++){ if (checkPalin(i)){ if (checkPrime(i))cout << i << endl; } } } return0; }
基础做法,加上一些小trick,险ac。
小玉在游泳
1 2 3 4 5 6 7 8 9 10
#include<bits/stdc++.h> usingnamespacestd; intmain(){ float n; int i = 0; cin >> n; while(100*(1-pow(0.98,i)) < n) i++; cout << i << endl; return0; }
数字反转
1 2 3 4 5 6 7
n1 = input() n = str(n1) n = n[::-1] if n[len(n)-1] == '-': n = n[:len(n)-1] print("-",end="") print(int(n))
月落乌啼算钱
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<bits/stdc++.h> usingnamespacestd; intmain(){ longlongint n,a=1,b=1,c=0; cin >> n; for(int i = 3; i <= n; i++){ c = a + b; a = b; b = c; } cout << c << ".00" << endl; return0; }
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int n; cin >> n; int *a = newint[n]; int min, max; for(int i = 0; i < n; i++){ cin >> a[i]; if (i == 0) { min = a[0]; max = a[0]; } if (max < a[i]) max = a[i]; if (min > a[i]) min = a[i]; } cout << max - min << endl; return0; }
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int n, res = 0, tmp; cin >> n; int *a = newint[n]; for(int i = 0; i < n; i++){ cin >> a[i]; } for(int i = 0; i < n - 1; i++){ if (a[i] + 1 == a[i+1]) res++; else { if (tmp < res + 1) tmp = res + 1; res = 0; } } cout << tmp; return0;
}
质因数分解
1 2 3 4 5 6 7 8 9 10 11 12
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int n; cin >> n; for(int i = 2; i <= sqrt(n); i++){ if (n % i == 0) { cout << n / i << endl; return0; } } }
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int n; cin >> n; float *a = newfloat[n]; float min, max, sum = 0; for(int i = 0; i < n; i++){ cin >> a[i]; if (i == 0){ min = a[0]; max = a[0]; } if (min > a[i]) min = a[i]; if (max < a[i]) max = a[i]; sum += a[i]; } float res = (sum-min-max)/(n-2); printf("%.2f", res); return0; }
[COCI2017-2018#6] Davor
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int n; cin >> n; n /= 364; if (n <= 103) cout << n - 3 << endl << 1 << endl; else { int k = (n - 101) / 3 + 1; int x = n - 3 * k; cout << x << endl << k << endl; } return0; }
津津的储蓄计划
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<bits/stdc++.h> usingnamespacestd; intmain(){ int a[12], num = 0, tmp = 0, res = 0; for(int i = 0; i < 12; i++){ cin >> a[i]; num += 300; if (num - a[i] < 0) { cout << "-" << i + 1 << endl; return0; } tmp = (num - a[i]) / 100; num -= tmp * 100; num -= a[i]; res += tmp * 100; } cout << num + res * 1.2 << endl; return0; }