Classical, practical, and interesting programming examples in C/C ++ (2)

Source: Internet
Author: User
Tags float max

11. Fishing or surfing the internet

There is a saying in China that "three days of fishing for two days ". Someone has been "fishing for two days" for three days since January 1, January 1, 1990, and asked if this person was "fishing" or "surfing the Net" in the next day ".

* Problem analysis and Algorithm Design
Based on the question, you can divide the problem solving process into three steps:
1) calculate the total number of days from January 1, January 1, 1990 to the specified date;
2) because the period of "Fishing" and "drying" is 5 days, the calculated days are removed by 5;
3) Based on the remainder, determine whether it is "Fishing" or "drying ";
If the remainder is 1, 2, 3, then it is "playing fish"
Otherwise"
In these three steps, the key is the first step. Determine the number of days from January 1, January 1, 1990 to the specified date. Determine whether a leap year exists in the year of experience. January 29, February, and 28. The leap method can be described using a pseudo statement as follows:
If (the year can be 4 out and cannot be 100 out) or 400 out)
The year is a leap year;
Otherwise, it is not a leap year.
In C language, the remainder operation can be used to determine whether division can be performed (namely, the modulus)

* Program description and comment
# Include <stdio. h>
Int days (struct date day );
Struct Date {
Int year;
Int month;
Int Day;
};

Int main ()
{
Struct date today, term;
Int yearday, year, day;
Printf ("Enter year/month/day :");
Scanf ("% d", & today. Year, & today. Month, & today. Day);/* enter the date */
Term. month = 12;/* set the initial value of the variable: month */
Term. Day = 31;/* set the initial value of the variable: day */
For (yearday = 0, year = 1990; year <today. Year; year ++)
{
Term. Year = year;
Yearday + = days (TERM);/* calculate the total number of days of the previous year from January 1, 1990 to the specified year */
}
Yearday + = days (today);/* add the number of days from the specified year to the specified date */
Day = yearday % 5;/* calculate the remainder */
If (day> 0 & day <4) printf ("he was fishing at that day./N");/* print the result */
Else printf ("he was sleeping at that day./N ");
}

Int days (struct date Day)
{
Static int day_tab [2] [13] =
{31, 28, 31, 30, 31, 30, 31, 30, 31,},/* Average number of days per month */
{0, 31, 29,31, 30,31, 30,31, 31,30, 31,30, 31 ,},
};
Int I, LP;
Lp = day. Year % 4 = 0 & day. Year % 100! = 0 | day. Year % 400 = 0;
/* Determine whether year is a leap year or a flat year. Lp = 0 indicates a flat year, and non-0 indicates a leap year */
For (I = 1; I <day. month; I ++)/* calculate the number of days since January 1, January 1 in the current year */
Day. day + = day_tab [LP] [I];
Return day. Day;
}

* Running result
Enter year/month/day: 1991 10 25
He was fishing at day.
Enter year/month/day: 1992 10 25
He was sleeping at day.
Enter year/month/day: 1993 10 25
He was sleeping at day.

* Questions
Print the calendar of the expected year

12. Capture traffic accidents

A truck broke the traffic rules and ran away. Three people witnessed the event, but did not remember the car number. They only noted down some characteristics of the car number. A said: The first two digits of a license are the same. B said: the last two digits of a license are the same, but different from the first two. C is a mathematician. He said: the four-digit car number is just the square of an integer. Find the vehicle number based on the above clues.

* Problem analysis and Algorithm Design
Create an integer with the same first and second digits and the same two digits and different ones according to the requirements of the question, and then judge whether the integer is the square of another integer.

* Program description and comment
# Include <stdio. h>
# Include <math. h>
Int main ()
{
Int I, J, K, C;
For (I = 1; I <= 9; I ++)/* I: the value of the first two digits of the vehicle ID */
For (j = 0; j <= 9; j ++)/* j: the value of the last two digits of the vehicle ID */
If (I! = J)/* determine whether two numbers are different */
{
K = I * 1000 + I * 100 + J * 10 + J;/* calculate the possible integer */
For (C = 31; C * C <K; C ++);/* determines whether the number is the square of another integer */
If (C * c = k) printf ("lorry-no. Is % d./N", k);/* If yes, print the result */

}
}

* Running result
Lorry _ No. Is 7744

 

