ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验2

2022-04-18 22:34:03  阅读:34  来源: 互联网

标签:main int scanf ++ 实验 printf include


任务1

//这个程序随机生成2018-2021年级的学号
#include <stdio.h>
 #include <stdlib.h>
 #include <time.h>

 #define N 5

 int main() 
 {
    int grade, number, i;
    
    srand(time(0));
    
    for (i = 0; i < N; ++i)
    {
        grade = rand() % 4 + 2018;//生成2018-2021之间的年份
        number = rand() % 999 + 1;//生成1-999之间的学号
        printf("%d8330%04d\n", grade, number);
    }
       
             return 0;
    
 }

 任务2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 int main() 
 {
    int m, n, i;
    srand(time(0));
    m = rand() % 30 + 1;
    printf("猜猜2022年4月的哪一天是你的幸运日\n");
    printf("\n你有三次机会\n");
    printf("开猜!:");
    for (i = 1; i <= 3; i++) 
    {
        scanf("%d", &n);
        if (m > n)
        {
             printf("早了\n");
             printf("再猜:");
        }
        else if (m < n)
        {
             printf("迟了\n");
             printf("再猜:");
         }       
        else 
        {
             printf("回答正确 \n");
             break;
        }
    }
    if (m != n)
       {
          printf("机会已尽,正确答案是%d号", m);
       }
       return 0;
 }

任务3

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    char light;
    while (scanf("%c", &light) != EOF) 
    {
        getchar();
        if (light == 'r')
        {
            printf("stop!\n");
        }
        else if (light == 'g')
        {
            printf("go go go\n");
        }
        else if (light == 'y')
        {
            printf("wait a minute\n");
        }
        else
        {
            printf("invaild input!\n");
        }
    }
    return 0;
}

任务4

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    int n, a;
    double s, temp;
    while (scanf("%d %d", &n, &a) != EOF)
    {
        temp = 0,s = 0;
        for (int i = 1; i <= n; i++)
        {
            temp = temp * 10 + a;
            s += 1.0 * i / temp;
        }
        printf("n=%d,a=%d,s=%f\n", n, a, s);
    }
    return 0;
}

任务5

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 int main() 
 {
     int i, j, n;
     while (scanf("%d", &n) != EOF)
     {
         printf("生成一个n阶乘法表:\nn=");
         scanf("%d", &n);
         for (i = 1; i <= n; i++)
         {
             for (j = 1; j <= i; j++)
             {
                 printf("%d×%d=%-3d", j, i, i * j);
             }
             printf("\n");
         }
     }
         return 0;
}

任务6

任务6.1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 int main() 
 {
     int i, n;
     while (scanf("%d", &n) != EOF)
     {
         printf("生成n个小人:\nn=");
         scanf("%d", &n);
         for (i = 1; i <= n; i++)
         {
             printf(" O\n");
             printf("<H>\n");
             printf("I I\n");
             printf("\n");
         }
     }
         return 0;
}

任务6.2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 int main() 
 {
     int i, n;
     while (scanf("%d", &n) != EOF)
     {
         printf("生成n个小人:\nn=");
         scanf("%d", &n);
         for (int i = 0; i < n; i++) 
         {
             printf("  O  ");
         }
         printf("\n");
         for (int i = 0; i < n; i++) 
         {
             printf(" <H> ");
         }
         printf("\n");
         for (int i = 0; i < n; i++) 
         {
             printf(" I I ");
         }
         printf("\n");
     }
         return 0;
}

任务6.3

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

实验总结

1.任务5-任务6.2使用了类似思路,偷懒成功

2.但任务6.3... 只能说债终究是要还的

标签:main,int,scanf,++,实验,printf,include
来源: https://www.cnblogs.com/Zxc123jkl/p/16162632.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有