# Classic examples of C language 1-5

Keywords: C Back-end

## Question 1:

Title: there are 1, 2, 3 and 4 numbers. How many three digits that are different from each other and have one digit, ten digit and hundred digit without repetition? How much is it?

analysis:

1. Show all the numbers first

2. Remove elements with the same number

3. Printing

code implementation

```#include<stdio.h>
int main()
{
int i, j, k;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
{

for (k = 1; k <= 4; k++)
{
if (i != k && i != j && j != k)  //Avoid having the same number
{
printf("%d%d%d\n", i, j, k);

}

}
}

}

}```

Result display ## Question 2:

Title: the bonus paid by the enterprise is based on the profit commission.

• When the profit (I) is less than or equal to 100000 yuan, the bonus can be increased by 10%;
• When the profit is higher than 100000 yuan and lower than 200000 yuan, the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan will be deducted by 7.5%;
• Between 200000 and 400000 yuan, the part higher than 200000 yuan can be deducted by 5%;
• 3% commission can be given for the part higher than 400000 yuan between 400000 and 600000 yuan;
• When it is between 600000 and 1 million yuan, the part higher than 600000 yuan can be deducted by 1.5%;
• When it is higher than 1 million yuan, the part exceeding 1 million yuan shall be deducted by 1%.

Enter the profit I of the current month from the keyboard to find the total amount of bonus to be paid?

Analysis: use if else if statement to complete classification discussion

if-else if   Statement flow chart: review:

The format description consists of '%' and format characters, such as% d% f. Its function is to convert the output data into the specified format for output.

Note that this problem is floating point.

Code implementation:

```#include<stdio.h>
int main()
{
double i;     //Note: profit is a double variable
double bonus1, bonus2, bonus4, bonus6, bonus10, bonus;   //Calculate the profit of each step separately
scanf("%lf", &i);   //The format output character of double type is lf
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("Proposed as: bonus=%lf", bonus);
printf("\n");
}```

## Question 3:

Title: an integer, which is a complete square number after adding 100, and a complete square number after adding 168. What is the number?

Method 1: violent solution. Computers are suitable for doing repetitive things, so we try the answers one by one

```#include<stdio.h>
#include<math.h>
int main()
{
int i,a,b;
for (i = -100; i < 1000000; i++)
{
a = sqrt(i + 100);
b = sqrt(i + 100 + 168);
if (a * a == i+100 && b * b == i+100+168)
{
printf("%d\n", i);
}
}
}```

result Method 2: mathematical solution

Program analysis:

Suppose the number is x.

1. Then: x + 100 = n2, x + 100 + 168 = m2

2. Calculation equation: m2  - n2  = (m + n)(m - n) = 168

3. Setting: m + n = i, m - n = j, i * j =168, at least one of i and j is even

4. It can be obtained that m = (i + j) / 2, n = (i - j) / 2. Since both m and N are integers, i and j are either even or odd   Number.

5. From the derivation of 3 and 4, we can know that i and j are even numbers greater than or equal to 2.

6. Since i * j = 168, J > = 2, then   1 < i < 168 / 2 + 1.

7. Next, cycle all the numbers of i.

```#include <stdio.h>

int main(void)
{
int  i, j, m, n, x;
for (i = 1; i < 168 / 2 + 1; i++)
{
if (168 % i == 0)    //Condition: because j is also an integer
{
j = 168 / i;
if (i > j && (i + j) % 2 == 0 && (i - j) % 2 == 0)    //i> J avoids repetition, m and N are not integers
{
m = (i + j) / 2;
n = (i - j) / 2;
x = n * n - 100;
printf("%d + 100 = %d * %d\n", x, n, n);
printf("%d + 268 = %d * %d\n", x, m, m);
}
}
}
return 0;
}```

result: ## Question 4:

Title: enter a certain day of a certain month in a certain year to judge the day of the year?

Program analysis: take March 5 as an example, you should add up the of the first two months, and then add 5 days, that is, the day of the year. In special cases, when the leap year and the input month is greater than 3, you need to consider adding one more day.

Method 1: use array

```#include<stdio.h>
int Isrun(int year)
{
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return 1;
else
return 0;

}

int main()
{
int day,mouth,year,i;
int sum = 0;
int a ={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b ={0,31,29,31,30,31,30,31,31,30,31,30,31};
printf("\n Please enter year, month and day in the format of year, month and day (2015),12,10)\n");
scanf("%d,%d,%d", &year, &mouth, &day);
if (Isrun(year))
{
for (i = 1; i <mouth; i++)
{
sum = sum + b[i];
}
sum = sum + day;
printf("Days are%d",sum);
}
else
{
for (i = 1; i <mouth; i++)
{
sum = sum + a[i];
}
sum = sum + day;
printf("Days are%d", sum);
}

return 0;
}

```

Method 2:

Use the switch---case statement

Review the use of switch case

Switch (expression)

case constant 1: statement 1; break;     (error prone) must be a constant!!

. . . . .

default: statement;

```#include <stdio.h>
int main()
{
int day,month,year,sum,leap;
printf("\n Please enter year, month and day in the format of year,month,Date (2015),12,10)\n");
scanf("%d,%d,%d",&year,&month,&day);  // Format: December 10, 2015
switch(month) // Calculate the total number of days before a month
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day; // Plus the number of days of a day
if(year%400==0||(year%4==0&&year%100!=0)) {// Judge whether it is a leap year
leap=1;
} else {
leap=0;
}
if(leap==1&&month>2) { // *If it is a leap year and the month is greater than 2, the total number of days should be added by one day
sum++;
}
printf("This is the third day of the year %d Oh, my God.",sum);
printf("\n");
}```

## Question 5

Title: enter three integers x, y and Z. please output these three numbers from small to large.

Program analysis: we try to put the smallest number on X, first compare x with y, if x > y, exchange the values of X and y, and then compare x with z. if x > z, exchange the values of X and z, so as to minimize X.

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

Posted by wiggly81 on Sun, 05 Dec 2021 14:02:56 -0800