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_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); // 暂停1000ms } 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); // 打印(line-1)行空行 print_spaces(col-1); // 打印(col-1)列空格 printf("%s", text); // 在第line行、col列输出text中字符串 }
task2
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! = %lld\n", i, fac(i)); return 0; } // 函数定义 long long fac(int n) { static long long p = 1; p = p * n; printf("p = %d\n",p); return p; }
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; }
task3
#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 result; if(n==m||m==0) result = 1; else if(m>n) result = 0; else result = func(n-1,m-1) + func(n-1,m); return result; }
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){ int result; if(n==m||m==0) result = 1; else if(m>n) result = 0; else result = func(n-1,m-1) + func(n-1,m); return result; }
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.0; int i=1; if(y>=0){ for(;i<=y;i++) ans = ans * x; } else{ for(;i<=-y;i++) ans = ans / x; } return ans; }
#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; if(y==0) ans = 1; else if(y>0){ if(y==1) ans = x; else ans = x * mypow(x,y-1);} else ans = 1 / mypow(x,-y); return ans; }
task6
#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 time; int mian(){ unsigned int n; while (scanf("%u",&n) !=EOF){//输入盘子数目 time = 0; hanoi(n,'A','B','C'); printf("一共移动了%d次",time); } system("pause"); 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);//将n-1个盘子从A以C为中心转移到B上 moveplate(n,from,to); //将盘子n从A移到C上 hanoi(n-1,temp,from,to);//将n-1个盘子从B以A为中转移到C上 } } void moveplate(unsigned int n,char from,char to){ printf("%u:%c-->%c\n",n,from,to); time++; }
task7
#include<stdio.h> #include<math.h> #include<stdlib.h> int is_prime(int n); int main(){ int i,p,q,n,p_flag,q_flag; while (scanf("%d",&n)!=EOF){ if((n%2==1)||n<4){ printf("数据输入错误\n"); exit(0); } do{ p=p+1; q=n-p; p_flag=is_prime(p); q_flag=is_prime(q); } while(p_flag*q_flag==0); printf("%d=%d+%d\n",n,p,q); } return 0; } int is_prime(int n){ int i,m; m = sqrt(1.0 * n); for(i=2;i<=m;i++) if(n%i==0) break ; if(i>m&&n!=1) return 1; else return 0; }
task8
#include <stdio.h> #include <math.h> #include <stdlib.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 i=0,m,result=0,p; for(;s>1;){ p = (s%10)%2; if(p!=0){ m = s%10; result = result + pow(10,i)*m; i++; } s = s/10; } return result; }
标签:return,int,实验,result,func,ans,include From: https://www.cnblogs.com/Tzaa/p/17291206.html