13. How much does the storage cost?

Assume that the minimum monthly interest of a bank in a year is 0.63%. Now someone has a money in his hand. He plans to get 1000 yuan by the end of the next five years. By the end of the fifth year, he just finished the money. Please calculate how much he should save.

* Problem analysis and Algorithm Design
You can use the reverse push method to analyze the process of saving and taking money. If you want to pay 1000 yuan for your linked interest at the end of the fifth year, you must first obtain the amount of bank deposits at the beginning of the fifth year:
Deposits at the beginning of the fifth year = 1000/(1 + 12*0.0063)
And so on ...... The amount of bank deposits at the beginning of the year:
Deposits at the beginning of the fourth year = (deposits at the beginning of the fifth year + 1000)/(1 + 12*0.0063)
Deposits at the beginning of the third year = (deposits at the beginning of the fourth year + 1000)/(1 + 12*0.0063)
Deposits at the beginning of the second year = (deposits at the beginning of the third year + 1000)/(1 + 12*0.0063)
Deposits at the beginning of the first year = (deposits at the beginning of the second year + 1000)/(1 + 12*0.0063)
Through the above process, you can easily find the amount of money to be saved at the beginning of the first year.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int I;
Float Total = 0;
For (I = 0; I <5; I ++)/* I is the number of years, and the value is 0 ~ 4 years */
Total = (total + 1000)/(1 + 0.0063*12);/* calculates the cumulative deposit amount at the beginning of the year, the fifth Calculation
The result is an answer */
Printf ("He must save %. 2f at first./N", total );
}

* Running result
He must save 4039.44 at first

14. How to save money to maximize profits

Assume that the monthly interest rates of the entire bank deposit for different periods are:
0.63% term = 1 year
0.66% term = 2 years
0.69% term = 3 years
0.75% term = 5 years
0.84% term = 8 years
Interest = Principal * monthly interest rate * 12 * deposit period.
Now someone has 2000 yuan in his hand. Please select a deposit scheme through computation, this gives the bank the most interest after 20 years of deposit (assuming that the bank does not pay any interest for the period beyond the deposit term ).

* Problem analysis and Algorithm Design
In order to get the most interest, the money deposited in the bank should be obtained immediately upon expiration, and then the original principal and interest should be immediately added up and then saved to the bank as the new principal, this keeps rolling until the last 20 years. Due to the different deposit interest rates, different deposit methods (years) save 20 years to get different interest.
For analysis purposes, 2000 RMB is saved for 20 years, including I1 for 1 year, I2 for 2 years, I3 for 3 years, I5 for 5 years, and i8 for 8 years, the total sum of profits The depositor shall receive upon expiration is:
2000*(1 + rate1) i1 * (1 + rate2) I2 * (1 + rate3) I3 * (1 + rate5) I5 * (1 + rate8) i8
Raten indicates the interest rate corresponding to the deposit period. You can also obtain the following restrictions based on the question:
0 <= i8 <= 2
0 <= I5 <= (20-8 * i8)/5
0 <= I3 <= (20-8 * i8-5 * I5)/3
0 <= I2 <= (20-8 * i8-5 * i5-3 * I3)/2
0 <= I1 = 20-8 * i8-5 * i5-3 * i3-2 * I2
You can use the exhaustive method to enumerate all the combinations of i8, I5, I3, I2, and I1, and then use the formula used to calculate the maximum value, which is the best deposit scheme.

