首页 > 其他分享 >实验2

实验2

时间:2024-04-14 23:34:59浏览次数:28  
标签:include int break ++ 实验 printf main

task1.c

#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:得到1~65之间的一个随机数

问题2:一次输出5个学号

task2.c

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

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

}

 

task3.c

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

int main()
{
    int lucky_day;
    int guess;
    int cnt;
    
    lucky_day = rand()%31+1;
    for(cnt = 0;cnt<3;++cnt){
    scanf("%d",&guess);
        
        if(guess == lucky_day)
            printf("哇,猜中了!\n");
        else if(guess < lucky_day)
            printf("你猜的日期早了,你的lucky_day还没到呢\n");
            else
            printf("你猜的日期晚了,你的lucky_day在前面哦\n");
            
        
                
    }
      if(guess != lucky_day)
      printf("次数用完了,偷偷告诉你,五月你的lucky_day是:%d\n",lucky_day);  
        
        
        return 0;
}

task4.c

#include<stdio.h>
int main(){
    int a,n ,i;
    
    double s;
    
    while(scanf("%d%d",&a,&n) != EOF){
    int e=a;
    s = 0.0;

    
    for(i=1;i<=n;i++)
    {
    s+=(double)i/e;    
    e = e*10 +a;
}
    printf("n = %d,a = %d,s = %lf",n,a,s);
    
}
    
    
    return 0;
}

task5.c

#include<stdio.h>
int main()
{
    int i,j;
    for(i+1;i<=9;i++)
    {
        for(j=1;j<=i;j++)
        printf("%d*%d=%d\t",j,i,j*i);
        
        printf("\n");
    }
    return 0;
}

task6.c

#include <stdlib.h>

int main() {
    int n, i, k, j;
    printf("input n:");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < i; j++)
        {
            printf("\t");
        }
        for (k = 2 * (n-i) - 1; k>0; k--)
        {
            printf(" O \t");
        }
        printf("\n");

            for (j = 0; j < i; j++)
            {
                printf("\t");
        }
        for (k = 2 * (n-i) - 1; k>0; k--) {
            printf("<H>\t");
        }
        printf("\n");

        for (j = 0; j < i; j++)
        {
            printf("\t");
        }
        for (k = 2 * (n - i) - 1; k > 0; k--)
        {
            printf("I I\t");
        }
        printf("\n");
    }
    return 0;
}

实验总结:

1、学会使用switch多分支语句

2、知道如何得到一个随机数

标签:include,int,break,++,实验,printf,main
From: https://www.cnblogs.com/wangmiao-wm/p/18121244

相关文章

  • 实验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;......
  • 实验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;//生成一个1-65之间的随机数printf("20238331%04d\n",......