山东理工大学PTA实验二选择结构

7-1 求两个整数之中较大者

#include <stdio.h>
int main()
{
    int a,b,max;
    scanf("%d %d",&a,&b);
    if(a>b)
        max=a;
    else
        max=b;
    printf("max=%d",max);
    return 0;
}

7-2 求绝对值(选择结构)

#include <stdio.h>
int main()
{
    int a;
    scanf("%d",&a);
    if(a>=0)
        printf("%d",a);
    else
        printf("%d",-a);
    return 0;
}

7-3 整除

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    if(n%3==0&&n%5==0)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

7-4 相加和最大值

#include<stdio.h>
int main()
{
    int a,b,c,x,y,z;
    scanf("%d %d %d",&a,&b,&c);
    x=a+b,y=a+c,z=b+c;
    if(x>=y&&y>=z||x>=z&&z>=y)
        printf("%d",x);
    else if(y>=x&&x>=z||y>=z&&z>=x)
        printf("%d",y);
    else
        printf("%d",z);
    return 0;
}

7-5 找中间数

方法一:

#include<stdio.h>
int main()
{
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if(a>=b&&b>=c||c>=b&&b>=a)
        printf("%d",b);
    else if(b>=a&&a>=c||c>=a&&a>=b)
        printf("%d",a);
    else
        printf("%d",c);
    return 0;
}

方法二:

#include<stdio.h>
int main()
{
    int x,y,z,n;
    scanf("%d,%d,%d",&x,&y,&z);
    if(x>y)
    {n=x;x=y;y=n;}
    if(x>z)
    {n=x;x=z;z=n;}
    if(y>z)
    {n=y;y=z;z=n;}
    printf("%d",y);
    return 0;
}

7-6 三个数排序

#include<stdio.h>
int main()
{
    int x,y,z,n;
    scanf("%d,%d,%d",&x,&y,&z);
    if(x>y)
    {n=x;x=y;y=n;}
    if(x>z)
    {n=x;x=z;z=n;}
    if(y>z)
    {n=y;y=z;z=n;}
    printf("%d %d %d",x,y,z);
    return 0;
}

7-7 三位数整数的各位数字

#include<stdio.h>
int main()
{
    int a,x,y,z;
    scanf("%d",&a);
    x=a/100;
    y=a/10-x*10;
    z=a-(x*100+y*10);
    if(a>=100&&a<=999)
        printf("%d = %d + %d*10 + %d*100",a,z,y,x);
    else
        printf("Please input a three digits number.\n");
    return 0;
}

7-8 三个整数的最大值

#include<stdio.h>
int main()
{
    int a,b,max;
    scanf("%d,%d,%d",&a,&b,&max);
    if(a>max)
        max=a;
    if(b>max)
        max=b;
    printf("max=%d",max);
    return 0;
}

7-9 时间间隔

#include<stdio.h>
int main()
{
    int a,b,c,x,y,z,m,n,t;
    scanf("%d:%d:%d\n%d:%d:%d",&a,&b,&c,&x,&y,&z);
    m=3600*a+60*b+c;
    n=3600*x+60*y+z;
    if(m>n)
        t=m-n;
    else
        t=n-m;
    printf("%02d:%02d:%02d\n",t/3600,t/60%60,t%60);
    return 0;
}

7-10 然后是几点

#include<stdio.h>
int main()
{
    int a,b,c,m,n,x,y;
    scanf("%d %d",&a,&b);
    x=a/100;
    y=a%100;
    c=x*60+y+b;
    m=c/60;
    n=c%60;
    printf("%d%02d",m,n);
    return 0;
}

7-11 计算分段函数【1】

#include<stdio.h>
int main()
{
    float x;
    scanf("%f",&x);
    if(x==0)
        printf("f(%.1f) = %.1f",x,0.0);
    else
        printf("f(%.1f) = %.1f",x,1/x);
    return 0;
}

7-12 计算分段函数【2】

#include<stdio.h>
int main()
{
    float x,restult;
    scanf("%f",&x);
    if(x>=0){
        restult=sqrt(x);
    printf("f(%.2f) = %.2f",x,restult);}
    else{
        restult=pow(x+1,2)+2*x+1/x;
    printf("f(%.2f) = %.2f",x,restult);}
    return 0;
}

7-13 虎子算电费

#include<stdio.h>
int main()
{
    double x,cost=0;
    scanf("%lf",&x);
    if(x<=50&&x>=0)
    {
        cost=0.53*x;
        printf("cost = %.2f",cost);
    }
    else if(x>50)
    {
        cost=0.53*50+(x-50)*0.58;
        printf("cost = %.2f",cost);
    } 
    else if(x<0)
    {
      printf("Invalid Value!");
    }
    return 0;
}

7-14 时间格式转换

#include<stdio.h>
int main()
{
    int m,n;
    scanf("%d:%d",&m,&n);
    if(m==0)
        printf("%02d:%02d AM",12,n);
    if(m>=1&&m<=11)
        printf("%02d:%02d AM",m,n);
    if(m==12)
        printf("%02d:%02d PM",12,n);
    if(m>=13&&m<=23)
        printf("%02d:%02d PM",m-12,n);
    return 0;
}

7-15 模拟计算器

#include <stdio.h>
int main()
{
    int a,b;
    char c;
    scanf("%d %d\n",&a,&b);
    scanf("%c",&c);
    if(c=='+')
        printf("%d\n",a+b);
    if(c=='-')
        printf("%d\n",a-b);
    if(c=='*')
        printf("%d\n",a*b);
    if(c=='/')
        printf("%d\n",a/b);
    return 0;
}

7-16 输入数字星期,输入英文(switch语句)

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    switch(n)
    {
        case 1:printf("Monday");break;
        case 2:printf("Tuessday");break;
        case 3:printf("Wednesday");break;
        case 4:printf("Thursday");break;
        case 5:printf("Friday");break;
        case 6:printf("Saturday");break;
        case 7:printf("Sunday");break;
        default:printf("error");
    }
    return 0;
}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值