任务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); }
回答:每间隔1000ms,随机打印0~25个空白行和0~80个空白列再打印hi,April~。总共反复操作10次。
任务2:
代码1:
#include <stdio.h> #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=%lld\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可以使局部变量在该函数结束时不会被系统回收空间,而是继续存在并保留原有值。
任务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 n){ long long f; if(n==1) f=1; else f=2*func(n-1)+1; return f; }
截图
任务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 n,int m){ int C; if(m==0||m==n) C=1; else if(m>n) C=0; else C=func(n-1,m)+func(n-1,m-1); return C; }
截图
任务5:
代码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 x,int y){ double ans=1; int i; if(y>=0) for(i=0;i<y;i++) ans=ans*x; else for(i=0;i>y;i--) ans=ans/x; return ans; }
截图
代码2:
#include <stdio.h> #include <math.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 x,int y){ double ans; if(y==0) ans=1; else ans=x*mypow(x,fabs(y)-1); if(y<0) ans=1/ans; return ans; }
截图
任务6:
代码:
#include <stdio.h> #include <stdlib.h> void hanio(unsigned int n,char from,char temp,char to); void moveplate(unsigned int n,char from,char to); int i; int main(){ unsigned int n; while(scanf("%u",&n)!=EOF){ hanio(n,'A','B','C'); printf("一共移动了%d次。\n",i); i=0; } return 0; } void hanio(unsigned int n,char from,char temp,char to){ if(n==1) moveplate(n,from,to); else{ hanio(n-1,from,to,temp); moveplate(n,from,to); hanio(n-1,temp,from,to); } } void moveplate(unsigned int n,char from,char to){ printf("%u:%c-->%c\n",n,from,to); i++; }
截图
任务7:
代码:
#include <stdio.h> #include <math.h> int is_prime(double n); int sovle(int ans); int main(){ int n,ans; while(scanf("%d",&ans)!=EOF){ n=sovle(ans); printf("%d=%d+%d\n",ans,ans-n,n); } return 0; } int sovle(int ans){ double n=ans-2.0; while(is_prime(n)==0||is_prime(ans-n)==0){ n--; } return (int)n; } int is_prime(double n){ int m; for(m=2;;m++){ if(m>sqrt(n)) return 1; else if((int)n%m==0) return 0; } }
截图
任务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; double i; while(s!=0){ if(s%10%2==1) t=t+s%10*pow(10.0,i),i++; s=s/10; } return t; }
截图
标签:return,int,long,实验,func,ans,include From: https://www.cnblogs.com/FC000/p/17273740.html