C + + 4th experiment (Basic Class)-cyclic structure programming

Source: Internet
Author: User
Tags arithmetic ming

The 4 topics on the Aircraft 6, Project 7 (1) will do. The other 22 questions can be selected from the remaining items, or from item 7.

"Item 1: Summing with loops" to find all even numbers within 1000 (answer: 250500)

Requirements: Please make up 3 programs, respectively, with three kinds of circular statements completed, pay attention to experience the operation of various circular statements and grammatical characteristics.

"Item 2-Accumulation of fractions" program, output 1/3-3/5+5/7-7/9...+19/21 results (answer:-0.383842)
Tip: Assuming that the problem is directly solved above, can design a "from easy to difficult" route, gradually solve the problem to be solved. Let your ideas clear up.


(1) 1+2+...+20--this should be
(2) Accumulation of 1+1/2+1/3+...+1/20--fractions. Note that two integers are divided and the quotient is integral, and it is clear that the sum result should be a decimal
(3) 1/2+2/3+3/4+...+19/20--molecules are not all 1, look for the law, a little modification on the good
(4) 1/2-2/3+3/4-... +19/20--to accumulate the value of a positive and negative daoteng. The use of POW ( -1,i) is a very inefficient practice and is not recommended.

Tip: Set a variable s to represent the symbol of the additive, multiply the value with the loop, multiplied by-1 each time. Thus changes in the + 1,-1, the summation of the sum of the loops and sums to be multiplied by the item (i/(I+1)) times the symbol of the S.


(5) 1/3-3/5+5/7-7/9...+19/21--This is our goal.

"Item 3: Multiplication table" programming. Output a multiplication table, shaped like
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
......

"Item 4: Output Count" A number hypothesis is exactly equal to the sum of its factors, and this number is called the "end number".

Like 6=1+2+3. Again such as 8 of the factor and is 7 (that is, 1+2+4), 8 is not the end number. Programming to find out all the numbers within 1000. (Answer: 6 28 496)
Tip: First, from 2 to 1000 constructs the loop control variable to the outer loop of I. In each loop. The factor of I is calculated one by one using an inline loop and added up (for efficiency, the possible factor is from 1 to I/2). Assuming the factor and equals I, the description is total number, output.

Then continue to cycle, investigate i+1 ...

"Item 5: Greedy Millionaire" A Millionaire met a stranger. The stranger talked to him about a plan to change money, for example, the following: I give you 100,000 yuan a day. And you just need to give me a penny on the first day. The next day I still give you 100,000 yuan, you give me two cents, the third day I still give you 100,000 yuan, you give me four cents, ..., you give me the money is twice times the day before, until full one months (30 days), the millionaire was very happy, gladly accepted the contract. Please compile the program to see if the money plan is a bargain for millionaires. (Answer: To the rich: 3e+006, Rich to Mo: 1.07374e+007 rich lose)
Tip: (1) It is necessary to calculate how much money a stranger gives to a millionaire after 30 days, how much money a millionaire gives to strangers, and then the ability to make inferences; (2) want to see clearly, can choose to list each day, two-party transactions to obtain the amount. (3) Give a reference to the answer, the daily accumulated to each other's money listed. Very intuitive.



"Item 6: Output asterisk graph" program output.

The project7: Poor lifting method solves combinatorial problems "(of course, all done better)

First read the examples, understand the poor lifting method (meaning "Exhaustive enumeration", also known as enumeration) of the idea, and then self-selected topic to solve, master the general method of the program design.

example: Xiao Ming has five new books. To lend a b c three children, if each person can only borrow a copy, how many different kinds of borrowing law?

Problem analysis and algorithm design: This problem is actually a permutation problem. That is, the total number of methods to arrange from 5 to 3 is obtained . First, five books are numbered from 1 to 5 . Then use the exhaustive method.

If three people borrow one of these five books, when the number of books borrowed by three people is not the same, it is a kind of borrowing method that satisfies test instructions.

Here are the procedures and their gaze, which should be paid attention to using the triple cycle "exhaustive":

#include <iostream> using namespace Std;int main () {int a,b,c,count=0;cout<< "Xiao Ming borrows books for three children:" << Endl;for (a=1;a<=5;a++)//Exhaustive a Lend 5 books in all cases of 1 for (b=1;b<=5;b++)//Poor lift B borrows 5 books in all cases for (c=1;c<=5;c++)// Exhaustive c borrows 5 books in all cases of 1 if (a!=b&&c!=a&&c!=b)//Infer whether the books borrowed by three are different, (A-B) * (B-C) * (c-a)!=0 better {++count;cout<< count<< ":" <<a<< "," <<b<< "," <<c<<endl;//output scheme}return 0;}

