实验2

任务一:
`#include<stdio.h>

include<stdlib.h>

include<time.h>

define N 5

int mian(){
int number;
int i;
srand(time(0));
for(i=0;i<N;++i){
number = rand()%65+1;
printf("20238331%04d\n",number);

}
return 0;
}
`
问题1:生成1到65的随机数
问题2:生成随机学号

任务二:

include<stdio.h>

include<stdlib.h>

int main()
{
char color;
while(1)
{
while(getchar()!='\n'){
continue;
}

scanf("%c",&color);
switch(color){

case 'r':
printf("stop!\n");
break;
case 'g':
printf("go go go\n");
break;
case 'y':
printf("wait a minute\n");
break;
default:
printf("something must be wrong\n");
break;
}
}
system("pause");
return 0;

}

任务三:
`#include <stdio.h>

include <stdlib.h>

include <time.h>

int main() {

srand((int) time(0));  
int lucky_day = rand() % 31+1;  

int guess_day;
printf("猜猜2024年5月哪一天会是你的lucky day?\n");

for (int i = 1; i <= 3; i++) {  
    
    printf("请输入你猜测的日期(1-31):");  
    scanf("%d", &guess_day);  
    
    if (guess_day == lucky_day) {  
        printf("恭喜!你猜中了,5月%d号是你的lucky day!\n", lucky_day);  
        break;  
    } else {  
       
        if (lucky_day > guess_day) {  
            printf("你猜的日期早了,还有%d天才是你的lucky day。再猜一次吧!\n", lucky_day - guess_day );  
        } else {  
            printf("你猜的日期晚了,还有%d天才是你的lucky day。再猜一次吧!\n", guess_day - lucky_day );  
        }  

	}
	if (i==3 && guess_day != lucky_day){
	printf("很遗憾,你没有在三次内猜中,5月%d号是你的lucky_day\n",lucky_day);
	}
} 

system ("pause");
return 0;  

}

`

任务四:
`#include <stdio.h>

include <stdlib.h>

include <math.h>

int main() {

int b=0;
int n,a;  
while(scanf("%d%d", &n, &a)==2){;  
double s = 0.0;  
for (int i = 1; i <= n; i++) { 
	b = b*10+a;
    s += 1.0*i/b;
}  
printf("S = %.10lf\n", s);  
system("pause");}
return 0;  

}

`

任务五:
`#include <stdio.h>

include <stdlib.h>

int main() {
int i,j;
for (int i = 1; i <= 9; i++) {

    for (int j = 1; j <= i; j++)  
        printf("%d×%d=%d\t", i, j, i * j);  
printf("\n");
}
	system("pause");
return 0;  

}

`

任务六:
`#include <stdio.h>

include <stdlib.h>

int main()

{
int n,a=0;
printf("input n:");
scanf("%d",&n);
for(;n>=1;n=n-1)
{
for(int m=1;m<=a;m=m+1)
{
printf("\t");
}
for(int i=1;i<=2n-1;i=i+1)
{
printf(" O\t");
}
printf("\n");
for(int m=1;m<=a;m=m+1)
{
printf("\t"); }
for(int j=1;j<=2
n-1;j=j+1)
{
printf("\t");}
printf("\n");
for(int m=1;m<=a;m=m+1)
{printf("\t");}
for(int b=1;b<=2*n-1;b=b+1)
{printf("I I\t"); }
printf("\n");
a=a+1;
}
system("pause");
return 0;
}`

posted @ 2024-04-14 23:11  酒庄  阅读(2)  评论(0编辑  收藏  举报