1. 程式人生 > >CCF CSP歷年一二題代碼匯總

CCF CSP歷年一二題代碼匯總

efi min lse 循環 scanf font color 小球 inf

實話說如果不是為了騙訪問量,才不會寫12題的qwq

201803-1 跳一跳

第一題向來是送分題。。。但是註意讀題。。。

模擬題按題意走是墜穩的

技術分享圖片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define LL long long
 6 #define debug(x) cout << "[" << x << "]" << endl
 7
using namespace std; 8 9 int main(){ 10 int ans = 0, last = 0, n; 11 while (scanf("%d", &n) == 1 && n){ 12 if (n == 2){ 13 if (ans == 0 || last == 1){ 14 last = 2; 15 ans += 2; 16 } 17 else { 18
last += 2; 19 ans += last; 20 } 21 } 22 else { 23 ans++; 24 last = 1; 25 } 26 } 27 printf("%d\n", ans); 28 return 0; 29 }
View Code

201803-2 碰撞的小球

t只有100,n也只有100,tn^2隨便模擬一下。。

技術分享圖片
 1
#include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define LL long long 6 #define debug(x) cout << "[" << x << "]" << endl 7 using namespace std; 8 9 int a[110], b[110]; 10 11 int main(){ 12 int n, l, t; 13 scanf("%d%d%d", &n, &l, &t); 14 for (int i = 1; i <= n; i++){ 15 scanf("%d", &a[i]); 16 b[i] = 1; 17 } 18 while (t--){ 19 for (int i = 1; i <= n; i++){ 20 for (int j = i+1; j <= n; j++){ 21 if (i == j) continue; 22 if (a[i] == a[j]){ 23 b[i] = -b[i]; 24 b[j] = -b[j]; 25 } 26 } 27 if (a[i] == 0 || a[i] == l) b[i] = -b[i]; 28 } 29 for (int i = 1; i <= n; i++) a[i] += b[i]; 30 } 31 for (int i = 1; i <= n; i++) 32 printf("%d%c", a[i], i == n ? \n : ); 33 return 0; 34 }
View Code

201712-1 最小差值

暴力或排序隨便怎麽實現都行

技術分享圖片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define INF 0x3f3f3f3f
 6 #define LL long long
 7 #define debug(x) cout << "[" << x << "]" << endl
 8 using namespace std;
 9 
10 int a[1010];
11 
12 int main(){
13     int n;
14     scanf("%d", &n);
15     for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
16     sort(a+1, a+n+1);
17     int ans = INF;
18     for (int i = 1; i < n; i++)
19         ans = min(ans, a[i+1]-a[i]);
20     printf("%d\n", ans);
21     return 0;
22 }
View Code

201712-2 遊戲

寫個死循環模擬到只剩一個人為止

技術分享圖片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define LL long long
 6 #define debug(x) cout << "[" << x << "]" << endl
 7 using namespace std;
 8 
 9 bool vis[1010];
10 
11 int main(){
12     int n, k, num = 0, sum = 0;
13     scanf("%d%d", &n, &k);
14     while (1){
15         for (int i = 1; i <= n; i++){
16             if (vis[i]) continue;
17             num++;
18             if (num%10 == k || num%k == 0) {
19                 vis[i] = 1;
20                 sum++;
21             }
22             if (sum == n-1) break;
23         }
24         if (sum == n-1) break;
25     }
26     for (int i = 1; i <= n; i++){
27         if (!vis[i]){
28             printf("%d\n", i);
29             break;
30         }
31     }
32     return 0;
33 }
View Code

201709-1 打醬油

算就行

技術分享圖片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define LL long long
 6 #define debug(x) cout << "[" << x << "]" << endl
 7 using namespace std;
 8 
 9 int main(){
10     int n, ans = 0;
11     scanf("%d", &n);
12     n /= 10;
13     ans += n/5*7;
14     n %= 5;
15     ans += n/3*4;
16     n %= 3;
17     ans += n;
18     printf("%d\n", ans);
19     return 0;
20 }
View Code

CCF CSP歷年一二題代碼匯總