选择结构程序设计 习题

作者在 2012-02-24 15:03:31 发布以下内容

4.4 有三个整数a,b,c 由键盘输入,输出其中最大的数

/*
时间:2011年10月17日14:24:37
题目:习题4.4 有三个整数a,b,c 由键盘输入,输出其中最大的数
*/
# include <stdio.h>

int main()
{
    
    int a,b,c;
    int max(int a,int b,int c);
    
    printf("输入三个整数:\n");
    scanf("%d%d%d",&a,&b,&c);
    
    printf("其中最大的是:%d\n",max(a,b,c));
    
    return 0;
}

int max(int a,int b,int c)
{
    int max;
    
    if(a>=b)
    {
        if(a>c)
        {
            max=a;
        }
        else
        {
            max=c;
        }
    }
    else if(b>=c)
    {
        max=b;
    }
    else
    {
        max=c;
    }
    return(max);
}

 

4.5 从键盘输入一个小于1000的正数,要求输出他的平方根(如果平方根不是整数,输出其整数部分)。要求在输入数据后先对其检查是否为小于1000的正数,若不是,则要求重新输入

/*
时间:2011年10月17日14:47:11
题目:从键盘输入一个小于1000的正数,要求输出他的平方根(如果平方根不是整数,输出其整数部分)
要求:要求在输入数据后先对其检查是否为小于1000的正数,若不是,则要求重新输入
*/
# include <stdio.h>
# include <math.h>

int main()
{
    double a;
    
    printf("请输入一个小于1000的正数\n");
    scanf("%lf",&a);
    
    while(a<=0 || a>=1000)
    {
        printf("请重新输入输入\n");
        scanf("%lf",&a);
    }
    
    printf("平方根的整数部分是:%-5.0lf\n",sqrt(a));
    return 0;
}

/*
在VC++6.0中的输出结果为:
————————————
请输入一个小于1000的正数
1001
请重新输入输入
0
请重新输入输入
5
平方根的整数部分是:2
Press any key to continue
————————————
*/

 

4.6 有一个函数:y=xx<1y=2x-11<=x<10y=3x-11 (x>=10).写程序,输入x的值,输出y相应的值

/*
时间:2011年10月17日15:17:52
题目:有一个函数,写程序,输入x的值,输出y相应的值
函数::y=x(x<1)
         =2x-1(1<=x<10)
         =3x-11 (x>=10)
*/
# include <stdio.h>

int main()
{
    double x,y;
    
    printf("输入x的值\n");
    scanf("%lf",&x);
    
    if(x<1)
    {
        y=x;
    }
    else if(1<=x && x<10)
    {
        y=2*x-1;
    }
    else
    {
        y=3*x-11;
    }
    printf("y的值是%lf\n",y);
    
    return 0;
}

/*
在VC++6.0中的输出结果为:
————————————
输入x的值
0
y的值是0.000000
Press any key to continue
输入x的值
1
y的值是1.000000
Press any key to continue
输入x的值
10
y的值是19.000000
Press any key to continue
————————————
*/

 

4.7 有一函数y=-1x<0y=0x=0y=1(x>0)..有人分别编写了以下两个程序,请分析它们是否能实现题目要求。画出流程图

两端程序的运行结果都应该是x>0时,y=1x<0时,y=0x=0时,y=-1,都不能实现题目要求,elae总是与他上面的最近的未配对的if配对,所以题目中的书写格式都不好

(1)段程序实际上的流程是

# include <stdio.h>

int main()
{
    int x,y;
    printf("enter x:");
    scanf("%d",&x);
    y=-1;
    if(x!=0)
        if(x>0)
            y=1;
    else                //这样写格式不好,实际上else是与if(x>0)配对的
        y=0;
    printf("x=%d,y=%d\n",x,y);
    return 0;
}
/*
在VC++6.0中的输出结果为:
————————————
enter x:1
x=1,y=1
Press any key to continue
enter x:0
x=0,y=-1
Press any key to continue
enter x:-1
x=-1,y=0
Press any key to continue
————————————
*/

 

(2)段程序实际上的流程是

# include <stdio.h>

int main()
{
    int x,y;
    printf("enter x:");
    scanf("%d",&x);
    y=0;
    if(x>=0)
        if(x>0) y=1;
    else y=-1;        //这样写格式不好,实际上else是与if(x>0)配对的
    printf("x=%d,y=%d\n",x,y);
    return 0;
}
/*
在VC++6.0中的输出结果为:
————————————
enter x:1
x=1,y=1
Press any key to continue
enter x:0
x=0,y=-1
Press any key to continue
enter x:-1
x=-1,y=0
Press any key to continue
————————————
*/

 

4.8 给出一百分制成绩,要求输出成绩等级’A’’B’’C’’D’’E’90分以上为A80~89B70~79C60~69D60分以下为E

