实验任务1
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> #define N 80 void print_text(int line , int co1 ,char text[]); void print_spaces(int n); void print_blank_lines(int n); int main() { int line,co1,i; char text[N] ="hi,November~"; srand(time(0)); for(i = 1;i<=10;i++){ line = rand()%25; co1 = rand()%80; print_text(line,co1,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); }
实验任务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; printf("p = %lld\n", p); p = p * n; return p; }
#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; }
实验任务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 int ans; if(n==1) ans=1; else ans=2*(func(n-1)+1)-1; return ans; }
实验任务4
4.1
#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 x=1; int y=1; int z=1; int i,j,q,ans; ans=0; if(n<m) ans=0; else if(n>=m) { for(i=1;i<=n;i++) { x=x*i; } for(j=1;j<=m;j++) { y=y*j; } for(q=1;q<=(n-m);q++) { z=z*q; } ans=x*1.0/(y*z); } return ans; }
4.2
#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) return 0; else if(n==m) ans = 1; else if(m==1) ans = n; else ans = func(n-1,m)+func(n-1,m-1); return ans; }
实验任务5
#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); long long func(int n); int main() { unsigned int n; while(scanf("%u",&n)!=EOF){ hanoi(n,'A','B','C'); printf("一共移动了%d次\n",func(n)); } system("pause"); return 0; } long long func(int n){ long long int ans; if(n==1) ans=1; else ans=2*(func(n-1)+1)-1; return ans; } 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); }
实验任务6
#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 ans,answ=0; ans=0; while(s*1.0/10!=0) { if((s%10)%2==1) { ans=ans*10+s%10; } s=s/10; } while(ans*1.0/10!=0) { answ=answ*10+ans%10; ans=ans/10; } return answ; }
标签:int,long,char,实验,func,ans,include From: https://www.cnblogs.com/PomPomDyx/p/17802239.html