BUCT蓝桥杯热身赛II题解
A题 编码(decode)
签到题,没有可说的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 6 using namespace std; 7 8 int main () { 9 int mark[26]; 10 memset(mark,0,sizeof(mark)); 11 string child,goal;int N; 12 cin >> child >> N >> goal; 13 set<int>s; 14 for (int i = 0;i < child.length();i++) { 15 mark[(N - 1 + i) % 26] = child[i]; 16 s.insert(child[i]); 17 } 18 int pos = N - 1 + child.length(); 19 for (int i = 'A';i <= 'Z';i++) { 20 if (s.count(i)) continue; 21 pos %= 26; 22 s.insert(i); 23 mark[pos] = i; 24 pos++; 25 } 26 for (int i = 0;i < goal.length();i++) { 27 for (int j = 0;j < 26;j++) { 28 if (goal[i] == mark[j]) cout << char(j + 'A'); 29 } 30 } 31 }
B题 阶乘问题(fact)
水题,不多说。钟鹏的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 int main() 3 { 4 int n,cache,sum=1,two=0,five=0; 5 scanf("%d",&n); 6 for(int i=1;i<=n;i++) 7 { 8 if(i%2==0||i%5==0) 9 { 10 cache=i; 11 while(cache%2==0) 12 { 13 cache/=2; 14 two++; 15 } 16 while(cache%5==0) 17 { 18 cache/=5; 19 five++; 20 } 21 sum=sum*cache%10; 22 } 23 else 24 sum=sum*i%10; 25 } 26 while(two-five>0) 27 { 28 sum=sum*2%10; 29 two--; 30 } 31 printf("%d %d",sum,five); 32 return 0; 33 }
C题 集合(subset)
水题,没说的,set用法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 #include <algorithm> 6 using namespace std; 7 8 #define ALL(x) x.begin() , x.end() 9 #define INS(x) inserter(x , x.begin()) 10 11 int main () { 12 set<int>s1; 13 set<int>s2; 14 int n; 15 int x; 16 cin >> n; 17 for (int i = 0;i < n;i++) { 18 cin >> x; 19 s1.insert(x); 20 } 21 cin >> n; 22 for (int i = 0;i < n;i++) { 23 cin >> x; 24 s2.insert(x); 25 } 26 if (s1 == s2) { 27 cout << "A equals B" << endl; 28 } 29 else { 30 set<int>xx; 31 set_intersection(ALL(s1),ALL(s2),INS(xx)); 32 if (xx == s1) { 33 cout << "A is a proper subset of B" << endl; 34 } 35 else if (xx == s2) { 36 cout << "B is a proper subset of A" << endl; 37 } 38 else if (xx.size() == 0) cout << "A and B are disjoint" << endl; 39 else cout << "I'm confused!" << endl; 40 } 41 }
D题 加工生产调度(prod)
简单贪心题,保证 x1 + max(x2,y1) + y2 < x2 + max(x1,y2) + y1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 class Node { 9 public: 10 int x,y; 11 bool operator < (const Node rhs) const { 12 return x + max(rhs.x,y) + rhs.y < rhs.x + max(x,rhs.y) + y; 13 } 14 }; 15 16 int main () { 17 int N; 18 // freopen("1.in","r",stdin); 19 cin >> N; 20 Node a[1010]; 21 for (int i = 0;i < N;i++) cin >> a[i].x; 22 for (int i = 0;i < N;i++) cin >> a[i].y; 23 sort(a,a + N); 24 int start[1010]; 25 int end[1010]; 26 end[0] = a[0].x; 27 for (int i = 1;i < N;i++) { 28 end[i] = a[i].x + end[i - 1]; 29 start[i] = end[i - 1]; 30 } 31 int maxn = end[N - 1]; 32 int ee = 0; 33 for (int i = 0;i < N;i++) { 34 ee = max(end[i],ee) + a[i].y; 35 } 36 cout << ee << endl; 37 }
E题 帕斯卡的旅行(tour)
简单DP题 dp[i][j] 表示从起点到点(i,j)的路径数,所以结果是dp[n - 1][m - 1];
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 long long dp[35][35]; 8 int a[35][35]; 9 10 int main () { 11 int n; 12 // freopen("1.in","r",stdin); 13 cin >> n; 14 char ch; 15 for (int i = 0;i < n;i++) { 16 for (int j = 0;j < n;j++) { 17 cin >> ch; 18 a[i][j] = ch - '0'; 19 } 20 } 21 memset(dp,0,sizeof(dp)); 22 dp[0][0] = 1; 23 for (int i = 0;i < n;i++) { 24 for (int j = 0;j < n;j++) { 25 if (a[i][j] == 0) continue; 26 if (i + a[i][j] < n) { 27 dp[i + a[i][j]][j] += dp[i][j]; 28 } 29 if (j + a[i][j] < n) { 30 dp[i][j + a[i][j]] += dp[i][j]; 31 } 32 } 33 } 34 cout << dp[n - 1][n - 1] << endl; 35 }
F题 泡泡龙(pop)
水题,简单模拟,深搜,广搜都可以,下面是泽中的代码。关键是前几行,注释写的很好。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // _ooOoo_ 2 // o8888888o 3 // 88" . "88 4 // (| -_- |) 5 // O\ = /O 6 // ____/`---'\____ 7 // . ' \\| |// `. 8 // / \\||| : |||// \ 9 // / _||||| -:- |||||- \ 10 // | | \\\ - /// | | 11 // | \_| ''\---/'' | | 12 // \ .-\__ `-` ___/-. / 13 // ___`. .' /--.--\ `. . __ 14 // ."" '< `.___\_<|>_/___.' >'"". 15 // | | : `- \`.;`\ _ /`;.`/ - ` : | | 16 // \ \ `-. \_ __\ /__ _/ .-` / / 17 // ======`-.____`-.___\_____/___.-`____.-'====== 18 // `=---=' 19 // 20 // ............................................. 21 // 佛祖保佑 永无BUG 22 // 佛曰: 23 // 写字楼里写字间,写字间里程序员; 24 // 程序人员写程序,又拿程序换酒钱。 25 // 酒醒只在网上坐,酒醉还来网下眠; 26 // 酒醉酒醒日复日,网上网下年复年。 27 // 但愿老死电脑间,不愿鞠躬老板前; 28 // 奔驰宝马贵者趣,公交自行程序员。 29 // 别人笑我忒疯癫,我笑自己命太贱; 30 // 不见满街漂亮妹,哪个归得程序员? 31 #include<iostream> 32 #include<memory.h> 33 #include<algorithm> 34 using namespace std; 35 36 int map[105][105]; 37 int map1[105][105]; 38 int Count; 39 40 int dfs(int n,int m) 41 { 42 map[n][m]=0; 43 if(map[n][m+1]==Count) dfs(n,m+1); 44 if(map[n][m-1]==Count) dfs(n,m-1); 45 if(map[n+1][m]==Count) dfs(n+1,m); 46 if(map[n-1][m]==Count) dfs(n-1,m); 47 return 0; 48 } 49 50 int main() 51 { 52 int m,n; 53 cin>>n>>m; 54 memset(map,-1,sizeof(map)); 55 memset(map1,-1,sizeof(map1)); 56 57 for(int i=1;i<=n;i++) 58 for(int j=1;j<=n;j++) 59 cin>>map[i][j]; 60 Count=map[n][m]; 61 dfs(n,m); 62 for(int j=1;j<=n;j++) 63 { 64 for(int i=n;i>=1;i--) 65 { 66 for(int k=i-1;k>=1;k--) 67 { 68 if(map[i][j]==0&&map[k][j]!=0) 69 { 70 map[i][j]=map[k][j]; 71 map[k][j]=0; 72 } 73 } 74 } 75 } 76 for(int i=1;i<=n;i++) 77 { 78 for(int j=1;j<=n;j++) 79 { 80 cout<<map[i][j]<<' '; 81 } 82 cout<<endl; 83 } 84 return 0; 85 }
G题卫星照片(satel)
水题,跟某年的NOIP统计细胞很像,随便深搜,广搜一下就搞定了。下面是罗的代码,供大家学习一下,挺不错的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define MAXN 80 5 using namespace std; 6 int dir[4][2]={1,0,-1,0,0,1,0,-1},R,C; 7 char mp[MAXN][MAXN]; 8 bool vis[MAXN][MAXN]; 9 int dfscnt; 10 int dfs(int x,int y){ 11 dfscnt++; 12 vis[x][y]=1; 13 for(int i=0;i<4;i++){ 14 int tx=x+dir[i][0],ty=y+dir[i][1]; 15 if(tx<0 || tx>=R || ty<0 || ty>=C || vis[tx][ty] || mp[tx][ty]=='.') continue; 16 dfs(tx,ty); 17 } 18 } 19 bool IsSq(int x,int y,int cnt){ 20 int row=0,col=0; 21 for(int i=x;i<R;i++) 22 if(mp[i][y]=='.') break; 23 else row++; 24 for(int i=y;i<C;i++) 25 if(mp[x][i]=='.') break; 26 else col++; 27 for(int i=x;i<x+row;i++) 28 for(int j=y;j<y+col;j++) 29 if(mp[i][j]=='.') return 0; 30 else cnt--; 31 if(cnt==0) return 1; 32 return 0; 33 } 34 int main(){ 35 while(~scanf("%d %d ",&R,&C)){ 36 for(int i=0;i<R;i++) 37 scanf("%s",mp[i]); 38 int ans1=0,ans2=0; 39 memset(vis,0,sizeof(vis)); 40 for(int i=0;i<R;i++) 41 for(int j=0;j<C;j++) 42 if(mp[i][j]=='#' && !vis[i][j]){ 43 dfscnt=0; 44 dfs(i,j); 45 if(IsSq(i,j,dfscnt)) ans1++; 46 else ans2++; 47 } 48 printf("%d\n%d\n",ans1,ans2); 49 } 50 }