1.
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int grade, number; int i; srand(time(0)); // 以当前系统时间作为随机种子 for(i=0; i<N; ++i) { grade = rand()%4 + 2018; number = rand()%999 + 1; printf("%d8330%04d\n", grade, number); } return 0; }
问题1:产生2018到2021的随机数并赋值给grade;
问题2:产生1到999的随机数并赋值给number;
问题3:输出N个随机的学号,其中学号前四位为2018到2021的随机数,表示年级;后三位为1到999的随机数。
2.
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int d,ld,i=3; printf("猜猜2022年4月哪一天是你的幸运日\n开始喽,你有三次机会,猜吧(1-30):"); srand(time(0)); ld=rand()%30+1; while(i) { scanf("%d",&d); if(d==ld) { printf("哇,猜中了"); break; } if(d<ld) { printf("你猜的日期早了,你的lucky day还没到呢\n"); i--; } if(d>ld) { printf("你猜的日期晚了,你的lucky day已经过啦\n"); i--; } } if(i==0) printf("次数用完啦,偷偷告诉你,4月,你的lucky day是%d号",ld); return 0; }
3.
#include<stdio.h> int main() { char color; while(scanf("%c",&color)!=EOF) { if(color=='r') printf("stop!\n"); else if(color=='g') printf("go go go\n"); else if(color=='y') printf("wait a sec\n"); else printf("sth must be wrong\n"); scanf("%c",&color); } return 0; }
4.
#include<stdio.h> double compute_s(int n,int a); int main() { int n,a; while(scanf("%d%d",&n,&a)!=EOF) printf("n=%d,a=%d,s=%lf\n",n,a,compute_s(n,a)); return 0; } double compute_s(int n,int a) { double s=0.0,under=0.0; for(int i=1;i<=n;i++) { under=a+under*10; s+=i/under; } return s; }
5.
#include<stdio.h> void scan_formula(int a,int b); int main() { int i,j; for(i=1;i<=9;i++) { for(j=1;j<=i;j++) scan_formula(j,i); printf("\n"); } return 0; } void scan_formula(int a,int b) { printf("%d×%d=%d ",a,b,a*b); }
6.1
#include<stdio.h> void print_vertical_guys(int a); int main() { int n; printf("input n:"); scanf("%d",&n); print_vertical_guys(n); return 0; } void print_vertical_guys(int a) { for(int i=1;i<=a;i++) printf(" O\n<H>\nI I\n"); }
6.2
#include<stdio.h> void print_horizontal_guys(int b); int main() { int n; printf("input n:"); scanf("%d",&n); print_horizontal_guys(n); return 0; } void print_horizontal_guys(int b) { char guy[3][10]={" O ","<H>","I I"}; for(int i=0;i<3;i++) { for(int j=1;j<=b;j++) printf("%s ",guy[i]); printf("\n"); } }
6.3
#include<stdio.h> void print_horizontal_guys_with_gaps(int c,int d); int main() { int n; printf("input n:"); scanf("%d",&n); for(int i=2*n-1;i>=1;i-=2) print_horizontal_guys_with_gaps(i,n); return 0; } void print_horizontal_guys_with_gaps(int c,int d) { char guy[3][10]={" O ","<H>","I I"}; for(int i=0;i<3;i++) { for(int k=(2*d-1-c)/2;k>0;k--) printf(" "); for(int j=1;j<=c;j++) printf("%s ",guy[i]); for(int k=(2*d-1-c)/2;k>0;k--) printf(" "); printf("\n"); } }