实验3_C语言函数应用编程
1.task_1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <windows.h> 5 #define N 80 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 { 13 int line,col,i; 14 char text[N]="hi,November~"; 15 srand (time(0)); 16 for (i=1;i<=10;++i) 17 { 18 line=rand()%25; 19 col=rand()%80; 20 print_text(line,col,text); 21 Sleep(1000); 22 } 23 return 0; 24 } 25 26 void print_spaces(int n) 27 { 28 int i; 29 for (i=1;i<=n;++i) 30 printf(" "); 31 } 32 33 void print_blank_lines(int n) 34 { 35 int i; 36 for (i=1;i<=n;++i) 37 printf("\n"); 38 } 39 40 void print_text(int line,int col,char text[]) 41 { 42 print_blank_lines(line-1); 43 print_spaces(col-1); 44 printf("%s",text); 45 }



答:下“hi,November~”雨,总计10个
2.task_2
(1)
1 #include <stdio.h> 2 long long fac(int n); 3 4 int main() 5 { 6 int i,n; 7 printf("Enter n:"); 8 scanf("%d",&n); 9 for (i=1;i<=n;++i) 10 printf("%d!=%lld\n",i,fac(i)); 11 return 0; 12 } 13 14 long long fac(int n) 15 { 16 static long long p=1; 17 //printf("p=%lld\n",p); 18 p=p*n; 19 return p; 20 }

(2)
1 #include <stdio.h> 2 int func(int,int); 3 4 int main() 5 { 6 int k=4,m=1,p1,p2; 7 p1=func(k,m); 8 p2=func(k,m); 9 printf("%d,%d\n",p1,p2); 10 return 0; 11 } 12 13 int func(int a,int b) 14 { 15 static int m=0,i=2; 16 i+=m+1; 17 m=i+a+b; 18 return m; 19 }

答:与分析一致。static定义答变量不随计算而改变
3.task_3
1 #include <stdio.h> 2 long long func(int n); 3 4 int main() 5 { 6 int n; 7 long long f; 8 while (scanf("%d",&n)!=EOF) 9 { 10 f=func(n); 11 printf("n=%d,f=%lld\n",n,f); 12 } 13 return 0; 14 } 15 16 long long func(int n) 17 { 18 long long int f=1; 19 int i; 20 for (i=1;i<=n;i++) 21 f=f*2; 22 f=f-1; 23 return f; 24 }

4.task_4
(1)
1 #include <stdio.h> 2 int func(int n,int m); 3 4 int main() 5 { 6 int n,m; 7 while (scanf("%d%d",&n,&m)!=EOF) 8 printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m)); 9 return 0; 10 } 11 12 int func(int n,int m) 13 { 14 int up=1,down=1,i,ans; 15 for (i=n-m+1;i<=n;i++) 16 up=up*i; 17 for (i=1;i<=m;i++) 18 down=down*i; 19 ans=up/down; 20 return ans; 21 }

(2)
1 #include <stdio.h> 2 int func(int n,int m); 3 4 int main() 5 { 6 int n,m; 7 while (scanf("%d%d",&n,&m)!=EOF) 8 printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m)); 9 return 0; 10 } 11 12 int func(int n,int m) 13 { 14 int ans; 15 if (m==0) 16 ans=1; 17 else if (m==1) 18 ans=n; 19 else if (m==n) 20 ans=1; 21 else if (m>n) 22 ans=0; 23 else 24 ans=func(n-1,m)+func(n-1,m-1); 25 return ans; 26 }

5.rask_5
1 #include <stdio.h> 2 #include <stdlib.h> 3 void hanoi(unsigned int n,char from,char temp,char to); 4 void moveplate(unsigned int n,char from,char to); 5 int count; 6 7 int main() 8 { 9 unsigned int n; 10 while(scanf("%u",&n)!=EOF) 11 { 12 count=0; 13 hanoi(n,'A','B','C'); 14 printf("\n"); 15 printf("一共移动了%d次.\n",count); 16 printf("\n"); 17 } 18 return 0; 19 } 20 21 void hanoi(unsigned int n,char from,char temp,char to) 22 { 23 if (n==1) 24 moveplate(n,from,to); 25 else 26 { 27 hanoi(n-1,from,to,temp); 28 moveplate(n,from,to); 29 hanoi(n-1,temp,from,to); 30 } 31 } 32 33 void moveplate(unsigned int n,char from,char to) 34 { 35 printf("%u:%c-->%c\n",n,from,to); 36 count++; 37 }

6.task_6
1 #include <stdio.h> 2 #include <math.h> 3 long func(long s); 4 5 int main() 6 { 7 long s,t; 8 printf("Enter a number:"); 9 while (scanf("%ld",&s)!=EOF) 10 { 11 t=func(s); 12 printf("new number is:%ld\n\n",t); 13 printf("Enter a number:"); 14 } 15 return 0; 16 } 17 18 long func(long s) 19 { 20 long k=0,m,t=0,n; 21 do 22 { 23 m=s%10; 24 if (m%2==1) 25 { 26 k=k*10+m; 27 } 28 s=s/10; 29 }while (s!=0); 30 do 31 { 32 n=k%10; 33 t=t*10+n; 34 k=k/10; 35 }while (k!=0); 36 return t; 37 }

7.task_7
1 #include <stdio.h> 2 #define N 10 3 4 int main() 5 { 6 int a=1,list[N]={0},m,n,i,k,t,f; 7 while (a!=EOF) 8 { 9 i=0,f=1; 10 m=a*a*a,n=a*a; 11 do 12 { 13 list[i]=m%10; 14 m/=10; 15 i++; 16 }while (m!=0); 17 do 18 { 19 list[i]=n%10; 20 n/=10; 21 i++; 22 }while (n!=0); 23 for (k=0;k<=N-2;k++) 24 { 25 for (t=k+1;t<=N-1;t++) 26 { 27 if (list[k]==list[t]) 28 { 29 f=0; 30 break; 31 } 32 } 33 } 34 if (f==1) 35 break; 36 else 37 a++; 38 } 39 printf("%d\n",a); 40 return 0; 41 }


浙公网安备 33010602011771号