每日打卡 2017.03.15 北大信科2017机试真题题解

来源:互联网 发布:f.lux for mac下载 编辑:程序博客网 时间:2024/04/27 14:23

注:由于系统尚未加入题目,暂时还不知道准确性


1.奖学金


#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int n;const int MAXN = 300 + 5;struct Node{int num;int chi;int math;int eng;int tot;}node[MAXN];bool operator < (const Node&a, const Node&b){if(a.tot != b.tot) return a.tot > b.tot;if(a.chi != b.chi) return a.chi > b.chi;return a.num < b.num;}int main(){//freopen("input.txt", "r", stdin);sf(n);for(int i = 0; i < n; i++){node[i].num = i;sf(node[i].chi);sf(node[i].math);sf(node[i].eng);node[i].tot = node[i].chi + node[i].math + node[i].eng;}sort(node, node + n);for(int i = 0; i < 5; i++){cout << node[i].num + 1 << ' ' << node[i].tot << endl;}return 0;}


2.高次同余方程求解

#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int n, k;const int MAXN = 300 + 5;int main(){//freopen("input.txt", "r", stdin);cin >> k >> n;for(int x = 1; x < n; x++){int p = x;for(int j = 1; j < k; j++){p*= x;p%= n;}if(p == 1) cout << x << endl;} return 0;}


3.二维数组回形遍历


#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int r, c;const int MAXN = 100 + 5;int a[MAXN][MAXN];bool vis[MAXN][MAXN];int p = 0;const int dr[] = {0, 1, 0, -1};const int dc[] = {1, 0, -1, 0};void dfs(int row, int col){vis[row][col] = true;cout << a[row][col] << endl;if(row + dr[p] <= r && row + dr[p] >= 1 && col + dc[p] <= c && col + dc[p] >= 1 && !vis[row+dr[p]][col+dc[p]])dfs(row + dr[p], col + dc[p]);else{p++;p%= 4; if(row + dr[p] <= r && row + dr[p] >= 1 && col + dc[p] <= c && col + dc[p] >= 1 && !vis[row+dr[p]][col+dc[p]])dfs(row + dr[p], col + dc[p]);}}int main(){//freopen("input.txt", "r", stdin);sf(r);sf(c);for(int i = 1; i <= r; i++){for(int j = 1; j <= c; j++){sf(a[i][j]);}}dfs(1, 1);return 0;}

4.Black Jack

#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<cstdio>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int n, m;const int MAXN = 11;int a[MAXN], b[MAXN];int atot, btot;char str[4];bool bj(int k){if(k == 0){if(n != 2 || atot != 21)return false;}else{if(m != 2 || btot != 21)return false;}return true;}int main(){//freopen("input.txt", "r", stdin);sf(n);for(int i = 0; i < n; i++){sfs(str);if(strlen(str) == 2) a[i] = 10;else{if(str[0] >= '2' && str[0] <= '9') a[i] = str[0] - '0';else if(str[0] != 'A') a[i] = 10;else a[i] = 1;}atot+= a[i]; }sf(m);for(int i = 0; i < m; i++){sfs(str);if(strlen(str) == 2) b[i] = 10;else{if(str[0] >= '2' && str[0] <= '9') b[i] = str[0] - '0';else if(str[0] != 'A') b[i] = 10;else b[i] = 1;}btot+= b[i]; }if(atot <= 11)for(int i = 0; i < n; i++){if(a[i] == 1){a[i] = 11;atot+= 10;break;}}if(btot <= 11)for(int i = 0; i < m; i++){if(b[i] == 1){b[i] = 11;btot+= 10;break;}}int ok = 0;if(btot > 21)ok = 1;else{if(atot > 21) ok = 2;else{if(bj(0)){if(!bj(1)) ok = 1;}else{if(bj(1)){if(!bj(0)) ok = 2;}else{if(atot > btot) ok = 1;else if(atot < btot) ok = 2;}}}}if(ok){if(ok == 1)cout << "win" << endl;elsecout << "lose" << endl;}else{cout << "draw" << endl;}return 0;}


5.我爱学习!


#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<cstdio>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int n, m, len;const int MAXN = 22;char a[MAXN][MAXN];char str[102];bool zz(int p, int q){int l = strlen(a[q]);if(p + l>len)return false;for(int i = 0; i < l; i++){if(str[p + i] != a[q][i]) return false;}return true;}int main(){//freopen("input.txt", "r", stdin);scanf("%d %d\n", &n, &m);for(int i = 0; i < n; i++){gets(a[i]);}while(m--){gets(str);len = strlen(str);for(int i = 0; i < len; i++){int ok = 0;for(int j = 0; j < n; j++){if(zz(i, j)){ok = strlen(a[j]);break;}}if(ok > 0){i+=ok-1;for(int j = 0; j < ok; j++)printf("*");}elseprintf("%c", str[i]);}printf("\n");}return 0;}



