Dream Dragon_C Language Homework 5

1. Fill in the blanks
1、26
Two, short answer questions
1. Enter 10 scores from the keyboard (between 0 and 100), and after removing the highest and lowest scores, find the average score, and keep 1 decimal place to display the result. (Do not have any prompt text, and the result is a double-precision number without a carriage return)

Method 1: Use an array

/*
时间:2021-3-30
作者:童话
环境:Win 10 、 Visual Studio 2019 
*/
double scores[10];			//	储存分数的数组
double max;					//	分数最大值的变量
double min;					//	分数最小值的变量
double sum;					//	分数求和变量
int i;						//	循环变量

/* 循环输入分数 */
for ( i = 0; i < 10; i++)
{
    
    
	scanf("%lf", &scores[i]);
}
/*	初始化最值为数组第一个元素	*/
max = min = scores[0];
for ( i = 1; i < 10; i++)
{
    
    
	if (scores[i] > max)
	{
    
    
		max = scores[i];
	}
	if (scores[i] < min)
	{
    
    
		min = scores[i];
	}
}
/*	初始化和为0	*/
sum = 0;
for (i = 0; i < 10; i++)
{
    
    
	sum += scores[i];
}
/*	累加的和减去最值	*/
sum -= (min + max);
/*输出	%.1f 表示输出一位小数*/
printf("%.1f", sum / 8);

Method Two:

/*
时间:2021-3-30
作者:童话
环境:Win 10 、 Visual Studio 2019 
*/
double score;				//	分数
double max = 0;				//	最大值,初始化为分数范围的下限
double min = 100;			//	最小值,初始化为分数范围的上限
double sum = 0;				//	分数的和,初始化为0
int i;						//	循环变量

for (i = 0; i < 10; i++)
{
    
    
	/* 输入 */
	scanf("%lf", &score);
	/* 判断最值 */
	if (score > max)
	{
    
    
		max = score;
	}
	if (score < min)
	{
    
    
		min = score;
	}
	/* 累加求和 */
	sum += score;
}
/* 输出 */
printf("%.1f", (sum - max - min) / 8);
3. Procedural questions
1. Please program to find all the daffodil numbers and sum them. The daffodil number is a three-digit number. The cube of the hundreds digit, the cube of the tens digit, and the cube of the single digit must add up to the number itself. For example: 153=1 1 1+5 5 5+3 3 3 is the number of narcissus. (Display all daffodil numbers and their sum in one line from small to large, separate the numbers with a space, do not prompt text and enter)
/*
时间:2021-3-30
作者:童话
环境:Win 10 、 Visual Studio 2019 
*/
int i;			/*	循环变量	*/
int a;			/*	个位	*/
int b;			/*	十位	*/
int c;			/*	百位	*/
int sum = 0;	/*	和		*/
for (i = 100; i <= 999; i++)
{
    
    
	a = i % 10;
	b = i / 10 % 10;
	c = i / 100;

	if (i == a * a * a + b * b * b + c * c * c)
	{
    
    
		sum += i;
		printf("%d ", i);
	}
}
printf("%d", sum);
2、

Given that the minimum salary of a company’s employees is 500, the relationship between the profit (a positive integer) of the project received in a certain month and the profit commission is as follows (measurement unit: yuan)
profit≤1000 no commission
1000<profit≤2000 commission 10%;
2000 <profit≤5000 commission 15%;
5000<profit≤10000 commission 20%;
10000<profit commission 25%.
It is required to input the project profit of a certain employee for a certain month, and output the actual salary of the employee. (Double-precision real number calculation, the result shows that keep 2 decimal places without carriage return)

Choose one of the two, recommend the second

/*
时间:2021-3-30
作者:童话
环境:Win 10 、 Visual Studio 2019 
*/
int profit;
double com = 0;
scanf("%d", &profit);

if (profit <= 1000)
	com = 0;
else if (profit > 1000 && profit <= 2000)
	com = 0.1;
else if (profit > 2000 && profit <= 5000)
	com = 0.15;
else if (profit > 5000 && profit <= 10000)
	com = 0.2;
else if (profit > 10000)
	com = 0.25;

printf("%.2f", 500 + profit * com);
/*
时间:2021-3-30
作者:童话的女朋友
环境:Win 10 、 学习通
*/
int profit,r;
double x;
scanf("%d",&profit);
r=(profit-1)/1000;
switch(r)
{
    
    
	case 0:x=500;break;
    case 1:x=500+profit*0.1;break;
    case 2:
    case 3:
    case 4:x=500+profit*0.15;break;
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:x=500+profit*0.2;break;
    default:x=500+profit*0.25;break;
}
printf("%.2f",x);
3、

Enter the integer n, calculate the expression
Insert picture description here
and display the result. (The result is a double-precision number, display 10 digits after the decimal point, do not enter)

/*
时间:2021-3-30
作者:童话
环境:Win 10 、 Visual Studio 2019 
*/
int n;
int i, j;
long long t;	// int 和 long 不通过
double e = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
    
    
	t = 1;
	for (j = 1; j <= i; j++)
	{
    
    
		t *= j;
	}
	e += (1.0 / t);
}
printf("%.10f", e);

Guess you like

Origin blog.csdn.net/l2754283833/article/details/115323069