实验二
#include <stdio.h> int main(){ int num; scanf("%d",&num); printf("2049%04d\n", num); scanf("%d", &num); printf("2049%04d\n", num); scanf("%d", &num); printf("2049%04d\n", num); return 0; }
%04d 是输出时留4位不足4位的用0补
#include <stdio.h> int main(){ int num; int n = 1; while(n<4) { scanf("%d",&num); printf("2049%04d\n", num); ++n; } return 0; }
实验任务二
#include <stdio.h> int main() { char ans1, ans2; printf("复习了没?(输入y或Y表示复习了,输入n或N表示没复习):"); ans1 = getchar(); getchar();//吃掉空格键 printf("\n动手敲代码了没?(输入y或Y表示敲了,输入n或N表示木有敲):"); ans2 = getchar(); if(ans1 == 'y'&&ans2 == 'y'||ans1 == 'Y'&&ans2 == 'y' ||ans1 == 'y'&&ans2 =='Y'||ans1 == 'Y'&&ans2 =='Y') printf("\n罗马不是一天建成的:\n"); else printf("\n罗马不是一天毁灭的。。。\n"); return 0; }
实验任务三
#include <stdio.h> int main() { char x; x = getchar(); if(x >= '1' && x <= '9') printf("%c是数字字符\n", x); else if(x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z') printf("%c是英文字母\n", x); else printf("%c是其他字符\n", x); return 0; }
实验任务四
#include <stdio.h> int main() { int a,n; while(scanf("%d%d", &n, &a)!=EOF){ int down = 0 ,up = 0, j; float sum = 0; j = 0; while(j < n) { up++; down = down*10 +a; sum += (float)up/down; j++; } printf("n= %d, a= %d, sum = %f\n",n , a , sum); } return 0; }
实验任务五
#include <stdio.h> #include <stdlib.h> #include <time.h> const int 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; }
实验任务六
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int luck_day,n ,i=0; srand(time(0)); luck_day= rand()%31+1; printf("猜猜2021年12月哪一天会是你的luck day"); printf("开始喽,你有三次机会,猜吧(1~31):"); scanf("%d", &n); while(i<2){ if(n==luck_day){ printf("猜对啦!\n"); break; } else if(n<luck_day){ printf("你猜的日期早了,luck day悄悄溜到后面啦\n再猜(1~31):"); scanf("%d",&n); i++; } else{ printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜(1~31):"); scanf("%d",&n); i++; } } if(i==2){ if(n==luck_day) printf("猜对啦!\n"); else if(n<luck_day) printf("你猜的日期早了,luck day悄悄溜到后面啦\n\n次数用完啦,偷偷告诉你:12月,你的luck day是%d号\n",luck_day); else if(n<luck_day) printf("你猜的日期晚了,luck day悄悄溜到后面啦\n\n次数用完啦,偷偷告诉你:12月,你的luck day是%d号\n",luck_day); } system("pause"); return 0; }
实验任务七
#include <stdio.h> int main() { int n; int i = 0; printf("input n: ", n); scanf("%d",&n); while(i < n) { printf(" o\n"); printf("<H>\n"); printf("I I\n"); printf("\n\n"); i++; } return 0; }
#include <stdio.h> int main() { int n; int i = 0,m = 0,j=0; printf("input n: ", n); scanf("%d",&n); while(i < n) { printf(" o \t"); i++; } printf("\n"); while(m < n){ printf("<H> \t"); m++; } printf("\n"); while(j < n){ printf("I I \t"); j++; } printf("\n"); return 0; }
#include <stdio.h> int main() { int n, j, i, m, q; printf("input n: ", n); scanf("%d", &n); m = n; for(i=1;i<=n;i++) { for(q=0;q<i;q++) printf("\t"); for(j=1;j<=2*m-1;j++) { printf(" O \t"); } printf("\n"); for(q=0;q<i;q++) printf("\t"); for(j=1;j<=2*m-1;j++) { printf("<H>\t"); } printf("\n"); for(q=0;q<i;q++) printf("\t"); for(j=1;j<=2*m-1;j++) { printf("I I\t"); } printf("\n"); m -= 1; } return 0; }