c语言练习

发布于 2023-04-01  122 次阅读


题目一

输入一个最高为五位的数字。

  1. 判断为几位数
  2. 按顺序逐个输出
  3. 逆序输出

判断为几位数

错误记录

#include <stdio.h>
int main()
{
    int a;
    printf("enter a number:");
    scanf("%d",&a);
    switch(a)
    {
    case'a<10':printf("一位数");break;
    case'a<100':printf("为两位数");break;
    case'a<1000':printf("为三位数");break;
    case'a<10000':printf("为四位数");break;
    case'a<100000':printf("为五位数");break;
    }
    return 0;
}    
  • 原因:switch中case公式为 case 常量:语句;这里的常量用了表达式;
  • 解决思路,用整型除法特征先计算位数

If语句版本

#include <stdio.h>
int main()
{
    int a;
    printf("enter a number:");
    scanf("%d",&a);
    if(a<10)
    {
        printf("为一位数");
    }
    else if(a<100)
    {
        printf("为两位数");
    }
    else if(a<1000)
    {
        printf("为三位数");
    }
    else if(a<10000)
    {
        printf("为四位数");
    }
    else if(a<100000)
    {
        printf("为五位数");
    }
        return 0;
}

按顺序逐个输出

开始想出来的是逆序运算,后面想到可以魔改做到顺序逐个输出


逆序输出

思路


就是把输入的数字处以10得到余数,比如1899%10=9=y,然后输出了个位数9
循环里把x/10由于x是整型得到的是189,再%10得到9输出
再把x/10得到18
然后%10得到8

代码

#include<stdio.h>

int main()
{
 int x = 0, y = 0;
 scanf("%d", &x);

 do 
 {
  y = x % 10;
  printf("%d", y);
  x = x / 10;
 } while (x != 0);
 return 0;

}

最终版本

#include<stdio.h>
#include<math.h>

int main()
{
	int x = 0, y = 0, z = 0, t = 0, m=0, i = 0;
	printf("请输出最高为五位的数字:");
	scanf("%d", &x);

	z = x;
	t = x;

	do 
	{
		z = z / 10;
		i++;
	} while (z != 0);

	switch(i)
	{
	case 1:printf("为个位数\n");
		break;
	case 2:printf("为十位数\n");
		break;
	case 3:printf("为百位数\n");
		break;
	case 4:printf("为千位数\n");
		break;
	case 5:printf("为万位数\n");
		break;
	default:
		printf("你小子是不是输入了大于五位的数?\n");        //算位数
		return 0;
	}

	printf("输入的数字逐个输出为:\n");
	do 
	{
		i--;
		m = t / pow(10, i);
		printf("%d\n", m);
		t = t - m * pow(10, i);
	} while (t != 0);

	printf("这个数字逆序输出为:");
	do
	{
		y = x % 10;
		printf("%d", y);
		x = x / 10;
	} while (x != 0);                                      //逆序输出

	return 0;
}

题目二

企业发放的奖金根据利润提成。利润I低于或等于 100 000 元的,奖金可提成10%:利润高于100 000 元,低于 200 000 元(100 000I<200 000)时,低于 100 000 元的部分按10%提成,高于 100 000 元的部分,可提成 7.5%;200 0001 000 000 时,超过1000 000 元的部分按 1%提成。从键盘输人当月利润 I,求应发奖金总数。

switch解决方案

错误示例

#include<stdio.h>
#include<math.h>

int main()
{
	int I = 0, i = 0;
	double I1 = 0, I2 = 0, I3 = 0, I4 = 0,
		  I5 = 0, I6 = 0, W = 0;
	printf("请输入月利润:");
	scanf("%d", &I);

	i = (I / 100000) ;
	if (I = i * 100000)
	{
		i = i - 1;
	}
	else 
	{
		i = i;
	}

	switch (i)
	{
	default:
		I6 = (I - 1000000) * 0.01;
		//大于1000 000

	case 7:
	case 8:
	case 9:
	{
		if (I > 1000000)
		{
			I5 = 400000 * 0.015;
		}
		else if(I <= 100000)
		{
			I5 = (I - 600000) * 0.015;
		}
	}
	//大于600 000
	case 4:
	case 5:
	{
		if (I > 600000)
		{
			I4 = 200000 * 0.03;
		}
		else if(I <= 600000)
		{
			I4 = (I - 400000) * 0.03;
		}
	}
	//大于400 000

	case 2:
	case 3:
	{
		if (I > 400000)
		{
			I3 = 200000 * 0.05;
		}
		else if(I <= 400000)
		{
			I3 = (I - 200000) * 0.05;
		}
	}
	//大于200 000
	case 1:
	{
		if (I > 200000)
		{
			I2 = 100000 * 0.075;
		}
		else if(I <= 200000)
		{
			I2 = (I - 100000) * 0.075;
		}
	}
	//大于100 000
	case 0:
	{
		if (I > 100000)
		{
			I1 = 100000 * 0.1;
		}
		else if(I <= 100000)
		{
			I1 = I * 0.10;
		}
	}
	printf("%lf %lf %lf %lf %lf %lf\n", I1, I2, I3, I4, I5, I6);
	printf("总奖金为%5.2f", W = (I1 + I2 + I3 + I4 + I5));
	break;
	}
	

	return 0;
}

