Title: Now there are 1 points, 2 points, 5 cent coins total 100, the total amount of 2.46 yuan, please use the program to calculate 1 points, 2 points, 5 points each has how many, how much algorithm?
This is a recent interview encountered a topic, just start without thinking, a look at this is a three-dimensional equation group, assuming that the number of a, B, C, there will be an unknown and two groups of equations, how to solve? Maths is really a white study!
So I found this topic on the Internet, but the topic is slightly different:
Files: Program1.cs
//question: 1 minutes, 2 minutes, 5 cents, a total of 2.46 yuan, to find the number of coins//Answer: You did not specify whether to ask for the minimum number of coins, because there are thousands of composition, so I will write you a console, ask for the minimum number of coins itusingSystem;namespaceconsoleapplication1{classProgram1 {Static voidMain (string[] args) { intMoney =246;//Total 246 points intNumber5;//Number5 The number of coins that require 5 cents intnumber2;//number2 The number of coins that require 2 cents intNumber1;//number1 The number of coins that require 1 centsNumber5 = Money/5; Number2= (Money%5) /2; Number1= Money%5%2; Console.WriteLine ("need:"+"\ n"+"5-cent Coin:"+ Number5 +"a"+"\ n"+"2-cent Coin:"+ Number2 +"a"+"\ n"+"1-cent Coin:"+ Number1 +"a"); Console.ReadLine (); } }}
Operation Result:
According to this buddy's train of thought, set three unknowns this point is the same as mine, and he gives me the inspiration to have the following points:
- Setting 246 also to a variable money is reflected in the code, so I can also set 100 as a variable, the advantage is that the number of coins changed to 1000 or even 10000 of cases.
- He used money as a variable, by taking the remainder and division, completely independent of the value of three variables, the revelation of my 5 cents in the number of coins, the number of 5 coins can be completely independent of the number of 1 points and 2 cent coins.
- Using the For loop to traverse 5 cent of the number of coins is another buddy gave me the inspiration, since there are only 2 equations 3 unknowns of the ternary one-time equation, think about the answer may be more than one, that gives me the first idea is to use the "test" method, first a coin may appear the number of "substituting" in, Fortunately, it is found that two equations can be added and reduced, eliminating one of the unknowns, can do the above mentioned, with one of the number of coins completely independent expression of the other two coins.
/* A + b + c = +; A + 2b + 5c = 246; b = 246-100-4c; A = 3c-246 + 2*100; */
Files: Program2.cs
usingSystem;namespaceconsoleapplication2{classProgram2 {Static voidMain (string[] args) { intMoney =246;//Total 246 points intAmount = -; intNumber5;//Number5 The number of coins that require 5 cents intnumber2;//number2 The number of coins that require 2 cents intNumber1;//number1 The number of coins that require 1 cents intCount = 0; for(inti =0; I < money/5; i++) {Number5=i; Number2= Money-amount-4*Number5; Number1=3* Number5-money +2*amount; if(Number2 >=0&& Number1 >=0) {Console.WriteLine ("5 points: {0}, 2 points: {1}, 1 points: {2}", Number5, Number2, number1); Count++; }} Console.WriteLine ("A total of {0} algorithms ...", Count); Console.ReadLine (); } }}
Calculation Result:
To this end, if the above process has any errors or who have a better way, hope to see the friends of the liberal enlighten, a lot of guidance!
Interviewquestion_c#_probl_ calculates 1 minutes, 2 minutes, 5 cents, how many pieces are there?