实验三
1.test1
1 #include <stdlib.h> 2 #include <time.h> 3 #include <windows.h> 4 #define N 80 5 #include<stdio.h> 6 7 void print_text(int line,int col,char text[]); 8 void print_spaces(int n); 9 void print_blank_lines(int n); 10 11 int main(){ 12 int line,col,i; 13 char text[N]="hi,november~"; 14 15 srand(time(0)); 16 17 for(i=1;i<=10;++i){ 18 line=rand()%25; 19 col=rand()%80; 20 print_text(line,col,text); 21 Sleep(1000);//暂停1000ms 22 } 23 return 0; 24 } 25 26 //打印空格 27 void print_spaces(int n){ 28 int i; 29 30 for(i=1;i<=n;++i) 31 printf(" "); 32 } 33 34 //打印n行空白行 35 void print_blank_lines(int n){ 36 int i; 37 38 for(i=1;i<=n;++i) 39 printf("\n"); 40 } 41 42 //在第line行第col列打印一段文本 43 void print_text(int line,int col,char text[]) { 44 print_blank_lines(line-1); 45 print_spaces(col-1); 46 printf("%s",text); //在第line行,第col列输出text中的字符串 47 }
2.test2
1 #include<stdio.h> 2 long long fac(int n); 3 4 int main(){ 5 int i,n; 6 7 printf("Enter n: "); 8 scanf("%d",&n); 9 10 for(i=1;i<=n;++i){ 11 printf("%d!=%11d\n",i,fac(i)); 12 } 13 14 return 0; 15 } 16 17 long long fac(int n){ 18 static long long p=1; 19 20 p=p*n; 21 return p; 22 }
3.test3
1 #include <stdio.h> 2 long long func(int n); 3 4 5 6 int main() 7 { 8 int n; 9 long long f; 10 11 while(scanf("%d",&n)!=EOF){ 12 f=func(n); 13 printf("n=%d,f=%lld\n",n,f); 14 } 15 16 return 0; 17 } 18 19 long long func(int n){ 20 long long f=1; 21 if(n==0) 22 f=0; 23 else 24 f=func(n-1)*2+1; 25 26 return f; 27 28 }
4.test4
1 #include<stdio.h> 2 #include<stdlib.h> 3 int func(int n,int m); 4 5 6 int main() 7 { 8 int n,m; 9 10 while(scanf("%d%d",&n,&m)!=EOF) 11 printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m)); 12 13 return 0; 14 } 15 //迭代 16 int func(int n,int m){ 17 int up=1,down=1; 18 int q; 19 int i=1,j=1; 20 if(n<m){ 21 q=0; 22 } 23 else if(n>=m){ 24 25 for(i=n-m+1;i<=n;++i){ 26 up*=i; 27 } 28 for(j=1;j<=m;++j){ 29 down*=j; 30 } 31 } 32 q=up/down; 33 return q; 34 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 int func(int n,int m); 4 5 int main() 6 { 7 int n,m; 8 9 while(scanf("%d%d",&n,&m)!=EOF) 10 printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m)); 11 12 return 0; 13 } 14 //递归 15 int func(int n,int m){ 16 int f; 17 if(n<m) { 18 f=0; 19 } 20 21 else if(m==1) { 22 f=n; 23 } 24 25 else if(m==0) { 26 f=1; 27 } 28 29 else{ 30 f=func(n-1,m)+func(n-1,m-1); 31 32 } 33 return f; 34 35 36 37 }
5.test5
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 void hanoi(unsigned int n,char from,char temp,char to); 5 void moveplate(unsigned int n,char from,char to); 6 7 8 9 int main(){ 10 unsigned int n; 11 int w; 12 while(scanf("%u",&n)!=EOF){ 13 hanoi(n,'A','B','C'); 14 w=pow(2,n)-1; 15 printf("一共移动了%d次",w); 16 } 17 system("pause"); 18 return 0; 19 } 20 21 void hanoi(unsigned int n,char from,char temp,char to){ 22 if(n==1) 23 moveplate(n,from,to); 24 else{ 25 hanoi(n-1,from,to,temp); 26 moveplate(n,from,to); 27 hanoi(n-1,temp,from,to); 28 } 29 } 30 void moveplate(unsigned int n,char from,char to){ 31 printf("%u:%c-->%c\n",n,from,to); 32 33 }
6.test6
1 #include<stdio.h> 2 #include<math.h> 3 long func(long s); 4 5 int main(){ 6 7 long s, t; 8 9 printf("Enter a number:"); 10 while(scanf("%ld",&s)!=EOF){ 11 t = func(s); 12 printf("new numberm is:%ld\n\n",t); 13 printf("Enter a number:"); 14 15 16 } 17 18 return 0; 19 } 20 long func(long s){ 21 int n,m; 22 long long k=0,d=0; 23 24 while(s>0){ 25 m=s%10; 26 if(m%2==0) 27 printf(" "); 28 else 29 k=k*10+m; 30 s=s/10; 31 32 } 33 while(k>0){ 34 n=k%10; 35 d=d*10+n; 36 k=k/10; 37 38 39 40 } 41 return d; 42 43 }