/*
时间:2011年10月18日8:32:38
题目:习题4.8 给出一百分制成绩,要求输出成绩等级'A''B''C''D''E'
备注:90分以上为A,80~89为B,70~79为C,60~69为D,60分以下为E
*/
# include <stdio.h>

int main()
{
    double score;
    
    do
    {
        printf("输入分数:");
        scanf("%lf",&score);
        if(score<0 || score>100)
        {
            printf("请重新");
        }
        else
        {
            break;
        }
    }
    while(1);
    
    switch(int(score)/10)
    {
    case 10:
    case 9:
        printf("等级为A\n");
        break;
    case 8:
        printf("等级为B\n");
        break;
    case 7:
        printf("等级为C\n");
        break;
    case 6:
        printf("等级为D\n");
        break;
    default:
        printf("等级为E\n");
    }
    
    return 0;
}
/*
在VC++6.0中的输出结果为:
————————————
输入分数:101
请重新输入分数:-5
请重新输入分数:100
等级为A
Press any key to continue
输入分数:91.5
等级为A
Press any key to continue
输入分数:88
等级为B
Press any key to continue
输入分数:75
等级为C
Press any key to continue
输入分数:60
等级为D
Press any key to continue
输入分数:59.9
等级为E
Press any key to continue
输入分数:0
等级为E
Press any key to continue
————————————
*/

 

4.9 给一个不多于5位的正整数,要求:

1)求出它是几位数

2)分别输出每一位数字

3)按逆序输出每位数字

/*
时间:2011年10月18日9:19:40
题目:习题4.9 给一个不多于5位的正整数
要求:(1)求出它是几位数
      (2)分别输出每一位数字
      (3)按逆序输出每位数字
*/
# include <stdio.h>

int main()
{
    int i,j,k=1;
    int a[5];
    
    do
    {
        printf("输入一个正整数(1~99999):");
        scanf("%d",&i);
        if (i<=99999 && i>0)
        {
            break;
        }
        else
        {
            printf("输入超出范围,请重新");
        }
    }
    while(1);
    
    for(j=0; j<5; ++j)
    {
        a[j]=i%10;
        i/=10;
        if(i!=0)
        {
            ++k;
        }
    }
    
    printf("这个整数一共有%d位\n",k);
    
    for(i=k-1; i>=0; --i)
    {
        printf("第%d位的数字是%d\n",i+1,a[i]);
    }
    
    printf("这个数的逆序输出是:");
    for(i=0; i<k; ++i)
    {
        printf("%d",a[i]);
    }
    printf("\n");
    
    return 0;
}
/*
在VC++6.0中的输出结果为:
————————————
输入一个正整数(1~99999):0
输入超出范围,请重新输入一个正整数(1~99999):100000
输入超出范围,请重新输入一个正整数(1~99999):12345
这个整数一共有5位
第5位的数字是1
第4位的数字是2
第3位的数字是3
第2位的数字是4
第1位的数字是5
这个数的逆序输出是:54321
Press any key to continue
输入一个正整数(1~99999):1234
这个整数一共有4位
第4位的数字是1
第3位的数字是2
第2位的数字是3
第1位的数字是4
这个数的逆序输出是:4321
Press any key to continue
————————————
*/

 

4.10 企业发放的奖金根据利润提成,从键盘输入当月利润I,求应发奖金总数。要求

1)用if语句编程序

/*
时间:2011年10月18日11:20:53
题目:习题4.10 企业发放的奖金根据利润提成,从键盘输入当月利润I,求应发奖金总数。
要求:用if语句编程序
*/
# include <stdio.h>

int main()
{
    double profit,bonus=0;
    
    printf("输入当月利润I:");
    scanf("%lf",&profit);
    
    if(profit>1000000)
    {
        bonus = 1.0/100*(profit-1000000);
        profit = 1000000;
    }
    if(profit<=1000000 && profit>600000)
    {
        bonus += 1.5/100*(profit-600000);
        profit = 600000;
    }
    if(profit<=600000 && profit>400000)
    {
        bonus += 3.0/100*(profit-400000);
        profit = 400000;
    }
    if(profit<=400000 && profit>200000)
    {
        bonus += 5.0/100*(profit-200000);
        profit = 200000;
    }
    if(profit<=200000 && profit>100000)
    {
        bonus += 7.5/100*(profit-100000);
        profit = 100000;
    }
    if(profit<=100000 && profit>=0)
    {
        bonus += 10.0/100*profit;
    }

    printf("可获得奖金总数为%lf\n",bonus);
    
    return 0;
}
/*
在VC++6.0中的输出结果为:
————————————
输入当月利润I:100000
可获得奖金总数为10000.000000
Press any key to continue
输入当月利润I:200000
可获得奖金总数为17500.000000
Press any key to continue
输入当月利润I:400000
可获得奖金总数为27500.000000
Press any key to continue
输入当月利润I:600000
可获得奖金总数为33500.000000
Press any key to continue
输入当月利润I:1000000
可获得奖金总数为39500.000000
Press any key to continue
输入当月利润I:2000000
可获得奖金总数为49500.000000
Press any key to continue
————————————
*/

 