6.水淹七军

(题目有歧义)

#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<cstdio>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int n, m, len, p;const int MAXN = 202;int a[MAXN][MAXN];int vis[MAXN][MAXN];int rp, cp;int rw, cw;const int dr[] = {1, 0, -1, 0};const int dc[] = {0, 1, 0, -1};void dfs(int row, int col){vis[row][col] = 1;for(int i = 0; i < 4; i++){int r = row + dr[i], c = col + dc[i];if(r <= m && r >= 1 && c <= n && c >= 1 && vis[r][c] == 0 && a[row][col] > a[r][c])dfs(r, c);//此处题目有歧义,水往低处流不明 }} int main(){//freopen("input.txt", "r", stdin);int K, t1, t2;sf(K);while(K--){memset(vis, 0, sizeof(vis));sf(m);sf(n);for(int i = 1; i <= m; i++){for(int j = 1; j <= n; j++){sf(a[i][j]);}}sf(rp);sf(cp);sf(p);while(p--){sf(t1);sf(t2);dfs(t1, t2); }cout << (vis[rp][cp] == 0 ? "No" : "Yes") << endl;}return 0;}

7.鸡蛋的硬度(待改)

#include<iostream>    #include<algorithm>    using namespace std;    const int MAXBall=55,MAXFloor=1010;    int DP[MAXBall][MAXFloor];    int main()    {        //freopen("input.txt","r",stdin);      for(int i=0;i<=1000;i++) DP[1][i]=i;        for(int i=2;i<=50;i++)            for(int j=1;j<=1000;j++)            {                if(i>j) DP[i][j]=DP[j][j];                 else DP[i][j]=DP[i-1][j-1]+DP[i][j-1]+1;               if(DP[i][j]>1010) DP[i][j]=1010;            }        int n,th,ball,floor;        while(cin>>floor >> ball)          cout<<lower_bound(DP[ball],DP[ball]+1000,floor)-DP[ball]<<endl;        return 0;}    

8.拓扑排序(AC)


#include<iostream>      #include<algorithm>      #include<cstdio>  #define sf(a) scanf("%d", &a)   using namespace std;        int v, a;  const int MAXN = 1005;     struct Node  {      int num;      int inv[MAXN];      int inn;  }node[MAXN];    int vis[MAXN];    int main()      {          //freopen("input.txt","r",stdin);        sf(v);      sf(a);      int t1, t2;      for(int i = 0; i < a; i++)      {          sf(t1);          sf(t2);          node[t2].inv[node[t2].inn++] = t1;      }      int tot = 0;      int flag = 1;      while(tot < v)      {          for(int i = 1; i <= v; i++)          {              if(vis[i] == 1)continue;              int ok = 1;              for(int j = 0; j < node[i].inn; j++)              {                  if(vis[node[i].inv[j]] == 0)                  {                      ok = 0;                      break;                  }              }              if(ok == 1)              {                  if(tot != 0)printf(" ");                  vis[i] = 1;                  printf("v%d", i);                  tot++;                  break;            }          }      }      printf("\n");      return 0;  } 



9.树的转换(AC)


#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#include<cstring>#define sf(a) scanf("%d", &a)#define sfs(a) scanf("%s", a)using namespace std;int n, k;const int MAXN = 10000 + 5;char str[MAXN];char p[MAXN];int len;struct Node{int cc;Node *lchild, *rchild;Node():lchild(NULL), rchild(NULL) {}};void del(Node *root){if(root != NULL){del(root->lchild);del(root->rchild);delete root;}}int z;Node* build(){if(p[z++] == 'u'){return NULL;}else{Node* ret = new Node();ret->cc = z;ret->lchild = build();ret->rchild = build();return ret;}}int depth(Node *root){if(root == NULL) return 0;else return max(depth(root->lchild), depth(root->rchild)) + 1;}int main(){//freopen("input.txt", "r", stdin);int kase = 0;while(sfs(str)){memset(p, 0, sizeof(p));int d1 = 0;len = strlen(str);if(len == 1)break;int tmp = 0;p[0] = 'd';for(int i = 0; i < len; i++){p[i+1] = str[i];if(str[i] == 'd'){tmp++;if(tmp > d1) d1 = tmp;}elsetmp--;}p[len+1] = 'u';p[len+2] = 'u';z = 0;Node *root = build();cout << "Tree " << ++kase << ": " << d1 << " => " << depth(root) - 1 << endl;del(root);}return 0;}


10.Strange Card Game


0 0
原创粉丝点击