# wust2013届推免生复试机试代码

1.忘记是什么题目了，简单的迭代，类似“兔子的繁殖”、“斐波那契数列”之类的~

 1 #include<stdio.h>
2
3 #define N 100000
4 int arr[N];
5 void Init();
6 void main()
7 {
8     int i;
9     Init();
10     while(scanf("%d",&i)!=EOF)
11     {
12         printf("%d ",arr[i]);
13     }
14 }
15 void Init()
16 {
17     int i;
18     arr[0] = 1;
19     arr[1] = 1;
20     arr[2] = 2;
21     for(i=3 ; i<=100000 ; i++)
22     {
23         arr[i] = arr[i-1]+arr[i-2]+arr[i-3];
24     }
25 }

2.约瑟夫环问题

 1 #include<stdio.h>
2 #define N 1000
3 int arr[N];
4 int Next(int k,int n);
5 void main()
6 {
7     int n,k,m,i,cnt,sum;
8     char ch;
9     while(scanf("%d%d%d",&n,&k,&m)!=EOF)
10     {
11         ch = getchar();
12         //Init
13         k-=1;
14         for(i=0 ; i<n ; i++)    arr[i]=1;//1 means the elem in queue
15         //Deal
16         sum=1;
17         while(sum<n)
18         {
19             cnt=0;
20             while(cnt<m)
21             {
22                 cnt++;
23                 //Get Next
24                 if(cnt!=m)        k=Next(k,n);
25             }
26             printf("%d",k+1);
27             sum++;
28             if(sum == n)
29             {
30                 printf("\n");
31                 break;
32             }
33             else
34                 printf(",");
35
36             arr[k]=0;
37             k=Next(k,n);
38         }
39     }
40 }
41 int Next(int k,int n)
42 {
43     int i=k;
44     do{
45         i=(i+1)%n;
46     }while(arr[i]==0);
47     return i;
48 }

3.输入英文数字表达式，输出对应结果：

 1 #include<stdio.h>
2 #include<string.h>
3
4 char strNo[10][10]={
5     "zero","one","two","three","four","five","six","seven","eight","nine"
6 };
7
8 void main()
9 {
10     int i,a=1,b=1,t;
11     char temp[10];
12     while(a!=0 || b!=0)
13     {
14         t=0;
15         while(scanf("%s",temp),temp[0] != '=')
16         {
17             if(temp[0] == '+')
18             {
19                 a=t;
20                 t=0;
21             }
22             else
23             {
24                 for(i=0;i<10;i++)
25                 {
26                     if(strcmp(temp,strNo[i]) == 0)
27                     {
28                         t*=10;
29                         t+=i;
30                         break;
31                     }
32                 }
33             }
34         }
35         b=t;
36         if(a!=0 || b!=0)
37             printf("%d\n",a+b);
38     }
39 }

4.统计每个分数线都有多少人

 1 #include<stdio.h>
2 #include<string.h>
3 int arr[101];
4 void main()
5 {
6     int i,n,idx,sc;
7     while(scanf("%d",&n),n)
8     {
9         memset(arr,0,sizeof(arr));
10         for(i=0;i<n;i++)
11         {
12             scanf("%d",&idx);
13             arr[idx]++;
14         }
15         scanf("%d",&sc);
16         printf("%d\n",arr[sc]);
17     }
18
19 }

5.把100-999之间不含9的素数写到result.txt文件中【采用筛选法打表】

 1 #include<stdio.h>
2 #include<string.h>
3 int arr[1001];
4 void Init();
5 int IsLegal(int i);
6 int main()
7 {
8     int i,cnt;
9     FILE *fp;
10     if((fp=fopen("result.txt","w")) == NULL)
11     {
12         printf("OPEN FILE ERROR!\n");
13         return -1;
14     }
15     Init();
16     cnt=0;
17     for(i=100;i<=1000;i++)
18     {
19         if(arr[i]==0 && IsLegal(i))
20         {
21             cnt++;
22             fprintf(fp,"%-5d",i);
23         }
24     }
25     fprintf(fp,"\n一共有%d个。\n",cnt);
26     fclose(fp);
27     return 0;
28 }
29 void Init()
30 {
31     int i,j;
32     memset(arr,0,sizeof(arr));
33     for(i=2;i<=35;i++)
34     {
35         for(j=i+i;j<=1000;j+=i)
36             arr[j]=1;
37     }
38 }
39 int IsLegal(int i)
40 {
41     int n=i;
42     while(n!=0)
43     {
44         if(n%10==9)
45             return 0;
46         n/=10;
47     }
48     return 1;
49 }

+ 订阅