实验任务1
1 #include<stdio.h> 2 #include<math.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include <windows.h> 6 #define N 80 7 void print_text(int line, int col, char text[]); 8 void print_spaces(int n); 9 void print_blank_lines(int n); 10 int main(){ 11 int line, col, i; 12 char text[N] = "hi, November~"; 13 srand(time(0)); 14 for(i = 1; i <= 10; ++i) { 15 line = rand() % 25; 16 col = rand() % 80; 17 print_text(line, col, text); 18 Sleep(1000); 19 } 20 return 0; 21 } 22 void print_spaces(int n) { 23 int i; 24 for(i = 1; i <= n; ++i) 25 printf(" "); 26 } 27 void print_blank_lines(int n) { 28 int i; 29 for(i = 1; i <= n; ++i) 30 printf("\n"); 31 } 32 33 void print_text(int line, int col, char text[]) { 34 print_blank_lines(line-1); 35 print_spaces(col-1); 36 printf("%s", text); 37 }
结果演示
程序功能:在屏幕上左右随机位置生成“hi,November”
实验任务2(1)
1 #include <stdio.h> 2 long long fac(int n); 3 int main() { 4 int i, n; 5 printf("Enter n: "); 6 scanf("%d", &n); 7 for (i = 1; i <= n; ++i) 8 printf("%d! = %lld\n", i, fac(i)); 9 return 0; 10 } 11 long long fac(int n) { 12 static long long p = 1; 13 printf("p=%lld\n",p); 14 p = p * n; 15 return p; 16 }
结果演示(1)
实验任务2(2)
1 #include <stdio.h> 2 int func(int, int); 3 int main() { 4 int k = 4, m = 1, p1, p2; 5 p1 = func(k, m); 6 p2 = func(k, m); 7 printf("%d, %d\n", p1, p2); 8 return 0; 9 } 10 int func(int a, int b) { 11 static int m = 0, i = 2; 12 i += m + 1; 13 m = i + a + b; 14 return m; 15 }
结果演示(2)
static特性:定义一次后就不会再定义,可以保存上一次的值继续用下去
实验任务3
1 #include <stdio.h> 2 long long func(int n){ 3 long long x=1; 4 for(int i=1;i<=n;i++){ 5 x*=2; 6 } 7 return x-1; 8 } 9 int main() { 10 int n; 11 long long f; 12 while (scanf("%d", &n) != EOF) { 13 f = func(n); 14 printf("n = %d, f = %lld\n", n, f); 15 } 16 return 0; 17 }
结果演示
实验任务4
(1)迭代:
1 #include <stdio.h> 2 int func(int n, int m){ 3 long long x=1; 4 for(int i=n;i>=n-m+1;i--){ 5 x*=i; 6 } 7 for(int i=m;i>=1;i--){ 8 x/=i; 9 } 10 return x; 11 } 12 int main() { 13 int n, m; 14 while(scanf("%d%d", &n, &m) != EOF) 15 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 16 return 0; 17 }
结果演示
递归:
1 #include <stdio.h> 2 int func(int n, int m){ 3 if(m>n){ 4 return 0; 5 }else if(m==n||m==0){ 6 return 1; 7 }else if(m==1){ 8 return n; 9 }else{ 10 return func(n-1,m)+func(n-1,m-1); 11 } 12 } 13 int main() { 14 int n, m; 15 while(scanf("%d%d", &n, &m) != EOF) 16 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 17 return 0; 18 }
结果演示
实验任务5
1 #include<stdio.h> 2 #include<stdlib.h> 3 static int x; 4 void move(unsigned int n,char from,char to){ 5 printf("%u:%c-->%c\n",n,from,to); 6 x++; 7 } 8 void hanoi(unsigned int n,char from,char temp,char to){ 9 if(n==1){ 10 move(n,from,to); 11 }else{ 12 hanoi(n-1,from,to,temp); 13 move(n,from,to); 14 hanoi(n-1,temp,from,to); 15 } 16 } 17 18 int main(){ 19 unsigned int n; 20 while(scanf("%d",&n)!=EOF){ 21 x=0; 22 hanoi(n,'A','B','C'); 23 printf("一共移动了%d次\n",x); 24 } 25 return 0; 26 }
结果演示
实验任务6
1 #include <stdio.h> 2 #include <math.h> 3 long func(long s){ 4 long x=0,t; 5 long m=1; 6 while(s!=0){ 7 t=s%10; 8 if(t%2!=0){ 9 x=x+t*m; 10 m*=10; 11 } 12 s=s/10; 13 } 14 return x; 15 } 16 int main() { 17 long s, t; 18 printf("Enter a number: "); 19 while (scanf("%ld", &s) != EOF) { 20 t = func(s); 21 printf("new number is: %ld\n\n", t); 22 printf("Enter a number: "); 23 } 24 return 0; 25 }
结果演示
实验任务7
1 #include<stdio.h> 2 #include<math.h> 3 static int a[11]; 4 static int n=0; 5 int cun(int x){ 6 if(n==0){ 7 n++; 8 a[n]=x; 9 }else{ 10 int pd=0; 11 for(int i=1;i<=n;i++){ 12 if(a[i]==x){ 13 pd++; 14 break; 15 } 16 } 17 if(pd==0){ 18 n++; 19 a[n]=x; 20 } 21 } 22 } 23 int chai(long long s){ 24 int x; 25 while(s!=0){ 26 x=s%10; 27 cun(x); 28 s=s/10; 29 } 30 } 31 int main(){ 32 long long x2,x3; 33 for(int i=1;;i++){ 34 x2=i*i; 35 x3=i*i*i; 36 chai(x2); 37 chai(x3); 38 if(n==10){ 39 printf("%d",i); 40 break; 41 }else{ 42 n=0; 43 a[11]={0}; 44 } 45 } 46 return 0; 47 }
结果演示
标签:10,return,函数,int,编程,long,C语言,func,include From: https://www.cnblogs.com/Tsuneo/p/17798404.html