实验 7 综合练习 ok

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

#include<stdio.h>
void main()
{
    int i,b=1;
    double s;
    s=0;                      /*给s赋值,函数为连加所以开始的s=0*/
    for(i=1;i<=15;i++)
    {
        s=s+double(i)/double(b);      /*输出结果不为整数,需要用double定义*/
        b=b+2;               /*研究函数得分母每次循环都大2*/
    }
    printf("sum=%f\n",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

#include <stdio.h>
void main( )
{
    int i, j, t, a[10];
    printf("Enter 10 integers: ");
    for(i = 0; i < 10; i++)
        scanf( "%d",&a[i] );   /*输入的a[10]为整数型*/
    for(i = 1; i < 10; i++)
        for( j = 0 ; j < 10 - i ; j++)  /*下标从0开始,因此初始给j赋值为0;一共有10个数,因此j<10-i*/
            if( a[j] < a[j+1] )     /*判断下标为j和j+1的数的大小,若a[j]<a[j+1]则进行交换*/
            {
                t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }
    printf("After sorted: ");
    for(i = 0; i < 10; i++)
        printf("%d ", a[i]);
    printf("\n");
}

 

 

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

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

#include<stdio.h>
#include<math.h>   /*调用数学函数*/
int main(void)
{
    int x;
    double y;
    printf("Enter x:");
    scanf("%d",&x);
    if(x<-2){
        y=x*x;
    }
    else if(x>2){
        y=sqrt(x*x+x+1);    /*调用平方根函数*/
    }
    else {
        y=2+x;
    }
    printf("y=%.2f\n",y);

    return 0;
}

 

 

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

四、编写程序,输入一批学生的成绩,遇0或负数则输入结束,要求统计并输出优秀(大于85)、通过(60~84)和不及格(小于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 score,pass,fail,good;
    pass=0;
    fail=0;
    good=0;
        printf("Enter scores :");
        scanf("%d",&score);
    while (score>0){
        if (score>=85){
        good = good + 1;
    }
    else if (score<60){
        fail = fail + 1;
    }
    else {
        pass = pass + 1;
    }
        scanf("%d",&score);      /*再次输入成绩*/
    }
    printf(">=85:%d\n",good);
    printf("60-84:%d\n",pass);
    printf("<60:%d\n",fail);

    return 0;
}

 

 

 

 

posted @ 2013-10-31 09:41  七颜℡  阅读(212)  评论(1)    收藏  举报