实验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);
    } 
    
    return 0;
    
}

程序运行截图

问题1:line18的功能是在R1=586和R2=701之间随机取数

问题2:这个程序的功能是在固定学号区间内随机取N个学号

 

实验任务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);
    
    scanf(" %c %c %c", &c1, &c2, &c3);  //字符变量间应用空格隔开
    printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);
    
    scanf("%lf %lf", &x, &y);  //浮点数应用lf表示长范围,且二者之间应用空格隔开
    printf("x = %lf, y = %lf", x, y);
    
    return 0;
}

程序运行截图

 

实验任务3

task3-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");
    }
    
    return 0;
 } 

程序运行截图

task3-3

程序源码

//F=9/5*C+32
#include <stdio.h>
#include <math.h>

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

程序运行截图

 

实验任务4

程序源码

#include <stdio.h>

int main()
{
    char color;
    
    while(scanf("%c", &color) != EOF)
    {
        getchar();
        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;
        }
    }
    
    return 0;
}

程序运行截图

 

实验任务5

程序源码

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

int main()
{
    int day, n, i, flag;
    
    printf("猜猜2023年4月哪一天会是你的lucky day\n");
    srand(time(0));
    day = rand()%(30-1+1)+1;
    
    printf("开始喽,你有三次机会,猜吧(1~30):  ", n);
    scanf("%d", &n);
    
    for(i=1;i<=2;i++){
        flag=0;
        
        if(n==day){
            printf("哇!猜中了\n");
            flag=1; break;
        }
        else if(n>day){
            printf("你猜的日期晚了,你的lucky day已经过啦\n");
            printf("再猜(1~30):  ", n);    
            scanf("%d", &n);
        }
        else{
            printf("你猜的日期早了,你的lucky day还没到呢\n");
            printf("再猜(1~30):  ", n);    
            scanf("%d", &n);
        }    
    }
    printf("\n");
    
    if(flag==0)
    printf("次数用完啦。偷偷告诉你:4月,你的lucky day是%d号", day);
    
    return 0;
 } 

程序运行截图

 

实验任务6

程序源码

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

int main()
{
    int i, j, a;
    for(i=1;i<=9;i++){
        for(j=1;j<=i;j++){
            a = i*j;
            printf("%d*%d=%d ", i, j ,a);
        }
    printf("\n");
    }
    
    return 0;
}

程序运行截图

 

实验任务7

程序源码

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

int main()
{
    int i, j, line;
    scanf("%d", &line);
    
    for(j=1; j<=line; j++){
        for(i=1; i<=(j-1); i++)
        printf("\t");
    
        for(i=1; i<=2*(line-j)+1; i++)
        printf(" o\t");
        printf("\n");
    
        for(i=1; i<=(j-1); i++)
        printf("\t");
    
        for(i=1; i<=2*(line-j)+1; i++)
        printf("<H>\t");
        printf("\n");
    
        for(i=1; i<=(j-1); i++)
        printf("\t");
    
        for(i=1; i<=2*(line-j)+1; i++)
        printf("I I \t");
        printf("\n\n");
    }
    
    return 0;

}

程序运行截图

问题:当输入n时,第i行需要打印2(n+1-i)-1个小人;第i行需要打印i-1个空白

posted @ 2023-03-22 23:25  邱奕萱  阅读(10)  评论(0编辑  收藏  举报