Task: Use the poor lifting method to solve the following problems (choose to do a complete task, others can take the time to freely arrange. Doing more will make you smarter. )

(1) Hundred money Hundred chicken question: the ancient Chinese mathematician Zhang Chujian in his "calculates the Sutra" to put forward the famous "hundred money buys Hundred chicken question": The chicken Weng One, the value five, the chicken mother one. Worth three, chickens three. Worth one, hundred money to buy hundred chickens, ask Weng, mother, young each geometry?

Hint: Set the number of chicken Weng, chicken mother, chickens is x, y, Z, test instructions given a total of 100 money to buy hundred chickens, if all buy cocks up to buy 20 only, obviously the value of x is between 0~20; Similarly, the value of y ranges between 0~33. The following indefinite equations can be obtained:

5x+3y+z/3=100

x+y+z=100

So this problem can be attributed to the integer solution of the indefinite equation.

Answer:

Chicken Weng 0 Only, chicken Mother 25 just. Chickens 75 just.


Chicken Weng 4 Only, chicken Mother 18 just. Chickens 78 just.
Chicken Weng 8 Just. Chicken Mother 11 Just, chickens 81 just.
Chicken Weng 12 Just. Chicken Mother 4 Just, chickens 84 just.

(2 " Age geometry: Zhang San, John Doe, Harry, Liu Liu age into one arithmetic progression. The age of the four is added to 880 4 " Span style= "font-family: Song body; margin:0px; padding:0px "> arithmetic progression of the previous 20 " Span style= "font-family: Song body; margin:0px; padding:0px "> item.

Hint: The first item of the set sequence is N, Xiangchao is a, then the sum of the first 4 items is n+ (n+a) + (N+a+a) + (N+a+a+a) =4*n+6*a ", the product of the first 4 items is n (n+a) * (n+a+a) * (N+a+a+a). At the same time there are 1<=a<=4 and 1<=n<=6. This series can be obtained by the method of poor lifting.

Answer: 2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59

(3) tri-color ball problem: If a pocket has Aa ball. Among them are3It 's a red one. 3a white and6a black one, ask to take it from the8How many different colors do you have in common with each other?

Hint: The number of red balls set is I, the number of white balls is J, then the number of black balls is 8-i-j. According to the number of test instructions red ball and white ball range is 0~3, in the number of red ball and white ball determined, the number of black ball should be 8-i-j<=6.

Answer:

The different color combinations are:
Red ball: 0, White ball: 2, Black ball: 6
Red ball: 0, White ball: 3, Black ball: 5
Red ball: 1, White ball: 1, Black ball: 6
Red ball: 1, White ball: 2, Black ball: 5
Red ball: 1, White ball: 3, Black ball: 4
Red ball: 2, White ball: 0, Black Ball: 6
Red ball: 2, White ball: 1, Black ball: 5
Red ball: 2, White ball: 2, Black ball: 4
Red ball: 2, White ball: 3, Black ball: 3
Red ball: 3, White ball: 0, Black Ball: 5
Red ball: 3, White ball: 1, Black ball: 4
Red ball: 3, White ball: 2, Black ball: 3
Red ball: 3, White ball: 3, Black ball: 2

(4) in the following addition equations, different symbols represent different numbers. The same symbol represents the same number.

Please design a program to find the " all, want, learn,C" 4 symbols represented by the number.


Tip: Let the computer solve math problems. The numbers represented by the "All, want, Learn, C" 4 symbols respectively (from 0 to 9). Then combine them, assuming they are combined to match the rules (different symbols represent different numbers.) The same symbol represents the same number, and the equation is true, the positive solution.

Answer:

All: 1 To: 4 Learn: 6 C:7
All: 1 To: 5 Learn: 0 C:2

(5) have an equation[※x(※ All※)]^2=8※※9, where ※ is1a number, and the ink was not recognizable. Please program to find out which number is indicated by ※.

Expansion: There is an equation [※x (※30※)]^2=8※※9. The ※ Place is 1 numbers, 0 is + 、-、 x, ÷ four operators of one, drip ink is not recognizable.

Please program to find out which number, 0 indicates which operator.

The answer to the exam

The equation is: [1x (93+0)]^2=8649
The equation is: [3x (23+8)]^2=8649

C + + 4th experiment (Basic Class)-cyclic structure programming

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.