Postgraduate computer questions (2014)

Source: Internet
Author: User
1. Calculate the sum of prime numbers greater than 1 in a string
The number of input numbers cannot exceed 100. The number cannot exceed 10. The number of input groups must be 0.
Example
Input
4 1 2 3 4
5 1 2 3 4 5
0
Output

5

10

# Include <stdio. h> # define max 10 bool isprime (int n) {// determine whether it is a prime number bool flag = true; If (n <= 1) return false; For (INT I = 2; I * I <= N; I ++) {If (N % I = 0) {flag = false; break ;}} return flag ;} int main () {int res [Max]; int sum, num, N, CNT; CNT = 0; while (scanf ("% d", & N )! = EOF & n! = 0) {sum = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & num ); if (isprime (Num) sum + = num;} res [CNT ++] = sum;} For (INT I = 0; I <CNT; I ++) {printf ("% d \ n", Res [I]);} return 0 ;}



2. Compress strings
A string containing only A-Z cannot exceed 1000 letters, and the same letter is compressed into duplicates + Subtitles (this forgets whether it is a multi-group input or a single group)
Example
Input
Abbccc
Output
A2b3c

#include <stdio.h>#include <string.h>#define N 1000char str[N];char ans[N];int main(){while(scanf("%s",str) != EOF){int len = strlen(str);    ans[0] = str[0];int i = 1,j = 0,count = 1;    char pre = str[0];while(1){char tmp = str[i];if(tmp != pre){j++;if(count >= 2)  ans[j++] = count+'0';count = 1;ans[j] = str[i++];pre = tmp;}else{count++;i++;}if(tmp == '\0')break;}puts(ans);printf("\n");}return 0;}


3. robots walk through the maze
The maze is composed of n w s e. Step N and step N up one grid. Step W to the left one grid. Step s down one grid and Step E to the right.
The number of rows in the input maze cannot exceed 10. the initial number of columns of the robot (note that the number of columns starts from 1) determines whether the machine can exit the maze. Number of outgoing steps
Multiple Input groups end with 0 0
Example
Input
4 6 5
Nnnnsn
Nnnswn
Nnswnn
Nswnnn
3 5 2
Nsnnnn
Nswnnn
Nennnn
0 0 0
Output
7
No

#include <stdio.h>#define MAX 100char buf[MAX][MAX];bool mark[MAX][MAX];int main(){int m,n,sx,sy;while(scanf("%d%d%d%d",&m,&n,&sx,&sy) != EOF){if(m == 0 && n == 0)break;getchar();for(int i = 0; i < m; i++){scanf("%s",buf[i]);getchar();}for( i = 0; i < m; i++){for(int j = 0; j < n; j++)mark[i][j] = false;}int count = 0;while(1){//printf("%d %d\n",sx,sy);if(sx < 0 || sx > m-1 || sy < 0 || sy > n-1){printf("%d\n",count);break;}if(mark[sx][sy]){printf("no\n");break;}switch(buf[sx][sy]){case 'W':mark[sx][sy] = true;sy--;count++;break;case 'E':mark[sx][sy] = true;sy++;count++;break;case 'S':mark[sx][sy] = true;sx++;count++;break;case 'N':mark[sx][sy] = true;sx--;count++;break;}}}return 0;}


4. Ranking of scores (the specific sorting order cannot be remembered, but it is reading files + sorting)
Score from the file. In txt, the student information is read and sorted. The student information includes the number of questions not higher than 20. The number of questions cannot exceed 10. First, the number of questions is the same as the number of answers. dashboard.
Example
File Content
Cs00000001 4 110
Cs00000002 4 120
Cs00000003 5 150


Output
Cs00000003 5 150
Cs00000001 4 110
Cs00000002 4 120

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define N 1000struct sts{char no[30];int  num;int  score;}stu[N];bool cmp(sts A, sts B){if(A.num != B.num)return A.num > B.num;if(A.score != B.score)return A.score < B.score;else{    int tmp = strcmp(A.no,B.no);return tmp < 0;}}int main(){FILE *fp;if((fp = fopen("Score.txt","r")) == NULL){printf("Read file failed\n");return -1;}int i = 0;while(!feof(fp)){fscanf(fp,"%s%d%d",stu[i].no,&stu[i].num,&stu[i].score);    i++;}sort(stu,stu+i,cmp);for(int j = 0; j < i; j++)printf("%s %d %d\n",stu[j].no,stu[j].num,stu[j].score);fclose(fp);return 0;}


Postgraduate computer questions (2014)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.