实验2

1、实验任务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:解释line18代码 number = rand() % (R2 - R1 + 1) + R1; 实现的功能

答:生成从R1到R2的随机数。

 

问题2:这个程序的功能是什么?

答:生成学号。

 

2、实验任务2

源代码 及 运行结果截图:

#include <stdio.h>

int main()
{
    double x,y;
    char c1,c2,c3;
    int a1,a2,a3;
    scanf("%d%d%d", &a1, &a2, &a3);
    printf("a1 = %d, a2 = %d , a3 = %d\n", a1,a2,a3);

    getchar();


    scanf("%c%c%c", &c1, &c2 , &c3);
    printf("c1 = %c, c2 = %c , c3 = %c\n", c1,c2,c3);

    scanf("%lf,%lf", &x, &y);
    printf("x = %lf , y = %lf\n",x,y);

    system("pause");
    return 0;
}

 

 

3、实验任务3

源代码 及 运行结果截图:

#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;
}

 

#include <stdio.h>
#include<math.h>

int main()
{
double c ,f;

while(scanf("%lf",&c)!= EOF){
    
f = 9/5*c + 32;
printf("摄氏度c = %.2f时,华氏度f = %.2f\n",c,f);
printf("\n");
}

    system("pause");
    return 0;
}

 

4、实验任务4

源代码 及 运行结果截图:

#include <stdio.h>

int main()
{
    char x;
    
    while(scanf("%c",&x)!= EOF){
    getchar();
        switch(x){
    case 'r':printf("stop!"); break;
    case 'y':printf("wait a minute"); break;
    case 'g':printf("go go go");break;
     default:printf("something must be wrong...");
    }
        printf("\n");
    }

    system("pause");
    return 0;
}

 

5、实验任务5

源代码 及 运行结果截图:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int x,i,n;

    srand(time(0));

    n = rand()%(30-1+1)+1;

    i=0;

    printf("猜猜2023年4月哪一天会是你的lucky day\n 开始咯,你有三次机会,猜吧(1~30):");

    for(i=0;i<3;i++)
    { 
        scanf("%d",&x);
        
       if (x == n) {
                printf("哇,猜中了:)\n");
                break;
            }
       if (x < n)
                printf("你猜的日期早了,你的lucky day还没到呢\n");
       if (x > n)
                printf("你猜的日期晚了,你的lucky day已经过啦\n");
    }
        printf("次数用完啦,偷偷告诉你:4月,你的lucky day是%d号", n);

    system("pause");
    return 0;
}

 

6、实验任务6

源代码 及 运行结果截图:

#include <stdio.h>

int main()
{
    int column,line,value;
    column=1;
    line=1;
    value = column * line;
    for(column=1;column<=9;column++){

        for (line=1; column >= line;line++) {
            value = column * line;
            printf("%d*%d=%d  ", column, line, value);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

 

7、实验任务7

源代码 及 运行结果截图:

#include <stdio.h>
int main()
{
 int n,i,j,k;
 printf("input n:");
 scanf("%d", &n);
 for (i = 1; i <= n; i++) {

        for (k = 1; k <= 3; k++) {

            for (j = 1; j <= i - 1; j++) {
                printf("     ");
            }

            for (j = 1; j <= 2 * (n - i) + 1; j++) {
                if(k==1)
                    printf(" O   ");
                if(k==2)
                    printf("<H>  ");
                if(k==3)
                    printf("I I  ");
            }
            printf("\n");
        }
    }
 system("pause");
    return 0;
}

用文字和公式描述出字符小人阵列的规律。

指出: 当输入为n时: 第i行,需要打印多少个字符小人    (打印 2*(n-i)+1个小人)

第i行,前面需要打印多少空白(需要使用多少个 \t )        (打印 i-1个)

 

posted on 2023-03-19 21:47  是我,  阅读(18)  评论(0编辑  收藏  举报