C language daily practice (1)

C Practice Example 1

Question: There are 1, 2, 3, 4 numbers, how many three-digit numbers that are different from each other and have no repeated numbers can be formed? How much are they?

Program analysis: The numbers that can be filled in the hundreds, tens, and ones are all 1, 2, 3, and 4. After all the permutations are formed, the permutations that do not meet the conditions are removed.

Program source code:

#include<stdio.h>

int main()
{
	int i,j,k;
	printf("\n");
	for(i=1;i<5;i++) { // 以下为三重循环
		for(j=1;j<5;j++) {
			for (k=1;k<5;k++) { // 确保i、j、k三位互不相同
				if (i!=k&&i!=j&&j!=k) { 
					printf("%d,%d,%d\n",i,j,k);
				}
			}
		}
	}
}

The output of the above example is:


1,2,3
1,2,4
1,3,2
1,3,4
1,4,2
1,4,3
2,1,3
2,1,4
2,3,1
2,3,4
2,4,1
2,4,3
3,1,2
3,1,4
3,2,1
3,2,4
3,4,1
3,4,2
4,1,2
4,1,3
4,2,1
4,2,3
4,3,1
4,3,2

C Practice Example 2 

Topic: The bonus issued by the company is based on the profit commission. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%. 7.5% of the cocoa commission; between 200,000 and 400,000, 5% for the part higher than 200,000 yuan; 3% for the part above 400,000 yuan between 400,000 and 600,000 ; When between 600,000 and 1 million, the portion higher than 600,000 can be commissioned at 1.5%. Total bonuses paid out?

Program analysis: Please use the number axis to divide and locate. Note that the bonus needs to be defined as a growing integer when defining.

Program source code:

#include<stdio.h>
int main()
{
	double i;
	double bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
	printf("你的净利润是:\n");
	scanf("%lf",&i);
	bonus1=100000*0.1;
	bonus2=bonus1+100000*0.075;
	bonus4=bonus2+200000*0.05;
	bonus6=bonus4+200000*0.03;
	bonus10=bonus6+400000*0.015;
	if(i<=100000) {
		bonus=i*0.1;
	} else if(i<=200000) {
		bonus=bonus1+(i-100000)*0.075;
	} else if(i<=400000) {
		bonus=bonus2+(i-200000)*0.05;
	} else if(i<=600000) {
		bonus=bonus4+(i-400000)*0.03;
	} else if(i<=1000000) {
		bonus=bonus6+(i-600000)*0.015;
	} else if(i>1000000) {
		bonus=bonus10+(i-1000000)*0.01;
	}
	printf("提成为:bonus=%lf",bonus);

	printf("\n");
}

The output of the above example is:

你的净利润是:
120000
提成为:bonus=11500.000000

C Practice Example 3 

Question: An integer, adding 100 to it is a perfect square number, and adding 168 to it is a perfect square number, what is the number?

Program analysis: To judge within 100,000, first add 100 to the number before opening the square, then add 268 to the number before opening the square. If the result after opening the square meets the following conditions, it is the result.

Program source code:

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

int main() {
    long int i,x,y;
    for (i=1;i<100000;i++) {
        x=sqrt(i+100); // x为加上100后开方后的结果
        y=sqrt(i+268); // y为再加上168后开方后的结果
        if(x*x==i+100 && y*y==i+268){ //如果一个数的平方根的平方等于该数,这说明此数是完全平方数
            printf("\n%ld\n",i);
        }
    }
}

The output of the above example is:

21

261

1581

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/131550663