2)用switch编程序

/*
时间:2011年10月18日11:20:53
题目:习题4.10 企业发放的奖金根据利润提成,从键盘输入当月利润I,求应发奖金总数。
要求:用switch语句编程序
*/
# include <stdio.h>

int main()
{
    double profit,bonus=0;
    
    printf("输入当月利润I:");
    scanf("%lf",&profit);
    
    switch(int(profit)/100000)
    {
    default:
        bonus += 1.0/100*(profit-1000000);
        profit = 1000000;
    case 10:
    case 9:
    case 8:
    case 7:
    case 6:
        bonus += 1.5/100*(profit-600000);
        profit = 600000;
    case 5:
    case 4:
        bonus += 3.0/100*(profit-400000);
        profit = 400000;
    case 3:
    case 2:
        bonus += 5.0/100*(profit-200000);
        profit = 200000;
    case 1:
        bonus += 7.5/100*(profit-100000);
        profit = 100000;
    case 0:
        bonus += 10.0/100*profit;
    }      
    
    printf("可获得奖金总数为%lf\n",bonus);
    
    return 0;
}
/*
在VC++6.0中的输出结果为:
————————————
输入当月利润I:100000
可获得奖金总数为10000.000000
Press any key to continue
输入当月利润I:200000
可获得奖金总数为17500.000000
Press any key to continue
输入当月利润I:400000
可获得奖金总数为27500.000000
Press any key to continue
输入当月利润I:600000
可获得奖金总数为33500.000000
Press any key to continue
输入当月利润I:1000000
可获得奖金总数为39500.000000
Press any key to continue
输入当月利润I:2000000
可获得奖金总数为49500.000000
Press any key to continue
————————————
*/

 

4.11 输入4个整数,要求由小到大顺序输出

/*
时间:2011年10月18日13:29:05
题目:输入4个整数,要求由小到大顺序输出
*/
# include <stdio.h>

int main()
{
    void sort (int * pArr);
    int i;
    int a[4];
    int t;
    
    printf("输入4个整数:");
    for(i=0; i<4; ++i)
    {
        scanf("%d",&a[i]);
    }

    sort (a);

    printf("由小到大排列:");
    for(i=0; i<4; ++i)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
    
    return 0;
}

void sort (int * pArr)
{
    int i=1,k;
    int t;
    for(k=0; k<3; ++k)
    {
        for(i=1; i<4-k; ++i)
        {
            if(*(pArr+k)>*(pArr+k+i))
            {
                t = *(pArr+k);
                *(pArr+k) = *(pArr+k+i);
                *(pArr+k+i) = t;
            }
        }
    }
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入4个整数:6 8 1 4
由小到大排列:1 4 6 8
Press any key to continue
————————————
*/

 

4.12 4个圆塔,圆心分别为(2.-2),(2,2),(-2-2),(-2,2)。圆半径为1这四个塔的高度为10m,塔以外没有建筑物。今输入任意一点坐标,求该点的高度

/*
时间:2011年10月18日14:01:03
题目:习题4.12 有4个圆塔,圆心分别为(2.,-2),(2,2),(-2,-2),(-2,2)。圆半径为1这四个塔的高度为10m,塔以外没有建筑物。今输入任意一点坐标,求该点的高度
*/
# include <stdio.h>
# include <math.h>

int main()
{
    int altitude (double x, double y);
    double x,y;
    
    printf("输入任意一点坐标:\n");
    printf("x=");
    scanf("%lf",&x);
    printf("y=");
    scanf("%lf",&y);

    printf("该坐标位置的高度为%dm\n",altitude(x,y));
    return 0;
}

int altitude (double x, double y)
{
    double d1,d2,d3,d4;
    
    d1 = sqrt(pow((x-2),2)+pow((y-2),2));
    d2 = sqrt(pow((x+2),2)+pow((y-2),2));
    d3 = sqrt(pow((x-2),2)+pow((y+2),2));
    d4 = sqrt(pow((x+2),2)+pow((y+2),2));
    
    if(d1<=1 || d2<=1 || d3<=1 || d4<=1)
    {
        return 10;
    }
    else
    {
        return 0;
    }
}
/*
在VC++6.0中的输出结果为:
————————————
输入任意一点坐标:
x=0.5
y=0.7
该坐标位置的高度为0m
Press any key to continue
输入任意一点坐标:
x=2.1
y=2.3
该坐标位置的高度为10m
Press any key to continue
————————————
*/
默认分类 | 阅读 1699 次
文章评论,共0条
游客请输入验证码
浏览11295次
文章分类
文章归档
最新评论