CCF-CSP-2013年12月-题解
2015-12-11 16:53
507 查看
①出现次数最多的数
问题描述给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
输入格式
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出格式
输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
样例输入
6
10 1 10 20 30 20
样例输出
10
代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; int n; int num[10005]; int main() { while(scanf("%d", &n) != EOF) { memset(num, 0, sizeof(num)); for(int i = 0; i < n; i ++) { int x; scanf("%d", &x); num[x] ++; } int ans = 0; int cnt = num[0]; for(int i = 1; i < 10005; i ++) { if(num[i] > cnt) { ans = i; cnt = num[i]; } } printf("%d\n", ans); } return 0; }
②ISBN号码
问题描述每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔之后的五位数字代表该书在出版社的编号;最后一位为识别码。
识别码的计算方法如下:
首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,…,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。
编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出是正确的ISBN号码。
输入格式
输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN号码的格式要求)。
输出格式
输出一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。
样例输入
0-670-82162-4
样例输出
Right
样例输入
0-670-82162-0
样例输出
0-670-82162-4
代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; char s[1005]; int main() { while(scanf("%s", s) != EOF) { int sum = 0; sum += s[0] - '0'; sum += (s[2] - '0') * 2; sum += (s[3] - '0') * 3; sum += (s[4] - '0') * 4; sum += (s[6] - '0') * 5; sum += (s[7] - '0') * 6; sum += (s[8] - '0') * 7; sum += (s[9] - '0') * 8; sum += (s[10] - '0') * 9; //cout << sum << endl; int shibiema = s[12] - '0'; if((sum % 11 == 10 && s[12] == 'X') || (sum % 11 == shibiema)) { printf("Right\n"); } else { s[12] = '\0'; printf("%s", s); if(sum % 11 == 10) printf("X\n"); else printf("%d\n", sum % 11); } } return 0; }
③最大的矩形
在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi。这n个矩形构成了一个直方图。例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3。请找出能放在给定直方图里面积最大的矩形,它的边要与坐标轴平行。对于上面给出的例子,最大矩形如下图所示的阴影部分,面积是10。
输入格式
第一行包含一个整数n,即矩形的数量(1 ≤ n ≤ 1000)。
第二行包含n 个整数h1, h2, … , hn,相邻的数之间由空格分隔。(1 ≤ hi ≤ 10000)。hi是第i个矩形的高度。
输出格式
输出一行,包含一个整数,即给定直方图内的最大矩形的面积。
样例输入
6
3 1 6 5 2 3
样例输出
10
代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; int n; int h[1005]; int main() { while(scanf("%d", &n) != EOF) { for(int i = 0; i < n; i ++) { scanf("%d", &h[i]); } int ans = 0; for(int i = 0; i < n; i ++) { int hmin = h[i]; for(int j = i; j < n; j ++) { hmin = min(hmin, h[j]); ans = max(ans, hmin * (j - i + 1)); } } printf("%d\n", ans); } return 0; }
④有趣的数
数位DP问题描述
我们把一个数称为有趣的,当且仅当:
1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次。
2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前。
3. 最高位数字不为0。
因此,符合我们定义的最小的有趣的数是2013。除此以外,4位的有趣的数还有两个:2031和2301。
请计算恰好有n位的有趣的数的个数。由于答案可能非常大,只需要输出答案除以1000000007的余数。
输入格式
输入只有一行,包括恰好一个正整数n (4 ≤ n ≤ 1000)。
输出格式
输出只有一行,包括恰好n 位的整数中有趣的数的个数除以1000000007的余数。
样例输入
4
样例输出
3
代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; #define MOD 1000000007 LL dp[1005][6]; void init() { dp[4][0] = 1;//dp[i][0]表示长度为i时,只含有{2}时的数目 dp[4][1] = 7;//dp[i][1]表示长度为i时,只含有{2,0}时的数目 dp[4][2] = 5;//dp[i][2]表示长度为i时,只含有{2,0,1}时的数目 dp[4][3] = 3;//dp[i][3]表示长度为i时,只含有{2,3}时的数目 dp[4][4] = 9;//dp[i][4]表示长度为i时,只含有{2,0,3}时的数目 dp[4][5] = 3;//dp[i][5]表示长度为i时,只含有{2,0,3,1}时的数目 for(int i = 5; i < 1005; i ++) { dp[i][0] = 1; dp[i][1] = (dp[i-1][0] + 2 * dp[i-1][1]) % MOD; dp[i][2] = (dp[i-1][1] + 2 * dp[i-1][2]) % MOD; dp[i][3] = (dp[i-1][0] + dp[i-1][3]) % MOD; dp[i][4] = (dp[i-1][1] + dp[i-1][3] + 2 * dp[i-1][4]) % MOD; dp[i][5] = (dp[i-1][2] + dp[i-1][4] + 2 * dp[i-1][5]) % MOD; } } int main() { init(); int n; while(scanf("%d", &n) != EOF) { printf("%lld\n",dp [5]); } return 0; }
⑤I’m stuck!
做两遍BFS即可。第一遍对所有从起始点可以到达的点,打上标记,顺便记录起始点是否可以达到终点。
第二遍则对那些从起始点可以到达的点进行bfs,看是否能够到达终点。
问题描述
给定一个R行C列的地图,地图的每一个方格可能是’#’, ‘+’, ‘-‘, ‘|’, ‘.’, ‘S’, ‘T’七个字符中的一个,分别表示如下意思:
‘#’: 任何时候玩家都不能移动到此方格;
‘+’: 当玩家到达这一方格后,下一步可以向上下左右四个方向相邻的任意一个非’#’方格移动一格;
‘-‘: 当玩家到达这一方格后,下一步可以向左右两个方向相邻的一个非’#’方格移动一格;
‘|’: 当玩家到达这一方格后,下一步可以向上下两个方向相邻的一个非’#’方格移动一格;
‘.’: 当玩家到达这一方格后,下一步只能向下移动一格。如果下面相邻的方格为’#’,则玩家不能再移动;
‘S’: 玩家的初始位置,地图中只会有一个初始位置。玩家到达这一方格后,下一步可以向上下左右四个方向相邻的任意一个非’#’方格移动一格;
‘T’: 玩家的目标位置,地图中只会有一个目标位置。玩家到达这一方格后,可以选择完成任务,也可以选择不完成任务继续移动。如果继续移动下一步可以向上下左右四个方向相邻的任意一个非’#’方格移动一格。
此外,玩家不能移动出地图。
请找出满足下面两个性质的方格个数:
1. 玩家可以从初始位置移动到此方格;
2. 玩家不可以从此方格移动到目标位置。
输入格式
输入的第一行包括两个整数R 和C,分别表示地图的行和列数。(1 ≤ R, C ≤ 50)。
接下来的R行每行都包含C个字符。它们表示地图的格子。地图上恰好有一个’S’和一个’T’。
输出格式
如果玩家在初始位置就已经不能到达终点了,就输出“I’m stuck!”(不含双引号)。否则的话,输出满足性质的方格的个数。
样例输入
5 5
–+-+
..|#.
..|##
S-+-T
####.
样例输出
2
样例说明
如果把满足性质的方格在地图上用’X’标记出来的话,地图如下所示:
–+-+
..|#X
..|##
S-+-T
####X
代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; int R, C; char mp[55][55]; int vis[55][55]; int f[55][55]; int flag;//ÓÃÓڼǼÆðʼµãÊÇ·ñÄܹ»µ½´ïÖÕµã const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; struct node { int x, y; node(int _x, int _y) : x(_x), y(_y) {} }; void bfs(int x, int y) { memset(vis, 0, sizeof(vis)); queue<node> que; vis[x][y] = 1; f[x][y] = 1; que.push(node(x, y)); while(!que.empty()) { node p = que.front(); que.pop(); int ch = mp[p.x][p.y]; if(ch == 'S' || ch == '+' || ch == 'T') { for(int i = 0; i < 4; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') flag = 1; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; f[xx][yy] = 1; que.push(node(xx, yy)); } } } else if(ch == '|') { for(int i = 0; i < 2; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') flag = 1; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; f[xx][yy] = 1; que.push(node(xx, yy)); } } } else if(ch == '-') { for(int i = 2; i < 4; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') flag = 1; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; f[xx][yy] = 1; que.push(node(xx, yy)); } } } else if(ch == '.') { for(int i = 1; i < 2; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') flag = 1; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; f[xx][yy] = 1; que.push(node(xx, yy)); } } } } } bool judge(int x, int y) { memset(vis, 0, sizeof(vis)); queue<node> que; vis[x][y] = 1; que.push(node(x, y)); while(!que.empty()) { node p = que.front(); que.pop(); int ch = mp[p.x][p.y]; if(ch == 'S' || ch == '+' || ch == 'T') { for(int i = 0; i < 4; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') return true; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; que.push(node(xx, yy)); } } } else if(ch == '|') { for(int i = 0; i < 2; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') return true; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; que.push(node(xx, yy)); } } } else if(ch == '-') { for(int i = 2; i < 4; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') return true; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; que.push(node(xx, yy)); } } } else if(ch == '.') { for(int i = 1; i < 2; i ++) { int xx = p.x + dx[i]; int yy = p.y + dy[i]; if(mp[xx][yy] == 'T') return true; if(xx >= 1 && xx <= R && yy >= 1 && yy <= C && !vis[xx][yy] && mp[xx][yy] != '#') { vis[xx][yy] = 1; que.push(node(xx, yy)); } } } } return false; } int main() { while(scanf("%d %d", &R, &C) != EOF) { memset(f, 0, sizeof(f)); flag = 0; for(int i = 1; i <= R; i ++) { scanf("%s", mp[i] + 1); } for(int i = 1; i <= R; i ++) { for(int j = 1; j <= C; j ++) { if(mp[i][j] == 'S') { bfs(i, j); } } } if(!flag) { printf("I'm stuck!\n"); continue; } int ans = 0; for(int i = 1; i <= R; i ++) { for(int j = 1; j <= C; j ++) { if(f[i][j] == 1) { if(!judge(i, j)) { ans ++; //cout << i << " " << j << endl; } } } } printf("%d\n", ans); } return 0; }
相关文章推荐