C语言——选择结构训练题

  1. 计算分段函数
    在这里插入图片描述

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double x, f;
        scanf("%lf",&x);
        
        if(x>=0)
        {
            f = pow(x,0.5);
        }
        else
        {
            f = pow((x+1),2) + 2.0*x + 1.0/x;
        }
        printf("f(%.2lf) = %.2lf",x, f);
        
        return 0;
    }
    
  2. 比较大小
    在这里插入图片描述

    #include <stdio.h>
    int main()
    {
        int x,y,z,t;
        scanf("%d %d %d",&x,&y,&z);
        if(x > y)
        {
            t = y;
            y = x;
            x = t;
        } 
        if(x > z)
        {
            t = z;
            z = x;
            x = t;
        } 
        if(y > z)
        {
            t = z;
            z = y;
            y = t;
        }
        printf("%d->%d->%d\n",x, y, z);
        
        return 0;
    
    }
    
  3. 计算个人所得税
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int salary = 0;
        float rate;
        scanf("%d",&salary);
        if(salary <= 1600)
        {
        	rate = 0.00;
    	}
        else if(salary>1600 && salary<=2500)
        {
        	rate = 0.05;
    	}
    	else if(salary>2500 && salary<=3500)
    	{
    		rate = 0.1;
    	}
    	else if(salary>3500 && salary<=4500)
    	{
    		rate = 0.15;
    	}
    	else
    	{
    		rate = 0.2;
    	}
    	printf("%.2lf",rate*(unsigned)(salary-1600));
    	
        return 0;
    }
    
  4. A除以B
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int a, b;
        scanf("%d %d",&a, &b);
        
        if(b < 0)
        {
            printf("%d/(%d)=%.2lf",a, b, (double)a/b);
        }
        else if(b == 0)
        {
            printf("%d/%d=Error",a, b);
        }
        else
        {
            printf("%d/%d=%.2lf",a, b, (double)a/b);
        }
        
        return 0;
    }
    
  5. 分段计算居民水费
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        double x, y;
        scanf("%lf",&x);
        
        if(x <= 15)
        {
            y = 4.0*x / 3.0;
        }
        else
        {
            y = 2.5*x - 17.5;
        }
        printf("%.2lf",y);
        
        return 0;
    }
    
  6. 计算火车运行时间
    在这里插入图片描述

    #include<stdio.h>
    int main()
    {
    	int x,y;
    	scanf("%d %d",&x,&y);
    	int hh1,mm1,hh2,mm2;
    	hh1 = x/100;	//hh1和hh2分别取x和y的前两项,也就是小时
    	hh2 = y/100;
    	mm1 = x%100;	//mm1和mm2分别取x和y的后两项,也就是分钟
    	mm2 = y%100;
    	int hh,mm;
    	int cnt = 0;
    	hh = hh2-hh1;
    	mm = mm2 + (hh*60-mm1);	//过了几个60分钟
    	while(mm >= 60) //用总共经过的分钟除60
    	{
    		cnt++;
    		mm -= 60;
    	}
    
    	printf("%02d:%02d", cnt, mm);
    
    	return 0;
    }
    
  7. 出租车计价
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        double mile, price;
        int time, total;
        scanf("%lf %d",&mile, &time);
        if(mile <= 3)
        {
            total = 10 + 2*(time/5);
            printf("%d",total);
        }
        else if(mile <= 10 && mile > 3)
        {
            total = 10.0+ (mile-3.0)*2 + 2.0*(time/5) + 0.5;
            printf("%d",total);
        }
        else if(mile > 10)
        {
            total = 24.0+ (mile-10.0)*3 + 2*(time/5) + 0.5;
            printf("%d",total);
        }
        
        return 0;
    }
    
  8. 用天平找小球
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int a, b, c;
        scanf("%d %d %d",&a, &b, &c);
        
        if(a == b)
        {
            printf("C");
        }
        else if(a == c)
        {
            printf("B");
        }
        else if(b == c)
        {
            printf("A");
        }
        
        return 0;
    }
    
  9. 成绩转换
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int score, a;
        scanf("%d",&score);
        
        a = score/10;
        switch(a)
        {
            case 10:
            case 9: printf("A"); break;
            case 8: printf("B"); break;
            case 7: printf("C"); break;
            case 6: printf("D"); break;
            default: printf("E");
        }
        
        return 0;
    }
    
  10. 阶梯电价
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int num;
        double price;
        scanf("%d",&num);
        
        if(num <= 50 && num >= 0)
        {
            price = num*0.53;
            printf("cost = %.2lf", price);
        }
        else if(num > 50)
        {
            price = 26.5 + (num-50)*0.58;
            printf("cost = %.2lf", price);
        }
        else
        {
            printf("Invalid Value!");
        }
        return 0;
    }
    
  11. 两个数的简单计算器
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int a, b;
        char s;
        scanf("%d %c %d",&a, &s, &b);
        
        switch(s)
        {
            case '+':
                printf("%d",a+b);
                break;
            case '-':
                printf("%d",a-b);
                break;
            case '*':
                printf("%d",a*b);
                break;
            case '/':
                printf("%d",a/b);
                break;
            case '%':
                printf("%d",a%b);
                break;
            default:
                printf("ERROR");
        }
        
        return 0;
    }
    
  12. 三天打鱼两天晒网
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
    	int day, y;
    	scanf("%d",&day);
    	
    	y = day % 5;
    	switch(y)
    	{
    		case 3:
    		case 1:
    		case 2: printf("Fishing in day %d",day); break;
    		case 4:
    		case 0: printf("Drying in day %d",day); break;
    	}
    	
    	return 0;
    }
    
  13. 计算油费
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
    	int a, b;
    	char c;
    	double price;
    	scanf("%d %d %c",&a, &b, &c);
    	
    	switch(b)
    	{
    		case 90:
    		{
    			if(c=='m')
    			{
    				price = 6.95*a*0.95;
    			}
    			else if(c=='e')
    			{
    				price = 6.95*a*0.97;
    			}
    			break;
    		}
    		case 93:
    		{
    			if(c=='m')
    			{
    				price = 7.44*a*0.95;
    			}
    			else if(c=='e')
    			{
    				price = 7.44*a*0.97;
    			}
    			break;
    		}
    		case 97:
    		{
    			if(c=='m')
    			{
    				price = 7.93*a*0.95;
    			}
    			else if(c=='e')
    			{
    				price = 7.93*a*0.97;
    			}
    			break;
    		}
    	}
    	printf("%.2lf",price);
    	
    	return 0;
    }
    
  14. 超速判断
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
    	int v=0;
    	scanf("%d",&v);     //输入速度 
    	
    	if(v > 60)
    	{
    		printf("Speed: %d - Speeding",v);
    	}else
    	{
    		printf("Speed: %d - OK",v);
    	}
    	
    	return 0; 
    } 
    
  15. 回文数
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int num;
        scanf("%d",&num);
        
        if(num/10000 == num%10)
        {
            if((num/1000 - num/10000*10) == ((num%100 - num%10)/10))
            {
                printf("yes");
            }else
                printf("no");
        }else
                printf("no");
        
        return 0;
    }
    
  16. 求一元二次方程的根
    在这里插入图片描述

    #include <stdio.h>
    #include <math.h>
    int main()
    {
        double a,b,c;
        scanf("%lf %lf %lf",&a, &b, &c);
        double x;
        x=b*b-4*a*c;
        if(a==0 && b==0 && c==0){
            printf("Zero Equation\n");
        }else if(a==0 && b==0 && c!=0){
            printf("Not An Equation\n");
        }else if(a==0 && b!=0){
            printf("%.2f\n",-c/b);
        }else if(x==0){
            printf("%.2f\n",-b/2/a);
        }else if(x>0){
            printf("%.2f\n%.2f\n",(-b+sqrt(x))/2/a,(-b-sqrt(x))/2/a);
        }else{
            if(b==0){
                printf("0.00+%.2fi\n0.00-%.2fi\n",sqrt(-x)/2/a,sqrt(-x)/2/a);
            }else
            printf("%.2f+%.2fi\n%.2f-%.2fi\n",-b/2/a,sqrt(-x)/2/a,-b/2/a,sqrt(-x)/2/a);
        }
        return 0;
    }
    
  17. 判断一个三位数是否为水仙花数
    在这里插入图片描述

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	int n, x = 0;
    	int y = 0, i;
    	scanf("%d",&n);
    	i=n;
    	
    	if(n<100 || n>999)
    	{
    		printf("Invalid Value.");
    	}else
    	{
    	for(;n != 0;)
    	{
    		x = n%10;
    		x = pow(x,3);
    		y += x;
    		n /= 10;
    	}
    	if(y == i)
    	{
    		printf("Yes");
    	}else
    		printf("No");	
    	}	
    	return 0;
    }
    
  18. 分段函数
    在这里插入图片描述

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double x, y;
        scanf("%lf",&x);
        
        if(x > 6)
        {
            y = 5.0/27*(pow(x,2) + 4.0*x - 6.0);
        }
        else if(x <= 6 && x > 0)
        {
            y = log(16.0)/log(3.0) + x;
        }
        else
        {
            y = 23/7.0*fabs(pow(x,3) + 4.0);
        }
        printf("%.3lf",y);
        
        return 0;
    }
    
  19. 判断字符或数字
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        char x;
        scanf("%c",&x);
        
        if((x>'a' && x<'z') || (x>'A' && x<'Z'))
        {
            printf("character");
        }
        else if(x>'0' && x<'9')
        {
            printf("digit");
        }
        else
        {
            printf("ERROR");
        }
        
        return 0;
    }
    
  20. 输出三角形面积和周长
    在这里插入图片描述

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int a, b, c;
        scanf("%d %d %d",&a, &b, &c);
        
        if(a<b+c && b<a+c && c<a+b)
        {
        double area, perimeter, s;
        perimeter = a+b+c;
        s = (a+b+c) / 2.0;
        area = sqrt(s*(s-a)*(s-b)*(s-c));
        
        printf("area = %.2lf; perimeter = %.2lf",area, perimeter);
        }
        else
        {
            printf("These sides do not correspond to a valid triangle");
        }
        
        return 0;
     }
    
  21. 考研录取情况
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int math, eng, politics, pro, total;
        scanf("%d %d %d %d",&math, &eng, &politics, &pro);
        
        total = math + eng + politics + pro;
        if(math>=85 && pro>=85 && eng>=55 && politics>=55 && total>=305)
        {
            if(total>=370)
            {
                printf("A");
            }else
                printf("B");
        }else
            printf("C");
        
        return 0;
    }
    
  22. 输入苹果等级和数量,计算并输出应付款
    在这里插入图片描述

    #include <stdio.h>
    
    int main()
    {
        int grade, num;
        double price;
        scanf("%d %d",&grade, &num);
        
        switch(grade)
        {
            case 1:
                price = 5.50*num;
                printf("%.2lf",price);
                break;
            case 2:
                price = 4.30*num;
                printf("%.2lf",price);
                break;
            case 3:
                price = 3.00*num;
                printf("%.2lf",price);
                break;
            case 4:
                price = 2.50*num;
                printf("%.2lf",price);
                break;
            default:
                printf("Data Error!");
        }
        
        return 0;
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值