[Programming clip] 06. Answers to questions on computer in Tsinghua computer postgraduate exams (some test cases cannot pass)

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. Questions answered during the (one of the test cases that I did this morning cannot be completed ~)

 

Department of Computer Science and Technology, Tsinghua University

2007Postgraduate Enrollment Review

2007Year3Month24Day

Note:

1. There are three questions in total, with a total score of 100. The test time is one and a half hours.

2. Do not use your own electronic devices, including laptops, USB drives, and mobile phones. Do not use reference books or materials.

3. The programming environment is Windows 2000 Professional + Visual Studio 6.0 and can only be C/C ++.

4. The input data of each question is read from the file input.txt and the result is output to the file output.txt,Strictly follow the input and output formats of each question. During the test, we will not provide test data except for the sample in the test questions. Please generate your own input data for self-testing of the program.

5. Set the compilation environment and configure compilation parameters before the exam ends, compile the program into an executable file, and the file name is specified in each question.The generated executable file will be the only basis for the final test.If you cannot run your executable file, the final score is recorded as zero.

6. The program can run each test data for up to 1 second. If the test data times out or the result is incorrect, the test case cannot be scored.

7. If the computer fails during the examination, please notify the staff in time to avoid delay in your examination time.

8. Do not leave the computer immediately after the examination is completed. Our staff will perform on-site tests directly. Your cooperation is required.

Question 1 (Executable File Name: program1.exe)