正确版本

# include <stdio.h>
int main()
{
    int i, branch;
    double bonus, bon1, bon2, bon4, bon6, bon10;
    bon1 = 100000 * 0.1;
    bon2 = bon1 + 100000 * 0.075;
    bon4 = bon2 + 100000 * 0.05;
    bon6 = bon4 + 100000 * 0.03;
    bon10 = bon6 + 400000 * 0.015;
    printf("请输入当月利润i:");
    scanf("%d", &i);
    printf("i=%d\n", i);
    branch = i / 100000;
    if (branch > 10) {
        branch = 10;
    }
    switch (branch) {
    case 0:bonus = i * 0.1; break;
    case 1:bonus = bon1 + (i - 100000) * 0.075; break;
    case 2:
    case 3:bonus = bon2 + (i - 200000) * 0.05; break;
    case 4:
    case 5:bonus = bon4 + (i - 400000) * 0.03; break;
    case 6:
    case 7:
    case 8:
    case 9:bonus = bon6 + (i - 600000) * 0.015; break;
    case 10:bonus = bon10 + (i - 1000000) * 0.01;
    }
    printf("奖金是%10.2f\n", bonus);
    return 0;
}

if解决方案

不知道是否正确版

#include <stdio.h>
int main()
{
    float I,r1,r2,r3,r4,r5,r6,bound1,bound2,bound3,bound4,bound5,bound6,I1;
    r1=0.1;
    r2=0.075;
    r3=0.05;
    r4=0.03;
    r5=0.015;
    r6=0.01;
    I1=100000;
    printf("请输入利润:");
    scanf("%f",&I);
    if(I<=100000)
    {
        bound1=r1*I;
        printf("奖金为:%f",bound1);
    }
    else if(I<=200000)
    {
        bound2=I1*r1+(I-I1)*r2;
        printf("奖金为:%f",bound2);
    }
    else if(I<=400000)
    {
        bound3=I1*r1+I1*r2+(I-2*I1)*r3;
        printf("奖金为:%f",bound3);
    }
    else if(I<=600000)
    {
        bound4=I1*r1+I1*r2+2*I1*r3+(I-4*I1)*r4;
        printf("奖金为:%f",bound4);
    }
    else if(I<=1000000)
    {
        bound5=I1*r1+I1*r2+2*I1*r3+2*I1*r4+(I-6*I1)*r5;
        printf("奖金为:%f",bound5);
    }
    else if(I>1000000)
    {
        bound5=I1*r1+I1*r2+2*I1*r3+2*I1*r4+4*I1*r5+(I-10*I1)*r6;
        printf("奖金为:%f",bound6);
    }
    return 0;
}

正确版

#include <stdio.h>
int main()
{
	double I, salary = 0;
	printf("enter performance:");
	scanf_s("%lf", &I);
	if (I < 0) {
		printf("请输入一个正数\n");
		system("pause");
		return -1;
	}
	double salary1 = 100000 * 0.1;//10万的奖金
	double salary2 = (200000 - 100000) * 0.075 + salary1;//20万的奖金
	double salary3 = (400000 - 200000) * 0.05 + salary2;//40万的奖金
	double salary4 = (600000 - 400000) * 0.03 + salary3;//60万的奖金
	double salary5 = (1000000 - 600000) * 0.015 + salary4;//100万的奖金
	if (I <= 100000) {
		salary = I * 0.1;//小于100000按10%提成
	}else if (I > 100000 && I <= 200000) {
		salary = salary1 + (I - 100000) * 0.075;//多出10万的按比例计算,加上10w的奖金
	}else if (I > 200000 && I <= 400000) {
		salary = salary2 + (I - 200000) * 0.05;//多出20万的按比例计算,加上20w的奖金
	}else if (I > 400000 && I <= 600000) {
		salary = salary3 + (I - 400000) * 0.03;//多出40万的按比例计算,加上40w的奖金
	}else if (I > 600000 && I <= 1000000) {
		salary = salary4 + (I - 600000) * 0.015;//多出60万的按比例计算,加上60w的奖金
	}else if (I > 1000000){
		salary = salary5 + (I - 1000000) * 0.01;//多出100万的按比例计算,加上100w的奖金
	}
	printf("salary:%f\n", salary);
	system("pause");
	return 0;
}