zoukankan      html  css  js  c++  java
  • 实验7

    实验 7 综合练习

    实验目的:巩固分支结构、循环结构、函数和数组的使用方法。
    习题

    一、填空:阅读下列程序说明和程序,在可选答案中,挑选一个正确答案。填补(1) (2) (3) (4)处空白,并注释说明为什么。
    程序说明
    求 1 + 2/3 + 3/5 + 4/7 + 5/9 + … 的前15项之和。
    运行示例:
    sum = 8.667936
    程序如下:

     

     1 #include <stdio.h>

     2 void main( )

     3 {

     4     int i, b = 1;

     5     double s;

     6     (1)  A  ;(根据i=1b=1,联系s=s+一个不为零得数,所以令s初始值为0)

     7     for(i = 1; i <= 15; i++)

     8     {

     9         s = s +  B   (2)    (因为sdouble型,所以I b应该一样。选B则第一个数为1/1=1

    10         (3)  B (根据式中分母1357的序列选B

    11     }

    12     printf(    (4)  C  , s);(因为s是double型,需要用%f,选C)

    13 }

     

    【供选择的答案】
    (1)   A、s = 0       B、s = 1       C、s = -1       D、s = 2
    (2)   A、i/b                           B、double(i)/double(b)
           C、i/2*i-1                     D、(double)i/(double)b
    (3)   A、;                             B、b = 2 * i – 1;
           C、b = 1.0 * b;             D、b = b + 2;
    (4)   A、"sum = %d "          B、"s = %c "
           C、"sum = %f "           D、"s = %s "

    ---------------------------------题目分割线-----------------------------------

    二、填空:阅读下列程序说明和程序,在可选答案中,挑选一个正确答案。填补(1) (2) (3) (4)处空白,并注释说明为什么。。
    【程序说明】
    输入10个整数,将它们从大到小排序后输出。
    运行示例:
    Enter 10 integers: 1 4 -9 99 100 87 0 6 5 34
    After sorted: 100 99 87 34 6 5 4 1 0 -9
    程序如下:

     

     1 #include <stdio.h>

     2 void main( )

     3 {

     4     int i, j, t, a[10];

     5     printf("Enter 10 integers: ");

     6     for(i = 0; i < 10; i++)

     7         scanf( (1) D);(因为变量定义是int,所以定义一个D中的数组)

     8     for(i = 1; i < 10; i++)

     9         for( (2) B; (3) C; j++)(根据i=1,j从1开始,递增到10-i。)

    10             if( (4)C )(这里比较大小,根据下文知是当a[j] < a[j+1],将其两个值调换。选C)

    11             {

    12                 t = a[j];

    13                 a[j] = a[j+1];

    14                 a[j+1] = t;

    15             }

    16     printf("After sorted: ");

    17     for(i = 0; i < 10; i++)

    18         printf("%d ", a[i]);

    19     printf(" ");

    20 }

     

    【供选择的答案】

    (1) A、"%f", a[i]          B、"%lf", &a[i]           C、"%s", a              D、"%d", &a[i] 
    (2) A、j = 0                B、j = 1                    C、j = i                   D、j = i - 1
    (3) A、j > i                 B、j < 9 - i                C、j < 10 - i            D、j > i - 1
    (4) A、a[i-1] < a[i]      B、a[j+1] < a[j+2]     C、a[j] < a[j+1]       D、a[i] < a[j]

    ---------------------------------题目分割线-----------------------------------

    三、编程,输入x后,根据下式计算并输出y值。

     

    /*编程,输入x后,根据下式计算并输出y值*/
    #include<stdio.h>
    #include<math.h> /*定义函数开根号*/
    int main(void)
    {
        double x,y;
        printf("Enter x:
    ");
        scanf("%lf",&x);
    
        if(x<-2){
            y=x*x;
        }
        else if(x>=-2 && x<=2){
            y=2+x;
        }
        else{
            y=sqrt(x*x+x+1);
    }
    printf("y=%.2f
    ",y);
    
    return 0;
    }

    ---------------------------------题目分割线-----------------------------------

    四、编写程序,输入一批学生的成绩,遇0或负数则输入结束,要求统计并输出优秀(大于85)、通过(6084)和不及格(小于60)的学生人数。

    运行示例:

    Enter scores: 88 71 68 70 59 81 91 42 66 77 83 0

    >=85:2

    60-84:7

    <60   : 2

    #include<stdio.h>
    int main(void)
    {
        int good,pass,count;              /*count是不及格人数*/
        double grade;
    
        printf("Enter grade:");
        scanf("%lf",&grade);
    
        good=0;
        pass=0;
        count=0;
    
        while(grade>0){         /*只有在成绩大于0执行程序*/
            if(grade>85){
                good++;
            }
            else if((grade<84) && (grade>64)){
                pass++;
            }
            else{
                count++;
            }
            scanf("%lf",&grade);         /*继续循环*/
        }
        printf("good is %d
    ",good);
        printf("pass is %d
    ",pass);
        printf("count is %d
    ",count);
    
        return 0;
    }
  • 相关阅读:
    编译nginx时openssl报错的解决方案
    编译nginx时make报错[-Werror=implicit-fallthrough=]的解决方案
    centos8下编译安装tomcat
    解决centos安装不上apache:No match for argument: httpd
    编译安装apache
    编译apache出现gcc: error: /usr/lib/rpm/redhat/redhat-hardened-ld: No such file or directory
    配置与管理FTP服务器
    2018icpc南京现场赛-I Magic Potion(最大流)
    Codeforces 1062B Math(质因数分解)
    BZOJ 1009 [HNOI2008]GT考试(矩阵快速幂优化DP+KMP)
  • 原文地址:https://www.cnblogs.com/jianghaoyu0129/p/3398516.html
Copyright © 2011-2022 走看看