#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;//2018~2021年 number = rand() % 999 + 1;//末尾三位是1~999 printf("%d8330%04d\n", grade, number); } return 0; }
程序功能:在2018~2021级同学中随机抽取5人
#include<stdio.h> #include<stdlib.h> #include<time.h> int main(){ int x,n,ans; srand(time(0)); x=rand()%30+1; printf("猜猜2022年4月哪一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); for(n=1;n<=3;n++){ scanf("%d",&ans); if(ans==x){ printf("哇,猜中了"); break;} else if(ans<x){ printf("你猜的日期早了,你的lucky day还没到呢。"); } else if(ans>x){ printf("你猜的日期晚了,你的lucky day已经过了。"); } if(n<=2){ printf("\n再猜(1~30):"); } } if(n==4){ printf("\n次数已经用完了,偷偷告诉你你的lucky day是4月%d号",x); } return 0; }
#include <stdio.h> int main() { char x; while(scanf("%c",&x)!= EOF) { if (x == 'r') { printf("stop!\n"); } else if (x == 'g') { printf("go go go\n"); } else if (x == 'y') { printf("wait a minute\n"); } else printf("something must be wrong...\n"); getchar(); } return 0; }
#include <stdio.h> #include <math.h> int main() { int i,n,a; printf("n=?,a=?\n"); while(scanf("%d%d",&n,&a)!= EOF) { float d=0.0,s=0.0; for(i=1;i<=n;i++) { d+=a*pow(10,i-1); s+=i/d; } printf("n = %d, a = %d, s= %f\n",n,a,s); printf("n=?,a=?\n"); } return 0;
#include <stdio.h> int main() { int column,line; for(column=1;column<=9;column++) { for(line=1;line<=column;line++) { printf("%dx%d=%2d ",line,column,line*column); } printf("\n"); } return 0; }
6.1
#include <stdio.h> int main() { int j,i; printf("input n:"); scanf("%d",&i); for(j=1;j<=i;j++) { printf(" o \n"); printf("<H>\n"); printf("I I\n\n"); } return 0; }
6.2
#include <stdio.h> int main() { int i,j; printf("input n:"); scanf("%d",&i); for(j=1;j<=i;j++) { printf(" o "); } printf("\n"); for(j=1;j<=i;j++) { printf("<H> "); } printf("\n"); for(j=1;j<=i;j++) { printf("I I "); } printf("\n"); return 0; }
6.3
#include<stdio.h> int main() { int N,m,n,a; printf("input n:"); scanf("%d", &N); for (m = 1; m <= N; m++) { for (n = 0; n <= m; n++) printf("\t"); for (a = 0; a < N * 2 + 1 - m * 2; a++) printf(" o \t"); printf("\n"); for (n = 0; n <= m; n++) printf("\t"); for (a = 0; a < N * 2 + 1 - m * 2; a++) printf("<H>\t"); printf("\n"); for (n = 0; n <= m; n++) printf("\t"); for (a = 0; a < N * 2 + 1 - m * 2; a++) printf("I I\t"); printf("\n"); } return 0; }