* Program description and comment
# Include <stdio. h>
# Include <math. h>
Int main ()
{
Int i8, I5, I3, I2, i1, n8, N5, N3, N2, N1;
Float max = 0, term;
For (i8 = 0; i8 <3; i8 ++)/* provides all possible deposit methods */
For (I5 = 0; I5 <= (20-8 * i8)/5; I5 ++)
For (I3 = 0; I3 <= (20-8 * i8-5 * I5)/3; I3 ++)
For (I2 = 0; I2 <= (20-8 * i8-5 * I3)/2; I2 ++)
{
I1 = 20-8 * i8-5 * i5-3 * i3-2 * I2;
Term = 2000.0 * POW (double) (1 + 0.0063*12), (double) i1)
* POW (double) (1 + 2*0.0063*12), (double) I2)
* POW (double) (1 + 3*0.0069*12), (double) I3)
* POW (double) (1 + 5*0.0075*12), (double) I5)
* POW (double) (1 + 8*0.0084*12), (double) i8 );
/* Calculate the total sum of profits upon expiration */
If (term> MAX)
{
Max = term; n1 = I1; N2 = I2; N3 = I3; N5 = I5; n8 = i8;
}
}
Printf ("For maxinum profit, He shoshould so save his money in a bank:/N ");
Printf ("made fixed deposit for 8 year: % d times/N", n8 );
Printf ("made fixed deposit for 5 year: % d times/N", N5 );
Printf ("made fixed deposit for 3 year: % d times/N", N3 );
Printf ("made fixed deposit for 2 year: % d times/N", N2 );
Printf ("made fixed deposit for 1 year: % d times/N", N1 );
Printf ("Toal: %. 2f/N", max );
/* Output deposit Method */
}

* Running result
For maxinum profit, He shoshould so save his money in a bank:
Made fixed deposit for 8 year: 0 times
Made fixed deposit for 5 year: 4 times
Made fixed deposit for 3 year: 0 times
Made fixed deposit for 2 year: 0 times
Made fixed deposit for 1 year: 0 times
Total: 8841.01
It can be seen that the best deposit scheme is to deposit for five years for four consecutive times.
* Questions
An organization sells housing for its employees, each of which is 20 thousand yuan. The payment method for purchasing a house is as follows:
One settlement, with a discount of 20%
From the first year, the annual installment payment at the beginning of each year:
5-year settlement, with a 50% discount;
10-year settlement, with a 10% discount;
The service has been paid off for 20 years and there is no discount.
Someone now has 20 thousand yuan in his hand. If the price and bank interest rate remain unchanged for the next 20 years, Ask him which payment method should he choose to minimize the amount of money payable?

15. fishing and fish splitting

Five people A, B, C, D, and E went fishing in partnership one night. They were exhausted in the morning of the next day, so they went to bed. When a wakes up three times a day, he divides the fish into five portions, throwing away the extra fish and taking one of his portions. B wakes up the second and divides the fish into five portions, throwing away the extra fish and keeping them one. C, D, and E woke up in sequence and took the fish in the same way. How many fish have they caught in partnership?

* Problem analysis and Algorithm Design
According to the meaning of the question, all fish are evenly allocated five times. The strategy for each allocation is the same, that is, the remaining fish after throwing away a fish are exactly divided into five portions, take one of your parts and the other four.
Assuming that the total number of fish is X, X can be allocated five times according to the requirements of the Subject: After the X-1 can be 5 divisible, the remaining fish is 4 * (X-1), 5. If X meets the preceding requirements, X is the solution of the question.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int N, I, X, flag = 1;/* flag: Control flag */
For (n = 6; flag; n ++)/* adopts the test method. Increase the test value n step by step */
{
For (x = N, I = 1 & flag; I <= 5; I ++)
If (x-1) % 5 = 0) x = 4 * (x-1)/5;
Else flag = 0;/* if it cannot be allocated, set flag = 0 to exit the allocation process */
If (FLAG) break;/* If the allocation process ends normally, find the result and exit the test process */
Else flag = 1;/* Otherwise, test the next number */
}
Printf ("Total number of fish catched = % d/N", n);/* output result */
}

* Running result
Total number of fish catched = 3121

* Further Discussion
The program uses the test method. The initial value of the test is 6, and the step size of each test is 1. This is an overly conservative approach. You can modify this value based on further analysis of the question, and increase the value of the test step to reduce the number of test attempts.

* Questions
Please use other methods to solve this question.

16. goldfish for sale

 

The buyer and seller sold the goldfish in a tank for five times. The last time the system sold half of the whole, it added 1/2; the second time it sold the remaining 1/3 and 1/3; for the third time, the remaining 1/4 and 1/4 items are sold. for the fourth time, the remaining 1/5 and 1/5 items are sold. For the last time, the remaining 11 items are sold. How many goldfish are there in the original fish tank?

