首页 > 其他分享 >实验二

实验二

时间:2024-04-14 23:44:05浏览次数:26  
标签:guess int lucky 实验 printf include day

1.实验任务1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N  5
int main()
{
    int number;
    int i;
    srand(time(0));
    for(i=0;i<N;++i)
    {
        number=rand()%65+1;
        printf("20238331%04d\n",number);
    }
    system("pause");
    return 0;
}

问题1:输出当前系统时间作为随机种子

问题2:生成一个随机学号并进行输出

2.实验任务2

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char color;
    printf("输入信号灯颜色:");
    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;
        }
    }
    system("pause");
    return 0;
}

3.实验任务3

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

int main() {
    int lucky_day, guess, attempts = 3;
    time_t t;
    srand((unsigned) time(&t));
    lucky_day = rand() % 31 + 1;

    printf("猜猜看5月的哪一天是你的幸运日?你有三次机会。\n");

    while (attempts > 0) 
    {
        printf("请输入一个1到31之间的日期:");
        scanf("%d", &guess);
        if (guess < 1 || guess > 31) {
            printf("输入的日期无效,请输入一个1到31之间的整数。\n");
        } else if (guess < lucky_day) {
            printf("你猜的日期早了,你的幸运日在后面哦。\n");
            attempts--;
        } else if (guess > lucky_day) {
            printf("你猜的日期晚了,你的幸运日在前面哦。\n");
            attempts--;
        } else {
            printf("恭喜你,猜中了!5月的第%d天是你的幸运日。\n", lucky_day);
            break;
        }
    }
    if (attempts == 0 && guess != lucky_day) {
        printf("很遗憾,你没有猜中。你在5月的幸运日是第%d天。\n", lucky_day);
    }
    system("pause");
    return 0;
}

 4.实验任务4

#include<stdio.h>
#include<stdlib.h>
double dou(int x,int y)
{
    int sum=0;
    for(int i=1;i<=x;i++)
        sum=sum*10+y;
    return sum;
}
int main()
{
    int a,n;
    double s;
    while(scanf("%d%d",&n,&a)!=EOF)
    {
        s=0;
        for(int i=1;i<=n;i++)
            s=s+i/dou(i,a);
        printf("n = %d,a = %d,s = %6lf",n,a,s);
    }
    system("pause");
    return 0;
}

 

5.实验任务5

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

int main()
{
    int n;
    printf("请输入乘法表的最大行数:");
    scanf("%d", &n);

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

6.实验任务6

 

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n;
    printf("input n:");
    scanf("%d",&n);
    for(int i=n;i>=1;i--)
    {
        for(int j=n-i;j>0;j--)
            printf("      ");
        for(int j=1;j<=2*i-1;j++)
            printf(" O    ");
        printf("\n");
        for(int j=n-i;j>0;j--)
            printf("      ");
        for(int j=1;j<=2*i-1;j++)
            printf("<H>   ");
        printf("\n");
        for(int j=n-i;j>0;j--)
            printf("      ");
        for(int j=1;j<=2*i-1;j++)
            printf("I I   ");
        printf("\n");
    }
    system("pause");
    return 0;
}

 

标签:guess,int,lucky,实验,printf,include,day
From: https://www.cnblogs.com/940804telck/p/18134922

相关文章

  • 实验2
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%65+1;printf("20238331%......
  • 实验2
    实验一#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%65+1;printf("20238331%04d\n",number......
  • 实验2
    任务一:`#include<stdio.h>include<stdlib.h>include<time.h>defineN5intmian(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%65+1;printf("20238331%04d\n",number);}return0;}`问题1:生成1到65的随机数问题2......
  • 原型设计实验
    原型设计工具比较分析:墨刀:适用领域:墨刀适用于快速原型设计和交互设计,特别适合移动应用和Web应用的设计。优点:界面简洁清晰,易于上手,适合初学者和团队合作。支持多种交互模式,如链接、滑动、弹出框等,可以较好地模拟用户操作流程。支持团队协作,可以多人同时编辑同一项目,方便团队......
  • 实验2_C语言分支与循环基础应用编程
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){ intnumber; inti; srand(time(0)); for(i=0;i<N;++i){ number=rand()%65+1; printf("20238331%04d\n",number); } return0;}......
  • 实验2
    实验1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%65+1;//控制随机数的范围从1~66printf("2023......
  • 实验2
    TASK1点击查看代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){ intnumber; inti; srand(time(0)); for(i=0;i<N;++i){ number=rand()%65+1; printf("2023833%04d\n",number); } ret......
  • 实验二
    task1点击查看代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%65+1;printf("202......
  • 实验二。
    include<stdio.h>include<stdlib.h>include<time.h>defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%65+1;printf("20238331%04d\n",number);}return0;}问题一:一到六......
  • 实验2 C语言分支与循环基础应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){ intnumber; inti; srand(time(0)); for(i=0;i<N;++i) { number=rand()%65+1; printf("20238331%04d\n",number); } return0;......