#include<bits/stdc++.h> usingnamespace std; #define mem(a) memset(a, 0, sizeof(a)) #define dbg(x) cout << #x << " = " << x << endl #define fi(i, l, r) for (int i = l; i < r; i++) #define cd(a) scanf("%d", &a) typedeflonglong ll;
intmain(){ fi (x, 1, 31) { fi (y, 1, 31) { fi (z, 1, 31) { if (x + y + z == 30 && 3 * x + 2 * y + z == 50) { cout << x << ' ' << y << ' ' << z << endl; } } } } return0; }
Python
1 2 3 4 5
for x inrange(1, 31): for y inrange(1, 31): for z inrange(1, 31): if x + y + z == 30and3 * x + 2 * y + z == 50: print(x, y, z)
#include<bits/stdc++.h> usingnamespace std; #define mem(a) memset(a, 0, sizeof(a)) #define dbg(x) cout << #x << " = " << x << endl #define fi(i, l, r) for (int i = l; i < r; i++) #define cd(a) scanf("%d", &a) typedeflonglong ll;
int a[10010];
intmain(){ int T; cin >> T; while (T--) { int m, n; cin >> m >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); int ans = 0; while (m > 0 && ans < n) { if (m >= a[ans]) { m -= a[ans++]; } else { break; } } cout << ans << endl; } return0; }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13
T = int(input()) for _ inrange(T): m, n = map(int, input().split()) a = list(map(int, input().split())) a.sort() ans = 0 while m > 0and ans < n: if m >= a[ans]: m -= a[ans] ans += 1 else: break print(ans)
#include<bits/stdc++.h> usingnamespace std; #define mem(a) memset(a, 0, sizeof(a)) #define dbg(x) cout << #x << " = " << x << endl #define fi(i, l, r) for (int i = l; i < r; i++) #define cd(a) scanf("%d", &a) typedeflonglong ll;
structTreasure { int w, v; double score; } a[10010];
boolcmp(Treasure& a, Treasure& b){ return a.score > b.score; }
intmain(){ int T; cin >> T; while (T--) { int m, n; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].w, &a[i].v); a[i].score = 1. * a[i].v / a[i].w; } sort(a, a + n, cmp); double v = 0; double w = m; int th = 0; while (w > 0 && th < n) { if (!a[th].w) { v += a[th++].v; continue; } int thisW = min((double)a[th].w, w); v += thisW * a[th].score; w -= thisW; th++; } cout << v << endl; } return0; }
boolcmp(pii& a, pii& b){ return a.second < b.second; }
intmain(){ int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].first, &a[i].second); } sort(a, a + n, cmp); int lastEnd = 0; int ans = 0; for (int i = 0; i < n; i++) { if (a[i].first >= lastEnd) { ans++; lastEnd = a[i].second; } } cout << ans << endl; } return0; }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
T = int(input()) for _ inrange(T): thisLine = '' whilenot thisLine: thisLine = input() n = int(thisLine) a = [] for __ inrange(n): thisInput = input().split() a.append((int(thisInput[0]), int(thisInput[1]))) # print(a) a.sort(key=lambda x: x[1]) lastEnd = 0 ans = 0 for p in a: if p[0] >= lastEnd: ans += 1 lastEnd = p[1] print(ans)
有一天,孩子回来对我说:“妈妈,听说马尔代夫很不错,放假了我想去玩。” 马尔代夫?我也想去!没有人不向往一场说走就走的旅行! “其实我想去的地方很多,呼伦贝尔大草原、玉龙雪山、布达拉宫、艾菲尔铁塔……”小孩子还说着他感兴趣的地方。 于是我们拿出地图,标出想去的地点,然后计算最短路线,估算大约所需的时间,有了这张秘制地图,一场说走就走的旅行不是梦! “哇,感觉我们像凡尔纳的《环游地球八十天》,好激动!可是老妈你也太 out 了,学计算机的最短路线你用手算?” 暴汗……,“小子你别牛,你知道怎么算?” “呃,好像是叫什么迪科斯彻的人会算。” 哈哈,关键时刻还要老妈上场了!
输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14
样例组数: t ( 0 < t <= 10 ) 城市的个数: n ( 0 < n < 100 ) 城市之间的路线的个数: m ( 0 < m < 10000 ) 请输入城市之间的路线以及距离: ( 0 < ui, vi, di <= 100 ) u1 v1 d1 u2 v2 d2 ... ui vi di 请输入小明所在的位置: l ( 0 < l < 100 )
/* 6 19 b 0.28 c 0.57 d 0.05 e 0.02 f 0.04 g 0.02 h 0.01 i 0.01 j 0.00 k 0.00 l 0.00 m 0.00 n 0.00 o 0.00 p 0.00 q 0.00 r 0.00 s 0.00 t 0.00 16 b 0.33 c 0.07 d 0.44 e 0.14 f 0.02 g 0.00 h 0.00 i 0.00 j 0.00 k 0.00 l 0.00 m 0.00 n 0.00 o 0.00 p 0.00 q 0.00 14 b 0.39 c 0.55 d 0.02 e 0.04 f 0.00 g 0.00 h 0.00 i 0.00 j 0.00 k 0.00 l 0.00 m 0.00 n 0.00 o 0.00 14 b 0.10 c 0.69 d 0.10 e 0.11 f 0.00 g 0.00 h 0.00 i 0.00 j 0.00 k 0.00 l 0.00 m 0.00 n 0.00 o 0.00 7 b 0.85 c 0.08 d 0.03 e 0.01 f 0.03 g 0.00 h 0.00 12 b 0.15 c 0.06 d 0.60 e 0.05 f 0.13 g 0.01 h 0.00 i 0.00 j 0.00 k 0.00 l 0.00 m 0.00
T = int(input()) for _CASE inrange(T): n, m = map(int, input().split()) visited = [Falsefor _ inrange(n + 1)] graph = [[] for _ inrange(n + 1)] for _ inrange(m): u, v, w = map(int, input().split()) graph[u].append(Edge(v, w)) graph[v].append(Edge(u, w)) visited[1] = True pq = PriorityQueue() for thisNode in graph[1]: pq.put(thisNode) ans = 0 cntEdge = 0 while cntEdge < n - 1: if pq.empty(): break thisEdge = pq.get() ifnot visited[thisEdge.toNode]: visited[thisEdge.toNode] = True ans += thisEdge.weight cntEdge += 1 for thatEdge in graph[thisEdge.toNode]: pq.put(thatEdge) if cntEdge isnot n - 1: print("0") else: print(ans)
#include<bits/stdc++.h> usingnamespace std; #define mem(a) memset(a, 0, sizeof(a)) #define dbg(x) cout << #x << " = " << x << endl #define fi(i, l, r) for (int i = l; i < r; i++) #define cd(a) scanf("%d", &a) typedeflonglong ll;
int a[10010];
intmain(){ int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); for (int i = 0; i < n; i++) { printf("%d ", a[i]); } puts(""); } return0; }
#include<bits/stdc++.h> usingnamespace std; #define mem(a) memset(a, 0, sizeof(a)) #define dbg(x) cout << #x << " = " << x << endl #define fi(i, l, r) for (int i = l; i < r; i++) #define cd(a) scanf("%d", &a) typedeflonglong ll;
int a[10010]; int temp[10010];
voidmergeSort(int l, int r){ if (l + 1 == r) { return; } int m = (l + r) >> 1; mergeSort(l, m); mergeSort(m, r); int ll = l, lr = m; // loc l, loc r int lt = l; // loc temp while (ll < m && lr < r) { if (a[ll] < a[lr]) { temp[lt++] = a[ll++]; } else { temp[lt++] = a[lr++]; } } while (ll < m) { temp[lt++] = a[ll++]; } while (lr < r) { temp[lt++] = a[lr++]; } for (int i = l; i < r; i++) { a[i] = temp[i]; } }
intmain(){ int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } mergeSort(0, n); for (int i = 0; i < n; i++) { printf("%d ", a[i]); } puts(""); } return0; }
/* 1 8 42 15 20 6 8 38 50 12
*/
Python
内置sort版:
1 2 3 4 5 6 7 8
T = int(input()) for _CASE inrange(T): n = int(input()) a = list(map(int, input().split())) a.sort() for i in a: print(i, end=' ') print()
defmergeSort(l: int, r: int) -> None: if l + 1 == r: return m = (l + r) >> 1 mergeSort(l, m) mergeSort(m, r) ll, lr = l, m lt = l while ll < m and lr < r: if a[ll] < a[lr]: temp[lt] = a[ll] lt += 1 ll += 1 else: temp[lt] = a[lr] lt += 1 lr += 1 while ll < m: temp[lt] = a[ll] lt += 1 ll += 1 while lr < r: temp[lt] = a[lr] lt += 1 lr += 1 for i inrange(l, r): a[i] = temp[i]
T = int(input()) for _CASE inrange(T): n = int(input()) a = list(map(int, input().split())) mergeSort(0, n) # for i in a: # print(i, end=' ') # print() print(*a)