measure c litre using a litre and b litre (algorithm) [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Suppose there are two jugs of a litre and b litre and i have to measure c litre by using the and b i cannot measure c if c % gcd(a,b)!=0.For example if a=21 and b=27 ,I cant't measure 10 litre . Can someone explain the intuition behind this?
Thanks in advance
AUTOCORRECT
This was the question
https://www.codechef.com/problems/POUR1
This was the accepted code
See the statement in main if (c % __gcd(a, b) || c > max(a, b))
He is printing -1 as this is not possible
#include<bits/stdc++.h>
using namespace std;
//int aa,bb,cc;
int process(int a, int b, int c)
{
int sum = 1, aa = a, bb = 0;
while (a != c and b != c) {
int f = min(aa, b - bb);
bb += f;
aa -= f;
sum++;
if (aa == c || bb == c)
break;
if (aa == 0)
aa = a, sum++;
if (bb == b)
bb = 0, sum++;
}
return sum;
}
main()
{
int t, a, b, c;
cin >> t;
while (t--) {
cin >> a >> b >> c;
//cerr << (c % __gcd(a,b)) <<'\n';
//int aa = a , bb = b;
if (c % __gcd(a, b) || c > max(a, b))
cout << -1 << '\n';
else if (c == a || c == b)
cout << 1 << '\n';
else
cout << min(process(a, b, c), process(b, a, c)) << '\n';
}
}

Bezout's Identity:
Let a and b be integers with greatest common divisor d. Then, there exist integers x and y such that ax + by = d. More generally, the integers of the form ax + by are exactly the multiples of d. (Wikipedia)
a and b here correspond to the jugs, x and y to the number of times they are used.
This proves not only that c must be a multiple of the gcd, but also that if c fulfils this condition, then a solution exists.

A and B are both multiplied by their GCD
Thus you can be sure that after any number of transfer, both the jars will contain a number divisible by the GCD. If C is not divisible by the GCD, that means it cannot be obtained by any combination of transfers.

Related

there is a question to find middle number . i tried really hard [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
I need to find middle number from 3 numbers the user inputs. I'm completely new to C++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c, d, mdq, mnq, mxq, mdw, mnw, mxw, mde, mne, mxe;
cin>>a>>b>>c;
mda = c;
if (mda>a and mda<b) mdq = c;
if (mda>a and mda>b) mxa = c;
if (mdq<a and mdq<b) mnq = c;
mdw = a:
if (mdw>b and mdw<c) mdq = a;
if (mdw>b and mdw>c) mxg = a;
if (mdw<b and mdw<c) mnq = a;
mde = b;
if (mde>a and mde<c) mdq = b;
if (mde>a and mde>c) mxq = b;
if (mde<a and mde<c) mnq = b;
d = mdq+mxq+mng:
d = d-mxq-mnq;
cout<<d;
}
I tried to find middle number and it sometimes work but whenever i type 4 7 1 the output is 1.
I think you are trying far too hard. There are only six possibilities, you don't need complicated logic and you definitely don't need mathematics.
#include <iostream>
int main()
{
int a, b, c;
std::cin >> a >> b >> c;
// it's a if b <= a <= c OR c <= a <= b
if ((b <= a && a <= c) || (c <= a && a <= b))
std::cout << "first # " << a << '\n';
// it's b if a <= b <= c OR c <= b <= a
else if ((a <= b && b <= c) || (c <= b && b <= a))
std::cout << "second # " << b << '\n';
// neither of the above, must be c
else
std::cout << "third # " << c << '\n';
}
I suppose the only tricky part is how to deal with equal numbers. I used <= which means given equal numbers the earlier number will be printed. So for example given 11 22 22 the output is second # 22 not third # 22.
Consider:
#include <algorithm>
#include <iostream>
int main()
{
int a, b, c ;
std::cin >> a >> b >> c ;
if( a > b ) std::swap( a, b ) ;
if( b > c ) std::swap( b, c ) ;
if( a > b ) std::swap( a, b ) ;
std::cout << b << '\n' ;
return 0;
}
It sorts the three values in order so that the answer is always b.
An alternative that does not modify the input:
#include <algorithm>
#include <iostream>
int main()
{
int a, b, c ;
std::cin >> a >> b >> c ;
int max1 = std::max( a, b ) ;
int max2 = std::max( b, c ) ;
int max3 = std::max( a, c ) ;
int mid = std::min( max1, max2 ) ;
mid = std::min( mid, max3 ) ;
std::cout << mid << '\n' ;
return 0;
}
which uses just four additional variables rather than ten in your attempt. It works by determining the maximum of all possible pairs: a,b, b,c and a,c, giving three values that contain either the largest or second largest value - the second largest value of three is also the middle value of course, so the smaller of these three maxima is the middle value.
You need not rely on the min()/max() functions here - they can be replaced with for example the expressions a < b ? a : b ; and a > b ? a : b or even if( a > b ) max1 = a; else max1 = b; etc.
Perhaps a more obvious solution, given that there are only 6 possible orderings of a b c, and only three possible outcomes for the middle value, then you can explicitly test for each possibility. You only need to explicitly test for two possible outcomes, since if neither of those are true, the answer is the third possibility:
#include <iostream>
int main()
{
int a, b, c;
std::cin >> a >> b >> c ;
int mid = a ; // Initially assume a is the middle
// (i.e. order: b a c or c a b)
// If b is in the middle...
if( (a <= b && b <= c) || // a b c or...
(c <= b && b <= a) ) // c b a
{
mid = b ;
}
// else if c is in the middle
else if( (a <= c && c <= b) || // a c b or...
(b <= c && c <= a) ) // b c a
{
mid = c ;
}
std::cout << mid << '\n' ;
return 0 ;
}
This is essentially #john's solution (credit), but you may find the explanatory comments here easier to follow perhaps.

How would I make it so if any number bigger than 10 or less than 10 it would say "wrong answer"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to make a code so if the answer is less than 10 or more than 10 it says "wrong answer" I just testing somethings right now I'm very new to coding.
right now I have:
#include <iostream>
#include "log.h"
int main()
{
MultiplyAndLog2(5, 2); // this is what is going to be multiplied
int x = 11;
if (x == 10) // I have it so I if it equal to 10 it says right answer
Log("right answer");
if (x == ) // Heres where im stuck I dont know what to add if any number other than 10 is the answer
Log("wrong answer");
std::cin.get();
}
Heres my log.h its a bit messy...
#pragma once
int Multiply(int a, int b, int c) // this is so I can multiply 3 integers at a time just for testing.
{
return a * b * c;
}
int Multiply(int a, int b) // this is the same thing but for 2 integers at a time
{
return a * b;
}
void MultiplyAndLog(int a, int b, int c) // this is so that whatever 3 integers are multiplied it would say answer: and then the answer to the question
{
int result = Multiply(a, b, c);
std::cout << "answer:" << result << std::endl;
}
void MultiplyAndLog2(int a, int b) // this is so that is 2 integers are multiplied it would say answer: and then the answer to the question
{
int result = Multiply(a, b);
std::cout << "answer:" << result << std::endl;
}
void Log(const char* message) // This is so if I type Log I can write whatever I want for example "right answer"
{
std::cout << message << std::endl;
}
Thank you,
Mario
You can use != operator to check if two values are not equal:
if (x != 10)
Log("wrong answer");
Another way is using ! operator to negate the logic:
if (!(x == 10))
Log("wrong answer");
You can also directly code "if the answer is less than 10 or more than 10":
if (x < 10 || x > 10)
Log("wrong answer");
Or better way:
if (x == 10) {
Log("right answer");
} else {
Log("wrong answer");
}

I am new to programming [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
https://www.urionlinejudge.com.br/judge/en/problems/view/1047
I cant find error anyone can help me ?!
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,h,m;
cin>>a>>b>>c>>d;
h = c-a;
m = d-b;
if (h<0)
{
h=24+(c-a);
}
if(m<0)
{
m=60+(d-b);
h--;
}
if(a==c && b==d)
{
cout<<"O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)"<<endl;
}
else
{
cout<<"O JOGO DUROU "<<h<<" HORA(S) E "<<m<<" MINUTO(S)"<<endl;
}
return 0;
}
Convert all to minutes before subtracting the end time for the start time.
If the end time is smaller than the start time, it means it started one day and ended another. This case is handled in the if statement.
#include<iostream>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
int start_time = a * 60 + b;
int end_time = c * 60 + d;
int duration = end_time - start_time;
if(start_time >= end_time) duration = (24 * 60 - (start_time - end_time));
cout<<"O JOGO DUROU "<<(duration / 60)<<" HORA(S) E "<<(duration % 60)<<" MINUTO(S)"<<endl;
}
One data set that will cause an issue, starting at 10:15 and finishing at 10:14 the following day (or, I suspect, any duration between 23:01 and 23:59).
Running those values through you code (slightly reduced and reformatted, but functionally the same):
cin >> a >> b >> c >> d; // a=10, b=15, c=10, d=14
h = c - a; // h=0
m = d - b; // m=-1
if (h < 0) // false
h = 24 + (c - a);
if (m < 0) // tru
m = 60 + (d - b); // m=59
h--; // h=-1, that's not right :-)
You can fix that quite easily just by doing the "minute less than zero" processing first. That way, the hour will never end up at -1:
if (m < 0) {
m += 60
h--;
}
if (h < 0) {
h += 24
}
if (h == 0 && m == 0) {
h = 24;
}
cout << "O JOGO DUROU " << h << " HORA(S) E " << m << " MINUTO(S)" << '\n';
You'll see I've also simplified some of the assignments (with +=) and removed the need for two separate cout statements by adjusting 0, 0 to become 24, 0.