Returns the number of prime factors of an integer n (n> 1. Note: 1 is not a prime factor of N: If n is a prime number, n is a prime factor of N. Duplicate calculation is required for the same prime factor.

For example, 120 = 2*2*2*3*5, there are 5 prime factors in total.

Input:

Positive integer N, 1 <n <109

Output:

Number of prime factors of N

Sample input:

120

Sample output

5

#include<stdlib.h>#include<stdio.h>#include<math.h> int main(void){    int i=2,count=1;    long int N;    char buffer[10];    FILE *fp1,*fp2;    fp1=fopen("input","r");    fgets(buffer,10,fp1);    N=atol(buffer);    while(i<=sqrt(N))    {        for(;i<=sqrt(N);i++)        if(N%i==0)        {            N=N/i;            printf("%d*",i);            count++;            break;        }    }    printf("%d",N);    fp2=fopen("output","w");    itoa(count,buffer,10);    fputs(buffer,fp2);    rewind(fp2);    fclose(fp1);    fclose(fp2);    return 0;}

Question 2 (Executable File Name: program2.exe)

For a decimal number A, convert a to a binary number, then sort it in reverse order by bit, and then convert it to a decimal number B. we multiply the value of B toBinary Reverse Order count.

For example, if the decimal number is 173, the binary format is 101011101, and 10110101 is obtained in reverse order. The decimal number is 181,181, that is, the binary inverse Number of 173.

Input:

A decimal number within 1000 bits (2999.

Output:

The binary reverse order of the input decimal number.

Sample input:

173

Sample output:

181

(The following programs can only tolerate some test cases)

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h> #define MAX 100#define LEN 1000 long int process(long int N){    long int M=0;    int count=0;    int i,j;    int temp[LEN];    int *p=temp;    int tempr[LEN];    int *r=tempr;    long int tmpnumber=N;    for(i=0;tmpnumber!=0;tmpnumber/=2,i++)    {        *p++=tmpnumber%2;        *r++=pow(2,i);        count++;    }     for(i=0,j=count-1;i<count;i++,j--)    {        printf("temp[i]*tempr[j]=%d/n",temp[i]*tempr[j]);        M+=temp[i]*tempr[j];    }     return M;} int main(void){    long int N,M;    FILE *fp1,*fp2;    char buffer[MAX];    fp1=fopen("input","r");    fgets(buffer,MAX,fp1);       N=atol(buffer);     M=process(N);     fp2=fopen("output","w");    ltoa(M,buffer,10);    fputs(buffer,fp2);    fclose(fp1);    fclose(fp2);    return 0;}

Question 3 (Executable File Name: program3.exe)

There are several stamps that require the minimum number of stamps to be selected to generate a given total value.

For example, if one, three, and four stamps are required to be scored as 10, three stamps are used: 3, 3, and 4.

Input:

The first is the total value of the stamp M, m <100

Then there is a number N, N <20, indicating there are n stamps. Next there are n positive integers, which represent the face values of the N stamps in ascending order.

Output:

The minimum number of stamps that can generate a total value of M. If no solution is available, 0 is output.

Sample input:

10 5 1 3 3 3 4

Sample output:

3

Analysis: This is the simplest problem with backpacks. The dynamic planning method is not well written, but the results are good and linear complexity ~~

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h> #define MAX 100#define LEN 1000 void process(int M,int *a,int offset,int *flag){       if(offset>=0)       {              if(M-a[offset]>0)              {                     if((M-a[offset])>=a[offset-1])                     {                            flag[offset]=1;                            if(M>=0)                                   process(M-a[offset],a,offset-1,flag);                     }                     else                     {                            if(M>=0)                                   process(M,a,offset-1,flag);                     }              }              else if(M-a[offset]==0)              flag[offset]=1;       }}  int main(void){       int N,M,i,number=0;       int result;       int count=0;       FILE *fp1,*fp2;       char tmp;       int value[MAX];       int option[MAX];       int *p=value;       int *flag=option;       fp1=fopen("input","r");       fscanf(fp1,"%d %d",&M,&N);       printf("M=%d,N=%d/n",M,N);       for(i=0;i<N;i++)       {              fgetc(fp1);              tmp=fgetc(fp1);              value[i]=atoi(&tmp);              printf("value=%d/n",value[i]);       }        process(M,p,N-1,flag);       for(i=0;i<N;i++)       {              if(*flag++==1)              {                     printf("%d",value[i]);                     count+=value[i];                     number++;              }       }        result=(count==M?number:0);        fp2=fopen("output","w");       fprintf(fp2,"%d",result);       fclose(fp1);       fclose(fp2);       return 0;}

2. Questions and answers for the Tsinghua computer postgraduate examination (I did this afternoon and was depressed... It takes too long)

I. Input: two rows

Line 1: M and N

Row 2: x

M and n are decimal numbers. m and n are both in the range of [2-36], X is an M-base number, and X is in [1-2*10 ^ 19].

Output: one row

The first line: Now you are required to convert the M-in-number X to the n-in-number output.

Input 1:

16 10

F

Output 1:

15

# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h> # define max 100/* Display Error Message */void printerror (char errno, char * num, int base1) {Switch (errno) {Case 1: printf ("/nerror: Origin Number % s (% d) is valid !!! /N ", num, base1); break; Case 2: printf ("/nerror: Radix (% d) is invalid !!! /N % s/n ", base1," correct: Radix >=2 and Radix <= 36 "); break ;}} /* Number Conversion Function */void transnum (char * num, int base1, int base2) {int Len, K, L, M, J, ibase1, ibase2; long inum = 0; char temp [20]; double r = 0; Len = strlen (Num);/* length of the value */ibase1 = base1; /* number base 1 */If (ibase1 <2) | (ibase1> 36) printerror (2, "", base1);/* Is it valid? */Ibase2 = base2;/* number base 2 */If (ibase2 <2) | (ibase2> 36) printerror (2, "", base2 ); /* Is it valid? */For (j = 0; j <Len; j ++) // This loop is used to convert the number to be converted into decimal {/******* to determine the letter or number, convert to the corresponding number ******************/r = POW (ibase1, len-j-1 ); /* calculate the power index of the number base */If (ibase1 <= 10) L = '9'-(10-ibase1 ); /* calculate the valid number range */else {/* calculate the valid number range */M = 'A' + (ibase1-11); L = '9 ';} if (Num [J]> = 48) & (Num [J] <= L )) /* calculate the decimal value of each digit */k = num [J]-48; else if (ibase1> 10) {/* calculate the decimal value represented by each letter */If (Num [J]> = 'A') & (Num [J] <= m-32 )) k = num [J]-'A' + 10; else if (Num [J]> = 'A') & (Num [J] <= m )) k = num [J]-'A' + 10; else printerror (1, num, base1);} else printerror (1, num, base1 ); /*************************************** * ***********/inum + = K * (INT) r;/* accumulate calculation result */}/* output Conversion Result */printf ("% s (% d) = % s (% d)/n", num, ibase1, ltoa (inum, temp, ibase2), ibase2);}/* Main Program */INT main (void) {char number [Max]; unsigned int M, N; static char num [10]; static in T base1, base2; printf ("m hexadecimal to N hexadecimal, enter M and N:/N"); scanf ("% d", & M, & N); If (M <2 | n <2) {printf ("illegal input! "); Return 1;} getchar (); printf (" Universal hexadecimal conversion program. Enter the number to be converted:/N "); gets (number);/* use the input m base number as a string to receive */strcpy (Num, number ); base1 = m; base2 = N; transnum (Num, base1, base2 );}

2. Enter letters on the keyboard of the mobile phone to plan the time spent

For example, A, B, and C are all on the "1" key. Input a only needs to be pressed once, and input C needs to be pressed three times in a row.

If two consecutive characters are not on the same button, you can press it directly. For example, if ad needs to be pressed twice, Kz needs to press 6

If two consecutive characters are on the same button, it takes some time between the two buttons, for example, AC. After pressing a, it takes a while to press C.

Now it is assumed that a time period is required each time, and the waiting time takes two time periods.

Now, a string of characters is given, and the time it takes needs to be scheduled.

Input 1: Bob

Output 1: 7

Input 2: www

# Include <stdlib. h> # include <stdio. h> # include <string. h> # define Max 100int process (char * STR, int Len) {int flag = 0; int delayflag = 1; int I, j, delay = 0; int length [9] = {0, 3, 3, 3, 3, 3, 4}; int location = 0; int count; int press = Len; char * pstr = STR; int * number; int table [27] [2]; int (* P) [2] = table; number = malloc (sizeof (INT) * (LEN + 1 )); for (I = 0; I <Len; I ++) {number [I] = (* pstr ++)-96 ); // printf ("% d/N", number [I]);} number [I] = number [I-1]; for (I = 0; I <= 26; I ++) // initialize other rows in the table {* (p + I) = I; Switch (I) {Case 3: Case 6: Case 9: case 12: Case 15: Case 19: Case 22: Case 26: * (p + I) + 1) = 1; break; default: * (p + I) + 1) = 0; break;} for (I = 0; I <len-1; I ++) {printf ("number [I] = % d/N", number [I]); printf ("number [I + 1] = % d/N ", number [I + 1]); delayflag = 1; if (number [I]-number [I + 1]) <0) {for (j = number [I]; j <number [I + 1]; j ++) {If (* (p + 1) + J) = 1) delayflag = 0; printf ("delayflag = % d/N", delayflag);} If (delayflag) {delay + = 1 ;}} else if (number [I]-number [I + 1])> 0) {for (j = number [I + 1]; j <number [I]; j ++) {If (* (p + 1) + J) = 1) delayflag = 0;} If (delayflag) {delay + = 1 ;}} else {delay + = 1;} printf ("delay = % d/N", delay);} for (I = 0; I <Len; I ++) {COUNT = 0; j = number [I]; do {If (* (p + J) + 1) = 0) Count + = 1 ;} while (* (p + (J ++) + 1) = 0); printf ("Count = % d", count ); location + = (length [I + 1]-count); printf ("location = % d/N", location);} If (location = 0) return (delay * 2 + location + Len); else return (delay * 2 + location);} int main (void) {int Len; char STR [Max]; char * P = STR; gets (STR); Len = strlen (STR); printf ("% d/N", process (p, Len); Return 0 ;}
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.