实验三
task.1
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> #define N 80 void print_text(int line,int col,char text[]); void print_spaces(int n); void print_blank_lines(int n); int main(){ int line,col,i; char text[N]="hi,April~"; srand(time(0)); for(i=1;i<=10;++i){ line=rand()%25; col=rand()%80; print_text(line,col,text); Sleep(1000); } return 0; } void print_spaces(int n){ int i; for(i==1;i<=n;++i) printf(" "); } void print_blank_lines(int n){ int i; for(i=1;i<=n;++i) printf("\n"); } void print_text(int line,int col,char text[]){ print_blank_lines(line-1); print_spaces(col-1); printf("%s",text); }
在十秒内每秒在一个随机位置生成“hi,April~"
task 2
1.原
#include<stdio.h> long long fac(int n); int main(){ int i,n; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;++i) printf("%d!=%lld\n",i,fac(i)); return 0; } long long fac(int n){ static long long p=1; p=p*n; return p; }
1.添加后
#include<stdio.h> long long fac(int n); int main(){ int i,n; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;++i) printf("%d!=%lld\n",i,fac(i)); return 0; } long long fac(int n){ static long long p=1; printf("p=5%d\n",p); p=p*n; return p; }
2.
#include<stdio.h> int func(int,int); int main(){ int k=4,m=1,p1,p2; p1=func(k,m); p2=func(k,m); printf("%d,%d\n",p1,p2); return 0; } int func(int a,int b){ static int m=0,i=2; i+=m+1; m=i+a+b; return m; }
一致
static变量可以修饰局部变量使其被调用时只被初始化一次,之后的计算在上一次运算得到的结果上继续进行。
task.3
#include<stdio.h> long long func(int n); int main(){ int n; long long f; while(scanf("%d",&n)!=EOF){ f=func(n); printf("n=%d,f=%lld\n",n,f); } return 0; } long long func(int f){ long long result; if(f==1) return 1; else { result=2*(func(f-1)+1); return result-1; } }
task.4
#include<stdio.h> int func(int n,int m); int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m)); } return 0; } int func(int a,int b){ int result; if(a<b) return 0; if(a==b||b==0) return 1; if(b==1) return a; else result=func(a-1,b)+func(a-1,b-1); return result; }
task5_1
#include<stdio.h> double mypow(int x,int y); int main(){ int x,y; double ans; while(scanf("%d%d",&x,&y)!=EOF){ ans=mypow(x,y); printf("%d的%d次方:%g\n\n",x,y,ans); } return 0; } double mypow(int a,int b){ double ans=1.0; int i; if(b>=0){ for(i=1;i<=b;i++){ ans=ans*a; } return ans; } else{ for(i=-1;i>=b;i--){ ans=ans/a; } return ans; } }
task5_2
#include<stdio.h> double mypow(int x,int y); int main(){ int x,y; double ans; while(scanf("%d%d",&x,&y)!=EOF){ ans=mypow(x,y); printf("%d的%d次方:%g\n\n",x,y,ans); } return 0; } double mypow(int a,int b){ double ans;if(b==0) return 1; else if(b>0){ ans=a*mypow(a,b-1); return ans; } else{ ans=(1.0/a)*mypow(a,b+1); return ans; } }
task.6
#include<stdio.h> #include<stdlib.h> void hanoi(unsigned int n,char from,char temp,char to); void moveplate(unsigned int n,char from,char to); int flag=0; int main() { unsigned int n; while(scanf("%u",&n)!=EOF){ hanoi(n,'A','B','C'); printf("\n"); printf("一共移动了%d次\n",flag); printf("\n");} system("pause"); return 0; } void hanoi(unsigned int n,char from,char temp,char to) { if(n==1) { moveplate(n,from,to); } else { hanoi(n-1,from,to,temp); moveplate(n,from,to); flag++; hanoi(n-1,from,to,temp); } } void moveplate(unsigned int n,char from,char to) { printf("%u:%c-->%c\n",n,from,to); }
task.7
#include<stdio.h> int is_prime(int n); int main(){ int a,n=2,m,flag1=0,flag2=1; while(scanf("%d",&a)!=EOF){ while(flag1!=1||flag2!=1) { m=a-n; if(m==2&&flag2==1) break; else{ flag1=is_prime(m); flag2=is_prime(n); n++; } } flag2=is_prime(n); if(flag2==0) n--; printf("%d=%d+%d\n",a,n,m); } return 0; } int is_prime(int n){ int t,flag=1; for(t=2;t<n-1;t++){ if(n%t==0) flag=0; break; } return flag; }
task.8
#include<stdio.h> #include<math.h> long func(long s); int main(){ long s,t; printf("Enter a number:"); while(scanf("%ld",&s)!=EOF){ t=func(s); printf("new number is:%ld\n\n",t); printf("Enter a number:"); } return 0; } long func(long s) { int t=0,m,n=0,i,j=1,count=0; while(s!=0) { m=s%10; s=s/10; if(m%2==1) { t=t+m*pow(10,n); n++; } } return t; }