task1
#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_black_line(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”随机出现在屏幕上,每个“hi,April”间间隔随机间隔line-1行及col-1空格
task2-2
#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; }
task2-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使得执行main函数时变量不再被初始化,变量数值得以保留
task3
#include <stdio.h> long long func(int n); int main(){ int n; long long f; printf("请输入一个0~32之间的整数\n"); while(scanf("%d",&n)!=EOF){ f=func(n); printf("n=%d,f=%lld\n",n,f); } return 0; } long long func(int n){ int f; for(int i=1;i<=n;i++){ f=2*f+1; } return f; }
task4
#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){ if (n==m || m==0){return 1;} else if(n<m){return 0;} else return func(n-1,m)+func(n-1,m-1); }
task5
#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; if(y>=0){ for(int i=1;i<=y;i++){ ans=ans*x; } } if(y<0){ for(int i=1;i<=-y;i++){ ans=ans/x; } } return ans; }
task6
#include<stdio.h> void move(char getone,char putone){ printf("%c--->%c\n",getone,putone); } void hanoi(int n,char one,char two,char three){ if(n==1) move(one,three); else{ hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } int main(){ int m,n=1,sum; while(scanf("%d",&m)!=EOF){ hanoi(m,'A','B','C'); for(int i=1;i<=m;i++){ n=2*n; sum=n-1; if(i==m)n=1; } printf("一共移动了%d次\n",sum); }return 0;
task7
#include<stdio.h> #include<stdlib.h> #include<math.h> int is_prime(int n); int main() { int n,p,q,a,b; for(n=4;n<=20;n+=2) {p=1; do{ p++; q=n-p; a=is_prime(p); b=is_prime(q); } while(a*b==0); printf("%d=%d+%d\n",n,p,q); } return 0; } int is_prime(int n){ int k; for(k=2;k<=sqrt(n);k++) if(n%k==0) { return 0; } return 1; }
task8
#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){ long g=0,l=0;int t; while(s!=0){ t=s %10; s=s/10; if(t%2!=0){ l=l*10+t; } } while(l!=0){ t=l%10; l=l/10; g=g*10+t; } return g; }
标签:main,int,long,实验,func,printf,include From: https://www.cnblogs.com/ghytf/p/17288043.html