当前位置:网站首页>Tencent interview: can you pour water?
Tencent interview: can you pour water?
2022-07-03 23:24:00 【Tao song remains the same】
Hello everyone , I'm brother Tao .
today , Let's look at a very interesting Tencent interview question , Pupils can understand , But it's not easy to get it right . Let's have a look at the topic , That's interesting :
Yes A and B Two cups and enough water , The cup capacity is 9 L and 15 l , Try to judge , Can you measure n Liters of water , If you can , Please program the steps .

Hand painted by Taoge
What does this topic mean ? Let me explain :
(1) Fill it up first A, Refill B, here A Yes 9 l ,B Yes 15 l . obviously , This measures 24 Liters of water . To put it bluntly , Namely A + B = 24.
(2) Fill it up first A, And then A Pour all the water in B in , here A It's empty ,B Yes 9 l . Then fill it up A, Keep on putting A Pour the water in B in , until B Until it's full , here A be left over 3 l . obviously , This measures 3 Liters of water . To put it bluntly , Namely 2A - B = 2 * 9 - 15 = 3.
One . logic analysis
set up A = 9,B = 15, We can equate these two cups to A = 9,B = 6, Continue to be equivalent to A = 3,B = 6, Continue to be equivalent to A = 3,B = 3, in other words , In the end, it's actually equivalent to having a 3 L Cup . therefore , The water that can be poured out must be 3 Multiple .
The above equivalence analysis process , In fact, it is the process of finding the greatest common divisor . therefore , We can get the law :A L cup and B L Cup , The measurable water is k * gcd(A,B) l . among K Is a nonnegative integer ,gcd Represents the greatest common divisor . therefore , The judgment function of the original question is :
#include <iostream>
using namespace std;
int gcd(int x, int y)
{
int z = y;
while (x % y != 0)
{
z = x % y;
x = y;
y = z;
}
return z;
}
bool can(int A, int B, int n)
{
if (n % gcd(A, B) == 0)
{
return true;
}
return false;
}
int main()
{
for(int n = 0; n < 20; n++) // test
{
if (can(9, 15, n) )
{
cout << n << endl;
}
}
return 0;
}The result is :
0
3
6
9
12
15
18
Two . Pouring steps
Now that we know what meets the conditions n Value , So how to use A and B Cup measurement n Liters of water ? Might as well set A < B, So when n >= A when , We can always start from n Constantly subtract A, Until it's not enough . What does that mean ?
In fact, it is aimed at any n value , We can always turn it into n < A < B Questions like this , such as :A = 9,B = 15,n = 33, We can equivalent to :A = 9,B = 15,n = 33 - 9 - 9 - 9 = 6. below , Look at the program :
#include<iostream>
using namespace std;
void pour(int A, int B, int target)
{
//inA, inB respectively A,B Amount of reclaimed water
int inA = 0;
int inB = 0;
int flag = 0;
while(1)
{
// Suppose that the pouring operation can be carried out in a certain while Complete within the number of cycles
if(flag++ > 999)
{
cout << "Fail" << endl;
break;
}
if(0 == inA)
{
cout << "fill A" << endl;
inA = A;
}
else
{
cout << "pour A into B" << endl;
inB += inA;
if(inB >= B)
{
inA = inB - B;
cout << "empty B" << endl;
inB = 0;
}
else
{
inA = 0;
}
}
if(target == inA || target == inB)
{
cout << "Success, OK!" << endl;
break;
}
}
}
int main()
{
int A = 9;
int B = 15;
int n = 6;
// Have proved , Any A,B,n In this topic, it can be converted into the following form
// namely :0 <= n < A < B
pour(A, B, n);
return 0;
}The result is :
fill A
pour A into B
fill A
pour A into B
empty B
pour A into B
fill A
pour A into B
fill A
pour A into B
empty B
Success, OK!
This Tencent interview question is not difficult , The key lies in logical analysis , As for programming , Just let it be 、 What happens naturally .
I hope you can draw one example from another , The written interview went well . It's Friday again , Did you feel the darkness before dawn ? I'll see you tomorrow Saturday .
边栏推荐
- [Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
- NPM script
- [issue 16] golang's one-year experience in developing Purdue Technology
- Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
- How can enterprises and developers take advantage of the explosion of cloud native landing?
- D24:divisor and multiple (divisor and multiple, translation + solution)
- Go error collection | talk about the difference between the value type and pointer type of the method receiver
- Summary of fluent systemchrome
- Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
- Gorilla/mux framework (RK boot): add tracing Middleware
猜你喜欢

How to quickly build high availability of service discovery

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

How to solve the problem of computer networking but showing no Internet connection

User login function: simple but difficult

Unsafe and CAS principle

Alibaba cloud container service differentiation SLO hybrid technology practice

Bufferpool caching mechanism for executing SQL in MySQL

Runtime. getRuntime(). totalMemory/maxMemory()

Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
随机推荐
ADB related commands
File copy method
D29:post Office (post office, translation)
炒股开户佣金优惠怎么才能获得,网上开户安全吗
IDENTITY
Alibaba cloud container service differentiation SLO hybrid technology practice
. Net ADO splicing SQL statement with parameters
How to prevent malicious crawling of information by one-to-one live broadcast source server
Current detection circuit - including op amp current scheme
540. Single element in ordered array
NPM script
JarPath
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Unity shader visualizer shader graph
How to quickly build high availability of service discovery
股票開戶傭金最低的券商有哪些大家推薦一下,手機上開戶安全嗎
Blue Bridge Cup -- Mason prime
2022.02.14
Design of logic level conversion in high speed circuit