实验2
实验1代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5
#define R1 586
#define R2 701
int main()
{
int number;
int i;
srand(time(0));
for(i =0;i<N;++i)
{
number =rand() %(R2-R1+1)+R1;
printf("20228330%04d\n",number);
}
system("pause");
return 0;
}
运行截图:
回答问题:问题1:line 18是为了生成从586~701的随机数
问题2:实现从咱班中(学号尾号在586~701的学生)随机点五名同学的学号
实验2代码:
#include<stdio.h>
int main()
{
double x,y;
char c1, c2, c3;
int a1, a2, a3;
scanf("%d%d%d",&a1, &a2, &a3);//scanf 取地址符无//
printf("a1=%d,a2=%d,a3=%d\n", a1,a2,a3);
scanf("%c%c%c",&c1, &c2, &c3);
printf("c1=%c,c2=%c,c3=%c\n", c1,c2,c3);
scanf("%lf%lf",&x, &y);//对double只能用lf//
printf("x=%lf,y=%lf\n",x,y);//此行同上//
system("pause");
return 0;
}
运行截图:
实验3-2代码:
#include<stdio.h>
#include<math.h>
int main()
{
double x, ans;
while(scanf("%lf", &x)!=EOF)
{
ans = pow(x,365);
printf("%.2f的365次方:%.2f\n",x,ans);
printf("\n");
}
system("pause");
return 0;
}
运行截图:
实验3-3代码:
#include<stdio.h>
#include<math.h>
int main()
{
double c, f;
while(scanf("%lf", &c)!=EOF)
{
f= 9.0/5*c+32;
printf("摄氏度c=%.2f时,华氏度f=%.2f\n",c,f);
printf("\n");
}
system("pause");
return 0;
}
运行截图:
实验4代码:
#include<stdio.h>
int main()
{
char x;
while(scanf("%c",&x)!=EOF)
{ switch(x)
{
case'y':printf("wait a minute");break;
case'g':printf("go go go");break;
case'r':printf("stop!");break;
default:printf("something must be wrong...");
}
printf("\n");
scanf("%c", &x);
}
system("pause");
return 0;
}
运行截图:
实验5代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int number,i,a;
srand(time(0));
number = rand() % 30+ 1;
printf("猜猜2023年4月哪一天会是你的lucky day");
i = 1;
printf("开始喽,你有三次机会,猜吧(1~30):");
scanf("%d", &a);
for (; i < 3; i++)
{
if (a > number)
{
printf("你猜的日期晚了,你的lucky day已经过啦\n");
printf("再猜(1~30):");
scanf_s("%d", &a);
}
else if (a < number)
{
printf("你猜的日期早了,你的lucky day还没到呢\n");
printf("再猜(1~30):");
scanf("%d", &a);
}
else if (a == number)
{
printf("哇~猜中了:-)");
}
}
if (i == 3)
printf("你的四月lucky day 是 第 %d 天", number);
system("pause");
return 0;
}
运行截图:
实验6代码:
#include<stdio.h>
int main()
{
int i=0;
int j=0;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
printf("%d*%d=%2d\t",i,j,i*j);
}
printf("\n");
}
system("pause");
return 0;
}
运行截图:
实验7代码:
#include<stdio.h>
int main()
{
int i, j, n, k,l,m,o,p;
printf("INput a number:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 0; j < i; j++)
{
printf("\t");
}
for (k = i; k < 2 * n - 1 - i; k++)
{
printf(" 0\t");
}
printf("\n");
for ( l= 0; l< i; l++)
{
printf("\t");
}
for (m = i; m < 2 * n - 1 - i;m++)
{
printf("<H>\t");
}
printf("\n");
for (o = 0; o< i; o++)
{
printf("\t");
}
for (p= i; p < 2 * n - 1 - i;p++)
{
printf("I I\t");
}
printf("\n\n");
}
system("pause");
return 0;
}
运行截图:
浙公网安备 33010602011771号