GCD of Multiple Number

I know how to write a code finding a GCD of 2 number . However, I am trying to solve a problem of finding a GCD of n number and I think the algorithm is a little bit different than using an Eucledian algorithm. My code can be compiled , but it always gave me the wrong result. For example when i put n = 2 , GCD of 16 and 12 it gave the answer 8. Here is my code :
#include<iostream>
using namespace std;
int main()
{
int a,b[100],c,d,e=0;
cin>>a;
for(c=0 ; c<a ; c++)
{
cin>>b[c];
}
for(c=0 ; c<a-1 ; c++)
{
if(c < 1)
{
d = b[c];
}
if(b[c] < d)
{
d = b[c];
}
}
while(d>0)
{
for(c=0 ; c<a ; c++)
{
if(b[c] % d < 1)
{
e++;
}
}
if(e == c)
{
cout<<d;
break;
}
d--;
}
}
Can you guys please find the mistake in my code?
Your code does not compute the greatest common divisor of the input array - it counts how many of the entries are evenly divisible by the smallest element d of the array, then how many are divisible by one smaller, and so on until d is 0. This has nothing to do with the GCD at all.
One easy way - though not necessarily the fastest - would be based on the fact that the GCD of three numbers must be the same as the GCD of any one of those numbers and the GCD of the other two.
gcd(a, b, c) = gcd(gcd(a, b), c) = gcd(a, gcd(b, c)) = gcd(gcd(a, c), b)
Extension to n inputs is elementary:
int result = a[0];
for (int i = 1; i < a.Length; ++i)
result = gcd(result, a[i]);
Code for the GCD of two numbers can be found all over the 'net, for example at Rosetta Code. One of my favourites is this plain iterative version:
int gcd (int a, int b)
{
while (b)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
C# allows a more succinct formulation but in other languages this probably won't work (in C++ it would invoke undefined behaviour, for example):
static int gcd (int a, int b)
{
while (b != 0)
b = a % (a = b);
return a;
}
In case some find it helpful, here is an implementation of the Euclidean algorithm in JavaScript.
function EuclideanGCD(a, b) {
// Make sure a > b, interchange values
if (a < b) {
c = a;
a = b;
b = c
}
// If A = 0 then GCD(A,B) = B and we can stop.
if (a == 0) {
return b;
// If B = 0 then GCD(A,B) = A and we can stop.
} else if (b == 0) {
return a;
} else {
let gdc = 0;
let quotient = Math.floor(a / b); // Get the divisor
let remainder = a % b; // Get the remainder
// Make a recursive call, till we hit 0
gdc = EuclideanGCD(b, remainder);
return gdc;
}
}
var gcd = EuclideanGCD(234, 357);
console.log(gcd); // Outputs: 3

Finding MAX of numbers without conditional IF statements c++ [duplicate]

This question already has answers here:
Mathematically Find Max Value without Conditional Comparison
(18 answers)
Closed 9 years ago.
So i have too get two numbers from user input, and find the max of the two numbers without using if statements.
The class is a beginner class, and we have too use what we already know. I kinda worked something out, but it only works if the numbers are inputted with the max number first.
#include <iostream>
using namespace std;
int main()
{
int x = 0, y = 0, max = 0;
int smallest, largest;
cout << "Please enter 2 integer numbers, and i will show you which one is larger: ";
cin >> x >> y;
smallest = (x < y == 1) + (x - 1);
smallest = (y < x == 1) + (y - 1);
largest = (x < y == 1) + (y - 1);
largest = (y > x == 1) + (x + 1 - 1);
cout << "Smallest: " << smallest << endl;
cout << "Largest: " << largest << endl;
return 0;
}
Thats what i have so far, but after putting different test data in, i found out it only works for numbers such as 4,5 or 6,7. But numbers with more then 2 spaces between eachother they dont such as, 4,8 or 5, 7. Any help would be appreciated.
I saw this question in Cracking the Coding interview book.
Let’s try to solve this by “re-wording” the problem We will re-word the problem until we get something that has removed all if statements
Rewording 1: If a > b, return a; else, return b
Rewording 2: If (a - b) is negative, return b; else, return a
Rewording 3: If (a - b) is negative, let k = 1; else, let k = 0 Return a - k * (a - b)
Rewording 4: Let c = a - b Let k = the most significant bit of c Return a - k * c
int getMax(int a, int b) {
int c = a - b;
int k = (c >> ((sizeof(int) * CHAR_BIT) - 1)) & 0x1;
int max = a - k * c;
return max;
}
Source: http://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/098478280X
Edit: This code works even when a-b overflows.
Let k equal the sign of a-b such that if a-b >=0, then k is 1, else k=0.Let q be the inverse of k. Above code overflows when a is positive or b is negative, or the other way around. If a and b have different signs, then we want the k to equal sign(a).
/* Flips 1 to 0 and vice-versa */
public static int flip(int bit){
return 1^bit;
}
/* returns 1 if a is positive, and 0 if a is negative */
public static int sign(int a){
return flip((a >> ((sizeof(int) * CHAR_BIT) - 1)) & 0x1);
}
public static int getMax(int a, int b){
int c = a - b;
int sa = sign(a-b); // if a>=0, then 1 else 0
int sb = sign(a-b); // if b>=1, then 1 else 0
int sc = sign(c); // depends on whether or not a-b overflows
/* If a and b have different signs, then k = sign(a) */
int use_sign_of_a = sa ^ sb;
/* If a and b have the same sign, then k = sign(a - b) */
int use_sign_of_c = flip(sa ^ sb);
int k = use_sign_of_a * sa + use_sign_of_c * sc;
int q = flip(k); //opposite of k
return a * k + b * q;
}
Here is a funny solution:
int max_num = (x>y)*x + (y>=x)*y;
Assuming that you have covered bitwise operators already you can do this:
max = a-((a-b)&((a-b)>>(sizeof(int)*8-1)));
This is based off of the solution from Mathematically Find Max Value without Conditional Comparison that #user93353 pointed out in the comments above.
This may be overkill if you really are just trying to avoid if statements, not comparisons in general.
You can try this code to find max and min for two input variables.
((a > b) && (max = a)) || (max=b);
((a < b) && (min = a)) || (min=b);
For three input variables you can use similar method like this:
int main()
{
int a = 10, b = 9 , c = 8;
cin >> a >> b >> c;
int max = a, min = a;
// For Max
((a > b) && (a > c) && (max=a)) ||
((b > c) && (b > a) && (max=b)) ||
(max=c) ;
// For min
((a < b) && (a < c) && (min=a)) ||
((b < c) && (b < a) && (min=b)) ||
(min=c) ;
cout << "max = " << max;
cout << "and min = " << min;
return 1;
}
One run is:
:~$ ./a.out
1
2
3
max = 3 and min = 1
Edit
Thanks to #Tony D: This code will fail for negative numbers.
One may try this for negative numbers for two inputs to find max(not sure for this):
((a > b) && ( a > 0 && (max = a))) || ((b > a) && (max = b)) || (max = a);