实验3
任务1
源代码
1 #include<stdio.h> 2 #include<time.h> 3 #include<windows.h> 4 #include<stdlib.h> 5 #define N 80 6 void print_line(int n); 7 void print_col(int n); 8 void print_text(int line,int col,char text[]); 9 int main(){ 10 int line,col,i; 11 char text[N]="hi,November~"; 12 srand(time(0)); 13 for(i=1;i<=10;i++){ 14 line=rand()%25; 15 col=rand()%80; 16 print_text(line,col,text); 17 Sleep(1000); 18 } 19 return 0; 20 } 21 void print_line(int n){ 22 int i; 23 for(i=1;i<=n;i++) 24 printf(" "); 25 } 26 void print_col(int n){ 27 int i; 28 for(i=1;i<=n;i++) 29 printf("\n"); 30 } 31 void print_text(int line,int col,char text[]){ 32 print_line(col-1); 33 print_col(line-1); 34 printf("%s",text); 35 }
运行结果
任务2.1
源代码
1 #include<stdio.h> 2 long long fac(int i); 3 int main(){ 4 int i,n; 5 printf("Enter n:",n); 6 scanf("%d",&n); 7 for(i=1;i<=n;i++) 8 printf("%d! = %lld\n",i,fac(i)); 9 } 10 long long fac(int i){ 11 static long long p=1; 12 printf("%lld\n",p); 13 p=p*i; 14 return p; 15 }
运行结果
2.2
源代码
1 #include<stdio.h> 2 int main(){ 3 int k=4,m=1,p1,p2; 4 p1=fac(k,m); 5 p2=fac(k,m); 6 printf("%d %d",p1,p2); 7 return 0; 8 } 9 int fac(int a,int b){ 10 static int i=2,m=0; 11 i+=m+1; 12 m=i+a+b; 13 return m; 14 }
运行结果
任务3
源代码
1 #include <stdio.h> 2 long long func(int n); 3 4 int main() { 5 int n; 6 long long f; 7 8 while (scanf("%d", &n) != EOF) { 9 f = func(n); 10 printf("n = %d, f = %lld\n", n, f); 11 } 12 13 return 0; 14 } 15 long long func(int n){ 16 if(n==0) 17 return 0; 18 else 19 return func(n-1)*2+1; 20 }
运行结果
任务4
源代码
1 #include <stdio.h> 2 int func(int n, int m); 3 4 int main() { 5 int n, m; 6 7 while(scanf("%d%d", &n, &m) != EOF) 8 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 9 10 return 0; 11 } 12 /*int func(int n,int m){ //递归法 13 if(n<m) 14 return 0; 15 if(m==0||n==0) 16 return 1; 17 else 18 return func(n-1,m)+func(n-1,m-1); 19 20 }*/ 21 int func(int n,int m){ //迭代法 22 int i,a=1,b=1,c=1,s=0; 23 if(n<m) 24 return 0; 25 else if(m==0||n==0) 26 return 1; 27 else{ 28 for(i=1;i<=n;i++) 29 a*=i; 30 for(i=1;i<=m;i++) 31 b*=i; 32 for(i=1;i<=n-m;i++) 33 c*=i; 34 s=a/b/c; 35 return s;} 36 37 }
运行结果
任务5
源代码
1 #include<stdio.h> 2 void nannoi(unsigned int n,char from,char temp,char to); 3 void print(unsigned int n,char from,char to); 4 int count(int n); 5 int main(){ 6 unsigned int n,t=0; 7 while(scanf("%d",&n)!=EOF){ 8 nannoi(n,'A','B','C'); 9 t=count(n); 10 printf("一共移动了%u次\n",t);} 11 return 0; 12 } 13 void nannoi(unsigned int n,char from,char temp,char to){ 14 if(n==1) 15 print(n,from,to); 16 else{ 17 nannoi(n-1,from,temp,to); 18 print(n,from,to); 19 nannoi(n-1,temp,to,from); 20 } 21 } 22 void print(unsigned int n,char from,char to){ 23 printf("%u:%c-->%c\n",n,from,to); 24 } 25 int count(int n){ 26 if(n==1) 27 return 1; 28 else 29 return 2*count(n-1)+1; 30 }
运行结果
任务六
源代码
1 #include<stdio.h> 2 #include<math.h> 3 long func(long s); 4 int main(){ 5 long s,t; 6 printf("Enter a number:"); 7 while(scanf("%ld",&s)!=EOF){ 8 t=func(s); 9 printf("new number is: %d\n\n",t); 10 printf("Enter a number: "); 11 } 12 13 return 0; 14 } 15 long func(long s){ 16 int x,t=0; 17 while(s!=0){ 18 x=s%10; 19 s/=10; 20 if(x%2!=0) 21 t=t*10+x; 22 } 23 while(t!=0){ 24 x=t%10; 25 t/=10; 26 s=s*10+x; 27 } 28 return s; 29 }
运行结果