* Problem analysis and Algorithm Design
All the fish in the question are sold for five times, and the strategy for each sale is the same; the remaining (J + 1) points of the second (J + 1) are repeatedly added with 1/(J + 1. In the fifth round, we sold all the remaining 11 items for the fourth round.
Assuming that the total number of times J fish is X, the J fish is left:
X-(x + 1)/(J + 1)
When the fourth sale is complete, there should be 11 remaining items. If X meets the preceding requirements, X is the solution of the question.
Note that "(x + 1)/(J + 1)" must meet the division criteria. The initial value of test X can start from 23, and the test step is 2, because the value of X must be an odd number.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int I, j, n = 0, x;/* n is the flag variable */
For (I = 23; n = 0; I + = 2)/* control the test step and process */
{
For (j = 1, x = I; j <= 4 & x> = 11; j ++)/* four sales operations */
If (x + 1) % (J + 1) = 0)/* If the Division conditions are met, the actual sales operation is performed */
X-= (x + 1)/(J + 1 );
Else {x = 0; break;}/* otherwise, the computing process is stopped */
If (j = 5 & X = 11)/* If the remaining 11 items in the fourth time meet the question */
{
Printf ("there are % d fishes at first./N", I);/* output result */
N = 1;/* control the exit test process */
}
}
}

* Running result
There are 59 fishes at first.

* Questions
Professor Yu Yizhi, a famous Japanese mathematical game expert, raised the following question: his father gave 2520 oranges to six sons. After the split, the father said, "the old man gave you 1/8 of the oranges to the second child. The second child gave the old man 1/7 of the oranges to the third child; after getting the old Three, they scored 1/6 yuan for the old four, and 1/5 yuan for the old four. After getting the old five, they scored 1/4 yuan for the old six; after getting the old six, they allocated 1/3 yuan to the boss together with the original oranges ". As a result, there are just as many oranges in your hands. How many oranges did the six brothers have in their hands?

17. Divide the fish into seven baskets

A, B, and C fish went out to sea to fish. They carried 21 baskets along with the ship. When they returned that night, they found that seven baskets were full of fish, and seven baskets were half baskets of fish. The other seven baskets were empty because they had no scales, I had to think through visual inspection that the weight of the seven full-basket fish is equal, and the weight of the seven half-basket fish is equal. How can we divide the fish and the basket into three portions without dumping them out?

* Problem analysis and Algorithm Design
According to the question, you can know that each person should have seven baskets, of which 3.5 are baskets of fish. A 3*3 array, a, is used to indicate the items assigned by three people. Each person corresponds to a row of array A, the number of fish baskets in column 0th of the array, and the number of half baskets in column 1st of the array, the number of empty baskets to which 2nd columns of the array are placed. Questions can be introduced:
. The sum of each row or element in each column of the array is 7;
. For the rows in the array, the number of full baskets plus the number of half baskets = 3.5;
. Each person's income cannot exceed 3 baskets;
. Each person must have at least one and a half baskets, and the number of half baskets must be odd.
For a fish splitting solution, which of the three Members take is the same. To avoid repeated allocation schemes, you can specify: the number of baskets of the second person is equal to the number of baskets of the first person; the number of half baskets of the second person is greater than or equal to the number of half baskets of the first person.

* Program description and comment
# Include <stdio. h>
Int A [3] [3], count;
Int main ()
{
Int I, J, K, M, N, flag;
Printf ("It exists possible distribtion plans:/N ");
For (I = 0; I <= 3; I ++)/* test the value of a [0] [0] In the first person's basket. The number of baskets cannot exceed 3 */
{
A [0] [0] = I;
For (j = I; j <= 7-i & J <= 3; j ++)/* test the value of a [1] [0] In the second basket, the number of baskets cannot exceed 3 */
{
A [1] [0] = J;
If (A [2] [0] = 7-j-a [0] [0])> 3) continue;/* The number of third persons in full baskets cannot exceed 3 */
If (A [2] [0] <A [1] [0]) break;/* requires the number of baskets for the next person to be divided> = the previous person to exclude duplicates */
For (k = 1; k <= 5; k + = 2)/* test the value of Half basket a [0] [1]. The number of half baskets is odd */
{
A [0] [1] = K;
For (M = 1; m <7-k; m + = 2)/* test the value of Half basket A [1] [1]. The number of half baskets is odd */
{
A [1] [1] = m;
A [2] [1] = 7-k-m;
For (flag = 1, n = 0; flag & n <3; n ++)
/* Determine that each person is allocated 3.5 baskets of fish, and the flag is the variable that matches the question */
If (a [n] [0] + a [n] [1] <7 & a [n] [0] * 2 + a [n] [1] = 7)
A [n] [2] = 7-A [N] [0]-a [n] [1];/* calculate the number of empty baskets */
Else flag = 0;/* Indicates 0 if the question does not match */
If (FLAG)
{
Printf ("No. % d full basket semi-basket empty/N", ++ count );
For (n = 0; n <3; n ++)
Printf ("Fisher % C: % d/N ",
'A' + n, a [n] [0], a [n] [1], a [n] [2]);
}
}
}
}
}
}
* Running result
It exists possible distribution plans:
No.1 full basket semi-basket empty
Fisher A: 1 5 1
Fisher B: 3 1 3
Fisher C: 3 1 3
No. 2 full basket semi-basket empty
Fisher A: 2 3 2
Fisher B: 2 3 2
Fisher C: 3 1 3

