CSP2015-03

CSP201503-1 图像旋转

 

 

 

 

 1 //
 2 //  main.cpp
 3 //  CSP201503-1 图像旋转
 4 //
 5 //  Created by sylvia on 2021/10/25.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <stdio.h>
11 #include <math.h>
12 using namespace std;
13 
14 #define M 1000+3
15 int a[M][M];
16 int main(){
17     int n,m;
18     cin>>n>>m;
19     for (int i=1;i<=n;i++){
20         for (int j=1;j<=m;j++){
21             cin>>a[i][j];
22         }
23     }
24     for (int i=m;i>=1;i--){
25         for (int j=1;j<=n;j++){
26             cout<<a[j][i]<<' ';
27         }
28         cout<<endl;
29     }
30     return 0;
31 }
View Code

 

  CSP201503-2 数字排序

 

 

 

 练一下sort cmp的写法

 1 //
 2 //  main.cpp
 3 //  CSP201503-2 数字排序
 4 //
 5 //  Created by sylvia on 2021/10/26.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 
 9 #include <math.h>
10 #include <algorithm>
11 #include <iostream>
12 #include <stdio.h>
13 #include <string.h>
14 using namespace std;
15 #define M 1000+5
16 pair<int,int> a[M];
17 int n,x,i=1;
18 int cmp(pair<int,int> a,pair<int,int> b){
19     if(a.first!=b.first) return a.first>b.first;
20     else return a.second<b.second;
21 }
22 int main(){
23     cin>>n;
24     memset(a,0,sizeof(a));
25     while (n--){
26         cin>>x;
27         a[x].first++;
28         a[x].second=x;
29     }
30     sort(a+1,a+1000+1,cmp);
31     while (a[i].first){
32         cout<<a[i].second<<' '<<a[i].first<<endl;
33         i++;
34     }
35     
36     
37     return 0;
38 }

 

  CSP201503-3 节日

 

 

 

 1. 运算符优先级搞错了,!比%高,刚开始没加括号,导致60分 ,戳这里看运算符优先级

2.cout格式化输出,对整数是 cout<<setw(2)<< setfill('0')<<a<<endl; 具体戳这里

 1 //
 2 //  main.cpp
 3 //  CSP201503-3 节日
 4 //
 5 //  Created by sylvia on 2021/10/27.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 
 9 #include <math.h>
10 #include <algorithm>
11 #include <iostream>
12 #include <stdio.h>
13 #include <iomanip>
14 #include <string.h>
15 using namespace std;
16 struct type{
17     int day;
18 }f[203][13][6][8];
19 int judge(int year,int month){
20     switch(month){
21         case 1:
22         case 3:
23         case 5:
24         case 7:
25         case 8:
26         case 10:
27         case 12:{
28             return 31;
29             break;
30         }
31         case 4:
32         case 6:
33         case 9:
34         case 11:{
35             return 30;
36             break;
37         }
38         case 2:{
39             if(!(year%400)) return 29;
40             if((year%100)&&(!(year%4))) return 29;
41             return 28;
42         }
43     }
44     return 1;
45 }
46 int main(){
47     int a,b,c,y1,y2;
48     cin>>a>>b>>c>>y1>>y2;
49     memset(f,0,sizeof(f));
50     int day=2;
51     for (int i=1850;i<=2050;i++) {
52         for (int j=1;j<=12;j++){
53             int daytmp=day;
54             int week=1;
55             for (int k=1;k<=judge(i,j);k++){
56                 f[i-1850][j][week][day].day=k;
57                 day=(day==7)?1:day+1;
58                 day==daytmp?week++:0;
59                 
60             }
61         }
62     }
63     for (int i=y1;i<=y2;i++){
64         if(f[i-1850][a][b][c].day){
65             cout<<i<<'/';
66             cout<<setw(2)<< setfill('0')<<a<<'/';
67             cout<<setw(2)<< setfill('0')<<f[i-1850][a][b][c].day<<endl;
68         }
69         else{
70             cout<<"none"<<endl;
71         }
72     }
73     return 0;
74 }
View Code

 

  CSP 201503-4 网络延迟

 

 

 

 树的直径模板题

dfs或dp两种做法,O(n)复杂度

模板详见&这里

 1 //
 2 //  main.cpp
 3 //  CSP 201503-4 网络延迟
 4 //
 5 //  Created by sylvia on 2021/10/28.
 6 //  Copyright © 2021 apple. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <stdio.h>
11 #include <math.h>
12 #include <algorithm>
13 #include <string.h>
14 using namespace std;
15 #define MAX 20000+5
16 int head[MAX],nxt[MAX],e[MAX],t;
17 int ans;
18 void add(int a,int b){
19     e[t]=b;  //节点编号
20     nxt[t]=head[a];
21     head[a]=t++;
22 }
23 int dfs(int u){
24     int f1=0,f2=0;  //最大值和次大值
25     for (int i=head[u];i!=-1;i=nxt[i]){
26         int j=e[i];  //当前节点编号
27         int f=dfs(j);  //当前节点子树最大距离
28         if(f>f1) f2=f1,f1=f;
29         else if(f>f2) f2=f;
30         
31     }
32     ans=max(ans,f1+f2);
33     return f1+1;  //+1是返回子树根节点到父节点的边
34 }
35 int main(){
36     int x,n,m;
37     cin>>n>>m;
38     memset(head,-1,sizeof(head));
39     for (int i=2;i<=n+m;i++){
40         cin>>x;
41         add(x,i);
42     }
43     dfs(1);
44     cout<<ans<<endl;
45     
46     return 0;
47     
48 }

 

posted @ 2021-10-25 19:23  Sylvia_lee  阅读(40)  评论(0)    收藏  举报