task1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <windows.h> 5 #define N 80 6 void print_text(int line, int col, char text[]); // 函数声明 7 void print_spaces(int n); // 函数声明 8 void print_blank_lines(int n); // 函数声明 9 int main() { 10 int line, col, i; 11 char text[N] = "hi, November~"; 12 srand(time(0)); // 以当前系统时间作为随机种子 13 for(i = 1; i <= 10; ++i) { 14 line = rand() % 25; 15 col = rand() % 80; 16 print_text(line, col, text); 17 Sleep(1000); // 暂停1000ms 18 } 19 return 0; 20 } 21 // 打印n个空格 22 void print_spaces(int n) { 23 int i; 24 for(i = 1; i <= n; ++i) 25 printf(" "); 26 } 27 // 打印n行空白行 28 void print_blank_lines(int n) { 29 int i; 30 for(i = 1; i <= n; ++i) 31 printf("\n"); 32 } 33 // 在第line行第col列打印一段文本 34 void print_text(int line, int col, char text[]) { 35 print_blank_lines(line-1); // 打印(line-1)行空行 36 print_spaces(col-1); // 打印(col-1)列空格 37 printf("%s", text); // 在第line行、col列输出text中字符串 38 }
功能:随时随地printf(hi,November)
Task2
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 // 函数定义 12 long long fac(int n) { 13 static long long p = 1; 14 p = p * n; 15 return p; 16 }
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 // 函数定义 12 long long fac(int n) { 13 static long long p = 1; 14 printf("p = %11d\n",p); 15 p = p * n; 16 return p; 17 }
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 // 函数定义 11 int func(int a, int b) { 12 static int m = 0, i = 2; 13 i += m + 1; 14 m = i + a + b; 15 return m; 16 }
运行结果与理论一致
static 在一个函数内只存在于第一次赋值。
task3
1 #include <stdio.h> 2 long long func(int n); // 函数声明 3 int main() { 4 int n; 5 long long f; 6 while (scanf("%d", &n) != EOF) { 7 f = func(n); // 函数调用 8 printf("n = %d, f = %lld\n", n, f); 9 } 10 return 0; 11 } 12 long long func(int n){ 13 long long s; 14 if(n==1){ 15 s=1; 16 return s; 17 } 18 else{ 19 s=(func(n-1)+1)*2-1; 20 return s; 21 } 22 }
Task 4
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() { 4 int n, m; 5 while(scanf("%d%d", &n, &m) != EOF) 6 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 7 return 0; 8 } 9 int func(int n,int m){ 10 int i,up=1,down=1,s; 11 if(m>n){ 12 s=0; 13 } 14 else{ 15 for(i=n;i>=n-m+1;--i){ 16 up*=i; 17 } 18 for(i=1;i<=m;++i){ 19 down*=i; 20 } 21 s=up/down; 22 } 23 return s; 24 }
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() { 4 int n, m; 5 while(scanf("%d%d", &n, &m) != EOF) 6 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 7 return 0; 8 } 9 int func(int n,int m){ 10 int s; 11 if(m>n){ 12 return 0; 13 } 14 else if(m==0||n==1&&m==1){ 15 return 1; 16 } 17 else{ 18 s=func(n-1,m-1)+func(n-1,m); 19 return s; 20 } 21 }
task5
1 #include<stdio.h> 2 #include<stdlib.h> 3 void hanoi(unsigned int n,char from,char temp,char to); 4 void moveplate(unsigned int n,char from,char to); 5 static int count=0; 6 int main(){ 7 unsigned int n,m; 8 while(scanf("%u",&n)!=EOF){ 9 hanoi(n,'A','B','C'); 10 printf("\n一共移动了%d次\n",count); 11 count=0; 12 } 13 system("pause"); 14 return 0; 15 } 16 void hanoi(unsigned int n,char from,char temp,char to){ 17 if(n==1){ 18 moveplate(n,from,to); 19 } 20 else{ 21 hanoi(n-1,from,to,temp); 22 moveplate(n,from,to); 23 hanoi(n-1,temp,from,to); 24 } 25 } 26 void moveplate(unsigned int n,char from,char to){ 27 printf("%u:%c-->%c\n",n,from,to); 28 count++; 29 }
task6
1 #include <stdio.h> 2 #include <math.h> 3 long func(long s); // 函数声明 4 int main() { 5 long s, t; 6 printf("Enter a number: "); 7 while (scanf("%ld", &s) != EOF) { 8 t = func(s); // 函数调用 9 printf("new number is: %ld\n\n", t); 10 printf("Enter a number: "); 11 } 12 return 0; 13 } 14 long func(long s){ 15 long m=0,n,i=0; 16 while(s!=0){ 17 n=s%10; 18 s=s/10; 19 if(n%2!=0){ 20 m=n*pow(10,i)+m; 21 ++i; 22 } 23 } 24 return m; 25 }
task 7
1 #include<stdio.h> 2 int main(){ 3 int i,j,k,m,n,count,flag; 4 int arr[10]; 5 for(i=1;;++i){ 6 flag=1; 7 count=0; 8 m=i*i; 9 n=i*i*i; 10 while(m!=0){ 11 m=m/10; 12 count++; 13 } 14 while(n!=0){ 15 n=n/10; 16 count++; 17 } 18 if(count==10){ 19 m=i*i; 20 n=i*i*i; 21 j=0; 22 while(m!=0){ 23 arr[j++]=m%10; 24 m=m/10; 25 } 26 while(n!=0){ 27 arr[j++]=n%10; 28 n=n/10; 29 } 30 for(j=0;j<10;j++){ 31 for(k=j+1;k<10;k++){ 32 if(arr[j]==arr[k]){ 33 flag=0; 34 break; 35 } 36 } 37 } 38 if(flag==1){ 39 printf("%d",i); 40 break; 41 } 42 } 43 } 44 return 0; 45 }
标签:10,return,int,long,实验,func,include From: https://www.cnblogs.com/nuist6/p/17798681.html