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; } //打印n个空格 void print_spaces(int n){ int i; for(i = 1;i <= n;++i) printf(" "); } //打印n行空白行 void print_blank_lines(int n){ int i; for(i = 1;i <= n;++i) printf("\n"); } //在第line行第col列打印一段文本 void print_text(int line, int col, char text[]){ print_blank_lines(line-1); print_spaces(col-1); printf("%s", text); }
作用:在空(line-1)行,空(col-1)列的一个随机位置打印“hi,April~”这个字符,整个过程持续1000ms
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! =%11d\n", i ,fac(i)); return 0; } long long fac(int n){ static long long p = 1; p=p*n; printf("p = %11d\n" ,p); return p; }
task.2.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=%11d\n", n, f); } return 0; } long long func(int n){ int i,t,m; t=2; m=2; for(i = 1; i < n; i++){ m*=t; } m=m-1; return m; }
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 n, int m){ int ans; if(n==m||m==0) ans=1; else if(n<m) ans=0; else ans=func((n-1),m)+func((n-1),(m-1)); return ans; }
task.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){ int t,i; double n=1; if(x==1){ return 1; } if(y>=0){ for(i = 0; i < y; i++){ t=x; n*=t; } }else{ y=-y; for(i = 0; i < y; i++){ t=x; n=n/t; } } return n; }
task.5.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 x,int y){ double n; if(y == 0){ n=1; }else if(y > 0){ n=x*mypow(x,y-1); } else if(y < 0){ n=mypow(x,y+1)/x; } return n; }
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 cnt=0; int main(){ unsigned int n; while(scanf("%u" , &n) != EOF){ hanoi(n,'A','B','C'); printf("一共移动了%d次\n",cnt); cnt=0; } 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); hanoi(n - 1, temp, from, to); } } void moveplate(unsigned int n, char from, char to){ printf("%u:%c-->%c\n",n,from,to); cnt++; }
task.7
#include<stdio.h> #include<math.h> int is_prime(int); int main(){ int yuanshu; int i; while(scanf("%d", &yuanshu)!=EOF){ for(i = 2;;i++){ if(is_prime(i)==1){ if(is_prime(yuanshu-i)==1){ printf("%d=%d+%d",yuanshu,i,yuanshu-i); break; } } } } return 0; } int is_prime(int x){ int i,m; m=1; for(i = 2;i<=sqrt(x);i += 1){ if(x%i==0){ m=0; return 0; } } return 1; }
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 n=0; int sum=0; while(s!=0){ n=n*10+s%10; s=s/10; } int a; while(n!=0){ a=n%10; if(a%2!=0){ sum=sum*10+a; } n=n/10; } return sum; }
标签:return,int,long,实验,func,printf,include From: https://www.cnblogs.com/autism-lotus1/p/17280449.html