* Questions
At the conference, mathematicians had a problem: Suppose there are three bottles of beer on the table, and the wine in the bottle is given to several people, but the number of people who drink Different bottles of wine is different. However, one of them drank the wine in each bottle, and the total was just a bottle. How many of them were there?
(Answer: the number of people who drink three bottles of wine is 2, 3, and 6 respectively)

 

18. Limited 5-digit

What is the total number of five-digit division with a single digit of 6 and three?

* Question analysis and Algorithm Design
According to the question, the five-digit range is 10006 and 10016... 99996. The number of basic settings I = 1000. After I * 10 + 6 is calculated, you can obtain the number to be selected (the range of I is 1000 ~ 999), and then judge whether the number can be divided by 3.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Long int I;
Int COUNT = 0;/* count: count the number of five digits that meet the condition */
For (I = 1000; I <9999; I ++)
If (! (I * 10 + 6) % 3)/* determines whether the selected number can be divisible by 3 */
Count ++;/* count if the condition is met */
Printf ("Count = % d/N", count );
}

* Running result
Count = 2999

 

* Questions
Calculate the number of digits between 100 and 1000 and the sum of them is an integer of 5.
(Answer: 104,113,122,131,140,203,212,221,230,302,311,320,401,410,500)

19.8 Division of Natural Numbers

A natural number is divided by 8 to more than 1, and the resulting quotient is divided by 8 to more than 1, then the second quotient is divided by 8 to more than 7, and finally a quotient is obtained as. It is also known that this natural number is divided by 17 to 4, the resulting quotient is divided by 17 to 15, and the final result is a quotient of 2 times that of. Calculate the natural number.

* Problem analysis and Algorithm Design
Based on the question, you can set the final quotient to I (I starts from 0), and use the inverse method to list the relational expressions:
(I * 8 + 7) * 8) + 1) * 8 + 1 = (2 * I * 17) + 15) * 18 + 4
Use the test method to obtain the quotient I value.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int I;
For (I = 0; I ++)/* test operator value */
If (I * 8 + 7) * 8 + 1) * 8 + 1 = (34 * I + 15) * 17 + 4)
{/* Determine whether the obtained current I value meets the relational expression through reverse push */
/* If yes, the output result */
Printf ("the required number is: % d/N", (34 * I + 15) * 17 + 4 );
Break;/* exit loop */
}
}

* Running result
The required number is: 1993

20. A singular three-digit number

 

The hexadecimal expression of a natural number is a three-digit number, and the nine-digit expression of this natural number is also a three-digit number. The two three-digit numbers are the opposite.

* Problem analysis and Algorithm Design
According to the meaning of the question, each digit of the all natural numbers in the hexadecimal and nine hexadecimal forms must be less than 7, it can be set to kji in the hexadecimal format (values of I, j, and K are 1 ~ 6), and then set its hexadecimal representation to ijk.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int I, J, K;
For (I = 1; I <7; I ++)
For (j = 0; j <7; j ++)
For (k = 1; k <7; k ++)
If (I * 9*9 + J * 9 + k = I + J * 7 + K * 7*7)
{
Printf ("the special number with 3 digits is :");
Printf ("% d (7) = % d (9) = % d (10)/n", K, J, I, I, j, K, I * 9*9 + J * 9 + k );
}
}

* Running result
The special number with 3 digits is: 503 (7) = 305 (9) = 248 (10)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.