首页 > 其他分享 >实验二

实验二

时间:2023-10-22 14:55:54浏览次数:29  
标签:include int number continue 实验 printf main

task1

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

#define N 5 
#define N1 374
#define N2 465 

int main()
{
    int number;
    int i;
    
    srand( time(0) ); 
    for(i = 0; i < N; ++i) {
        number = rand()%(N2 - N1 + 1) + N1; 
        printf("202383290376%04d\n", number); return 0;
}

问题1:解释line17代码 number = rand()%(N2 - N1 + 1) + N1; 的功能?
答:line17代码的作用是限制随机数的范围为N1~N2,即374~465。

问题2:这个程序的功能是什么?
答:这个程序的功能是每次输出5个最后四位是随机数的数字。

task2

#include<stdio.h>

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

task3

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

int main(int argc, const char *argv[])
{
    srand(time(0));                             //以当前系统时间作为随机种子
    int number = (rand()%30)+1;                     //生成1~30随机数
    int time = 3;                                   //猜的次数
    printf("猜猜2023年11月哪一天是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):  ");
    while(time--)                               //次数减少,到0则为假,循环不执行
    {
        int tar;
        scanf("%d", &tar);
        if(tar > number){                       //判断条件
            printf("你猜的日期晚了,你的lucky day已经过了\n\n");
            if(time > 0)
                printf("再猜(1~30): ");
            continue;                           //continue直接进行下一次循环,之后的不再执行
        }   
        if(tar < number){
            printf("你猜的日期早了,你的lucky day还没到呢\n\n");
            if(time > 0)
                printf("再猜(1~30): ");
            continue;
        }
        if(tar == number){
            printf("哇,猜中了 :-)\n");
            return 0;
        }
    }
    printf("次数用光了。偷偷告诉你:11月,你的lucky day是%d号\n", number);
    
    return 0;
}

task4

#include <stdio.h>
#include <math.h> //pow()鍑芥暟鐨勫ご鏂囦欢

int main()
{
    int n,a,i;
    double s;
    
    
    while(1)
    {
        scanf("%d%d",&n,&a);
        
        s=0;
    
        for(i=1;i<=n;++i)
        {
            s+=(double)i/((pow(10,i)-1)/9*a); 
        }
    
        printf("n = %d,a = %d,s = %lf\n",n,a,s);
    }

    return 0;
}

task5

#include<stdio.h>

int main()
{
    int line,column; 
    for(line=1;line<=9;++line)
    {
       
        for(column=1;column<=line;++column)
        {
            printf("%d*%d = %2d  ",column,line,line*column); 
            if(line==column) 
            {
                printf("\n");
            }
        }
    }

    return 0;
}

task6

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

int main()
{
    printf("input n: ");
    int n;
    scanf("%d", &n);            
    if(n<0)return -1;
    int m = (n*2)-1;                //第一行小人个数
    int time = 0;
    for(; m>=1; m-=2)               //最外层循环
    {
        for(int i=0; i<time; i++)   //空格循环
        {
            printf("        ");     //小人前的空格
        }
        for(int i=0; i<m; i++)      //画小人头 循环
        {
            printf(" O      ");
        }
        printf("\n");
        for(int i=0; i<time; i++)
        {
            printf("        ");
        }
        for(int i=0; i<m; i++)      //画小人身子 循环
        {
            printf("<H>     ");
        }
        printf("\n");
        for(int i=0; i<time; i++)
        {
            printf("        ");
        }
        for(int i=0; i<m; i++)      //画小人脚 循环
        {
            printf("I I     ");
        }
        printf("\n");
        time++;                     //time = time+1;
        
        printf("\n");
    }
    
    
    return 0;
}

 

标签:include,int,number,continue,实验,printf,main
From: https://www.cnblogs.com/20050109dfe/p/17780445.html

相关文章

  • 实验二-类和对象_基础编程2
    task1t.hpp1#pragmaonce2#include<iostream>3#include<string>4usingnamespacestd;56classT{7public:8T(intx=0,inty=0);9T(constT&t);10T(T&&t);11~T();12voidset_m1(intx);13intget......
  • 实验2
    实验任务1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));1516for(i=......
  • 实验课二
    一、task1    1,源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));1415for(i......
  • 实验2 类和对象
    实验任务3Complex.hpp#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;}Complex(constComplex&x){real=x.real......
  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;pr......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));1516......
  • 实验二
    实验任务3:1.complex.hpp1#pragmaonce2#include<iostream>3#include<cmath>4classComplex{public:5Complex(doubler=0,doublei=0){6real=r;imag=i;}7Complex(constComplex&x){8real=x.real;......
  • 实验二
    Task1: 1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));1415for(i=0;i<N;......
  • 实验二
    1.test1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%......