北京航空航天大学计算机系考研复试上机真题及答案---2014

第一题,阶乘数。spa

输入一个正整数,输出时,先输出这个数自己,跟着一个逗号,再输出这个数的各位数字的阶乘和,等号,code

阶乘和的计算结果,并判断阶乘和是否等于原数,若是相等输出Yes,不然输出No。题目说明输入的正整数blog

以及其各位阶乘和都不会超出int型的表示范围。input


输入样例1:io

       145class


输出样例1:方法

       145,1!+4!+5!=145im

       Yes数据


输入样例2:di

       1400


输出样例2:

       1400,1!+4!+0!+0!=27

       No

 

第二题,五子棋。

输入一个19*19的矩阵,只包含数字0、一、2,表示两人下五子棋的棋牌状态,一、2分别表示两人的棋子,0表示空格。

要求判断当前状态下是否有人获胜(横向、竖向或者斜线方向连成5个同色棋子)。题目说明输入样例保证每条线上至多

只有连续5个同色棋子,而且保证至多只有1人获胜。若是有人获胜,输出获胜者(1或2)加一个冒号,接着输出获胜的

五连珠的第一个棋子的坐标,从上到下从左到右序号最小的为第一个,序号从1开始编号。若是无人获胜,输出no。


样例略。

参考答案(我的编写仅用于交流)

第一题

 1 #include"stdio.h"
 2 
 3 
 4 ////shuaishuaizhang
 5 
 6 int factorial(int n){  7     int sum=1;  8     for(int i=2; i<=n; i++){  9         sum = sum*i; 10  } 11     return sum; 12 } 13 
14 void sum(int n){ 15     
16     
17     int a,b = n; 18     int s = 0; 19 
20     int m=10; 21     while(b/m != 0){ 22         m = m*10; 23  } 24     m = m/10; 25 
26     while(m !=0 ){//注意是正序输出非倒序输出
27         a = b/m; 28         printf("%d!",a); 29         b = b - a*m; 30         s = s + factorial(a); 31         m = m/10; 32         if(m != 0){ 33             printf("+"); 34         }else{ 35             printf("=%d\n",s); 36  } 37  } 38     if(n == s){ 39         printf("Yes\n"); 40     }else{ 41         printf("No\n"); 42  } 43     
44 } 45 
46 int main(){ 47     int n; 48     freopen("c:\\input.txt","r",stdin);//输入方式
49     scanf("%d",&n); 50     printf("%d,",n); 51  sum(n); 52     return 0; 53 }

 

第二题

//输入数据不符合题意,但方法正确

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define M 19
 6 #define N 19
 7 
 8 //shuaishuaizhang
 9 
10 void isWin(int a[M][N],int m,int n){ 11     int t1,t2,t3,t4; 12     int i=0,j=0; 13     for(i=0; i<m; i++){ 14         t1 =0,t2 = 0, t3 = 0, t4 = 0; 15         for(j=0; j<n; j++){ 16             if(j+4<19){ 17             t1 = a[i][j] & a[i][j+1] & a[i][j+2] & a[i][j+3] & a[i][j+4]; 18  } 19             if(i+4<19 && j+4<19){ 20             t2 = a[i][j] & a[i+1][j+1] & a[i+2][j+2] & a[i+3][j+3] & a[i+4][j+4]; 21  } 22             if(i+4<19){ 23             t3 = a[i][j] & a[i+1][j] & a[i+2][j] & a[i+3][j] & a[i+4][j]; 24  } 25             if(i+4<19 && j-4>=0){ 26             t4 = a[i][j] & a[i+1][j-1] & a[i+2][j-2] & a[i+3][j-3] & a[i+4][j-4]; 27  } 28 
29             if(t1 !=0 || t2 != 0 || t3!=0 || t4 != 0){ 30                 break; 31  } 32  } 33         if(t1 !=0 || t2 != 0 || t3!=0 || t4 != 0){ 34                 break; 35  } 36  } 37     if(t1 !=0){ 38         printf("%d:%d,%d\n",t1,i+1,j+1); 39  } 40     else if(t2 !=0){ 41         printf("%d:%d,%d\n",t2,i+1,j+1); 42  } 43     else if(t3 !=0){ 44         printf("%d:%d,%d\n",t3,i+1,j+1); 45  } 46     else if(t4 !=0){ 47         printf("%d:%d,%d\n",t4,i+1,j+1); 48     }else{ 49         printf("no\n"); 50  } 51 
52 } 53 
54 int main(){ 55     srand(time(0)); 56     int a[M][N]; 57     for(int i=0; i<M; i++){ 58         for(int j=0; j<N; j++){ 59             a[i][j] = rand()%3; 60             printf("%d ",a[i][j]); 61  } 62         printf("\n"); 63  } 64  isWin(a,M,N); 65     
66     return 0; 67 }
相关文章
相关标签/搜索