实验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; } 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); }
回答问题:随机在第line行第col列生成“hi,April"十次。
实验二
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!=%lld\n", i,fac(i)); return 0; } long long fac(int n){ static long long p = 1; p = p * n; system("pause"); return p; }
增加后运行截图
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); system("pause"); return 0; } int func(int a,int b){ static int m=0,i=2; i+=m+1; m=i+a+b; return m; }
运行截图
回答问题:static后的那行代码运行时只读一遍,在计算p2时static后的数值不再进入运算,不会更新数值i和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) { if(n==1) return 1.0; if(n!=1) return 2.0*(func(n-1)+1.0)-1; }
运行截图
实验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) {if(m<0||m>n) return 0; if(m==n) return 1; if(m<=n) return func(n-1,m)+func(n-1,m-1); }
运行截图
实验5
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); } system("pause"); return 0; } double mypow(int x,int y) { int i , j; double A=1.0; if(y>=0) {for(i=1;i<=y;i++) A=A*(x*1.0);} else {for(i=-1;i>=y;i--) A=A/(x*1.0);} return A; }
运行截图
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); } system("pause"); return 0; } double mypow(int x,int y) { if(y==0) return 1.0; if(y>0) return x*1.0*mypow(x,y-1); if(y<0) return 1.0/mypow(x,-y); }
运行截图
实验7
运行代码
#include <stdio.h> #include<math.h> int is_prime(int n); int main() { int m,j,k; int count=1; printf("%d = %d + %d\n",4,2,2); for(m=6;m<20;m=m+2) { k=m/2; for(j=3;j<=k;j=j+2) if(is_prime(j)&&is_prime(m-j)) {printf("%d = %d + %d\n",m,j,m-j); count++; break;} } printf("\n"); system("pause"); return 0; } int is_prime(int n) {int i,k; if(n==1) return 0; k=(int)sqrt(n); for(i=2;i<=k;i++) if(n%i==0) return 0; return 1; }
运行截图
实验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 j, count = 0; long n=0; while (s != 0) { j = s % 10; s = s / 10; if (j % 2 != 0) { n += j * pow(10, count); count++; } } return n; }
运行截图
标签:return,int,long,实验,func,printf,include From: https://www.cnblogs.com/cty-ymwx/p/17273745.html