2013年CCF考试真题及答案
- 格式:doc
- 大小:102.50 KB
- 文档页数:8
更多CCF真题及答案,请搜索:/acpchenpeng/article/category/5965279试题编号:201312-1试题名称:出现次数最多的数1.#include<cstdio>2.#include<cstdlib>3.#include<iostream>4.#include<string>5.#include<vector>6.#include<list>7.#include<map>8.ing namespace std;10.11.int main()12.{13.int n,m,max;14.int num=0;15. cin >> n;16. map<int,int> f; //有序容器17.18.for(int i = 0;i < n; i++)19. {20. cin>>m;21. f[m]++;22. }23.24.for(map<int,int>::iterator it = f.begin();it!=f.end();it++)25. {26.if(it->second > num)27. {28. num = it->second;29. max = it->first;30. }31. }32.33. cout << max;34.35.return 0;36.37.}201312-2试题名称:ISBN号码[cpp]view plaincopy1.#include<cstdlib>2.#include<iostream>3.#include<string>4.#include<vector>5.#include<map>6.ing namespace std;8.9.int main()10.{11. string s;12. cin >> s;13.int a[10]={0};14.int sum =0;15.int j =0;16.for(string::iterator i = s.begin() ;i != s.end(); i++)17. {18.19.if(*i != '-')20. {21. a[j] = *i - '0';22.if(j < 9)23. sum += a[j] * (j+1);24. j++;25. }26. }27.28.29.if(sum % 11 == a[9] ||(sum % 11 == 10 && s[12] == 'X'))30. cout << "Right" << endl;31.32.else33. {34.if(sum % 11 == 10)35. s[12] = 'X';36.else37. s[12] = sum % 11 + '0'; //不要忘记+‘0’38. cout << s << endl;39.40. }41.42.return 0;43.}试题编号: 3试题名称:最大的矩形[cpp]view plaincopy1.#include<cstdlib>2.#include<iostream>3.#include<string>4.#include<vector>5.#include<map>6.ing namespace std;8.9.int main()10.{11.int n,m;12. cin >> n;13.14. vector<int> v;15.for(int i =0; i< n;i++)16. {17. cin >> m;18. v.push_back(m);19. }20.21.int s_max=0; //全局变量,用于保存最大的面积22.23.for(int i=0;i<n;i++)24. {25.int h_min = v[i]; //保存最小高26.27.for(int j=i;j<n;j++)28. {29.if(v[j] < h_min)30. h_min = v[j]; //更新最小高,注意是v[j],不是j31.int s = h_min * (j-i+1); //计算当前面积32.if( s > s_max)33. s_max = s; //更新最大面积34. }35. }36. cout << s_max <<endl;37.38.return 0;39.}试题编号:201312-4试题名称:有趣的数#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <deque>#include <list>using namespace std;long long f[2000][3][2]; // f[seq_k to place][0: to place 0 , 1: ethier 0 or 1, 2 : must be 1][3 is placed ?1 : 0]int dp(int n, int p1, int p3){long long &now = f[n][p1][p3];if (now != -1)return now;if (n == 0){if (p1 == 2 && p3 == 1){now = 1;}else{now = 0;}return now;}now = 0;if (p1 == 0){now += dp(n-1, 1, p3); // go 0}else if (p1 == 1){now += dp(n-1, 1, p3); // go 0 now += dp(n-1, 2, p3); // go 1 }else // p1 == 2{now += dp(n-1, 2, p3); // go 1}if (p3 == 0){now += dp(n-1, p1, p3); // go 2;now += dp(n-1, p1, 1); // go 3;}else{now += dp(n-1, p1, 1); // go 3;}now %= 1000000007;}int main(){int n;cin >> n;memset(f, -1, sizeof(f));int ans = dp(n - 1, 0, 0); // seq[n] is 2cout << ans << endl;return 0;}试题编号:201312-5试题名称:I’m stuck!#include <cstdio>#include <string>#include <vector>#include <deque>#include <cstring>#include <list>using namespace std;//class Move{ public:virtual bool CanMove(char from, char to, int dx, int dy) = 0; };class ForwardMove : public Move{public:virtual bool CanMove(char from, char to, int dx, int dy) {if (to == '#') return false;switch (from){case '+' : case 'S' : case 'T' : return true; break;case '-' : return dy != 0; break;case '|' : return dx != 0; break;case '.' : return dx == 1; break;}return false;}};class BackwardMove : public Move{public:virtual bool CanMove(char from, char to, int dx, int dy) {if (to == '#') return false;switch (to){case '+' : case 'S' : case 'T' : return true; break;case '-' : return dy != 0; break;case '|' : return dx != 0; break;case '.' : return dx == -1; break;}return false;};char s[100][100];typedef bool ARR[100][100];ARR bs, bt;int sx, sy, tx, ty;int d[4][2] = {{-1, 0},{1, 0},{0, 1},{0, -1}};void Bfs(ARR b, Move *move, int x, int y){if (b[x][y])return;b[x][y] = true;for (int o = 0; o < 4; o++){int dx = d[o][0];int dy = d[o][1];int xx = x + dx;int yy = y + dy;if (move->CanMove(s[x][y], s[xx][yy], dx, dy)) {Bfs(b, move, xx, yy);}}}int n, m;int main(){cin >> n >> m;for (int i = 0; i <= n + 1; i++)for (int j = 0; j <= m + 1; j++)s[i][j] = '#';for (int i = 1; i <= n; i++)cin >> s[i]+1;for (int i = 0; i <= n + 1; i++)s[i][m + 1] = '#';for (int i = 0; i <= n + 1; i++){for (int j = 0; j <= m + 1; j++){if (s[i][j] == 'S'){sx = i; sy = j;}if (s[i][j] == 'T'){tx = i;ty = j;}}}Bfs(bs, new ForwardMove(), sx, sy); Bfs(bt, new BackwardMove(), tx, ty);int ans = 0;for (int i = 0; i <= n + 1; i++){for (int j = 0; j <= m + 1; j++){if (bs[i][j] && ! bt[i][j])ans ++;}}if (bs[tx][ty] == false)cout << "I'm stuck!" << endl;elsecout << ans << endl;return 0;}。