实验三
#include <stdio.h> #include <stdlib.h> #include <windows.h> void printCharMan(int line,int col); void printSpaces(int n); int main(){ int line,col; for(line=5,col=5;col<=60;col++){ printCharMan(line,col); Sleep(50); system("cls"); } } void printSpaces(int n){ int i; for(i=1;i<=n;i++) printf(" "); } void printCharMan(int line,int col){ int i,j; for(i=1;i<=line-1;i++) printf("\n"); printSpaces(col-1); printf(" o \n"); printSpaces(col-1); printf("<H>\n"); printSpaces(col-1); printf("I I\n"); }
#include<stdio.h> #include<math.h> int isPrime(int n); int main(){ int i,s,a=0; for(i=101;i<=200;i++){ s=isPrime(i); if(s==i){ printf("%5d",s); a=a+1; } if(s!=0&&a%5==0) printf("\n"); } return 0; } int isPrime(int n){ int i,m; m=sqrt(n); for(i=2;i<=m;i++) if(n%i==0)break; if(i>m&&n>1) return n; else return 0; }
#include <stdio.h> double fun(int n); int main(){ int n; double s; printf("Enter n(1~10):"); while(scanf("%d",&n)!=EOF){ s=fun(n); printf("n=%d,s=%f\n\n",n,s); printf("Enter n(1-10):"); } return 0; } double fun(int n){ double s; int i,f=1 ,z=1; for(i=1,s=0;i<=n;) { f=f*i; s=s+z*1/f; z=-z; i++; } return s; }
#include<math.h> #include<stdio.h> void solve(double a,double b,double c); int main(){ double a,b,c; printf("Enter a,b,c:"); while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){ solve(a,b,c); printf("Enter a,b,c:"); } return 0; } void solve(double a,double b,double c){ double x1,x2; double delta,real,imag; if(a==0) printf("not quadratic equation.\n"); else{ delta=b*b-4*a*c; if(delta>=0){ x1=(-b+sqrt(delta))/(2*a); x2=(-b-sqrt(delta))/(2*a); printf("x1=%.2f,x2=%.2f\n",x1,x2); } else{ real=-b/(2*a); imag=sqrt(-delta)/(2*a); printf("x1=%.2f+%.2fi,x2=%.2f-%.2fi\n",real,imag,real,imag); } } }
#include<stdio.h> int main(){ long s,t,n; while(printf("Enter a number: "),scanf("%ld",&s)!=EOF) { int m; t=0; n=1; while(s!=0) { m=s%10; if(m%2) { t=t+m*n; n=n*10; } s=s/10; } printf("New number is: %ld\n",t); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int x, n, i; printf("猜猜2021年5月哪一天会是你的 Lucky Day\n开始了,你有三次机会,猜吧(1~31):"); srand(time(0)); // 以当前系统时间作为随机种子 x =1+ rand() % 31; // 生成一个1~31之间的随机整数 for (i=1;i<=3;){ scanf("%d",&n); if (n==x){ printf("猜对啦!\n"); break; } else if (n<x){ printf("你猜的日期早了,Luck Day还没到呢。\n"); } else { printf("你猜的日期晚了,Luck Day悄悄溜到前面啦。\n"); } i++; printf("再猜(1~31):"); if (i>3) printf("次数用完了,偷偷告诉你:5月,你的Luck Day是%d号。\n",x); } return 0; }
#include<stdlib.h> #include<time.h> #define N 5 int main(){ int x,n; srand(time(0)); for(n=1;n<=N;n++){ x=rand()%100; printf("%3d",x); } printf("\n"); return 0; }