I'm solving a problem. I want a function that returns all ways to make different positive int number plus equal to that number, for example 6 will be 1+5 ,2+3+1,2+4 so will be 3
but my solution return infinity loop
#include <iostream>
using namespace std;
int find(int num,int before)
{
int first=1;
int count=1;
int end =num-1;
if(end-first==0) return count;
while(end-first!=1&&end-first!=0)
{
if(end==before||first==before) continue;
first++;
end--;
}
before=first;
return count+find(end,before);
}
int main()
{
int a;
cin>>a;
int x=find(a,1);
cout<<x;
}
i try cout "a" in loop and repeat forever. Please help me.
EDIT: My code just solve a piece of problem, so it not solution, i'll try to close topic, thanks all
In this concept first will always be 1 and before will always be 1 because the first is never incremented here as the instruction is not reached.
first is initialized by 1 and before by 1, that means before==first is true and the loop will ignore all other instructions after continue.
and since you are comparing end==before||first==before this will always be true, because even when end==before is false the second test will be true. The test logic of false||true is true.
Related
I'm trying to print all permutations using only the digits 4 and 7 upto n digits using recursion like (4,7,44,47,74,77) for 2 digits.I'm trying to call the function recursively twice. I can't figure what's wrong with my code.
#include <bits/stdc++.h>
using namespace std;
vector<int> s;
void check(int a, int b)
{
if(b==0)
return;
else
{
check(a*10+7, b--);
s.push_back(a*10+7);
check(a*10+4, b--);
s.push_back(a*10+4);
}
return;
}
int main(void)
{
int t;
cin >> t;
check(0, t);
for(auto i=s.begin();i!=s.end();i++)
{
cout<<*i<<endl;
}
return 0;
}
You are using post-decrement which has no effect on the recursive call. On the other hand, it has an unintended effect on the second call. Just decrement the b before both calls and only once.
Coupled with non-defensive if(b==0), you get stack overflow when b slips into negative values and your program terminates.
Something a debugger or address sanitizer would immediately told you.
Visit https://www.interviewbit.com/problems/largest-number/ for the question...
Now I wrote the below code to solve the question (although I used an array to store the number, will do the storing in strings part later..)-
So in this algorithm, I basically used quicksort but with a twist, I changed the definition of greater than or lesser than of two numbers say X, Y such that if the number formed by using X first and Y second or XY is >= YX then greater than(X, Y) is true
In the present scenario, the code is giving runtime error, which I can't understand why, also after a bit of debugging as shown in the comments, still the answer is not coming as expected.
#include <iostream>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h>
using namespace std ;
bool greaterthan(int a,int b)
{
int n1,n2,s1,s2;
n1=((int )log10(a))+1;
n2=((int)log10(b))+1;
s1=a*((int )pow(10,n2))+b;
s2=a + ((int )pow(10,n1))*b;
if(s1>=s2){return true;}
else{return false;}
}
int spartitions(vector<int >&B,int s , int e)
{
int pivot = B[e];
int pin =s;
int i;
for(i=s;i<=e;i++) //if i change this to i<e
{
if(B[pin]>=pivot)
{swap(B[pin],B[i]);
pin++;
}
// and add swap(B[pin],B[e]);
}
return pin-1; // and return pin here then it works but not give correct output
}
int prand(vector<int >&B,int s ,int e)
{
srand(time(NULL));
int n = rand()%(e-s+1)+s;
swap(B[n],B[e]);
int pin = spartitions(B,s,e);
return pin;
}
void qsort(vector<int >&B,int s, int e )
{
if(s<e){
int p= prand(B,s,e);
qsort(B,s,p-1);
qsort(B,p+1,e);
}
}
vector<int> largestnumber(vector<int >&A)
{
int n =A.size();
vector<int >B(n);
B=A;
qsort(B,0,n-1);
return B;
}
int main()
{
int n;
cin>>n;
vector<int>A(n);
int i;
for(i=0;i<n;i++)
{
cin>>A[i];
}
vector<int >B(n);
B=largestnumber(A);
for(i=0;i<n;i++)
{
cout<<B[i];
}
}
Please Help as I am a newbie in programming and can't figure this out from like 3-4 hours ...??
Would really appreciate if someone can correct my code only and not give a different algorithm, as I want this algorithm to be corrected.
Your self-written qsort function recursively calls itself, which adds more things to the stack, which only has so much space. When the list is too big, there will be too many function calls in the stack and it overflows. That's why anything less than 5 for the first input (which is for n) works fine but as soon as you exceed that, you get a runtime error. Consider not using a recursive function call.
Edit: Enabling optimisation also seems to fix this issue.
This may not work depending on the compiler and how it optimises. (Works on MSVC)
My Need:
Using a while loop greet as many names as that are available on the stdin. Stop when you read the string '42' as a name.
My Coding:
#include<iostream>
using namespace std;
int main()
{
int input=1;
int i= 0;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Result:
For input 42, test case is passed. For other input, test case failed. Please post your answer.
Answer After ~1 Year:
Very sorry for this question . This is I asked when I have 0 knowledge about C++. This may be useful for the freshers.
Your loop is flawed
int input=1;
int i= 0;
string name;
while(input<=i)
as input is greater than i to start with
You think the test case works for 42 but actually the logic inside your loop is never executed. It is simply the case that the console output is the same (i.e. there is none) but your code never gets as far as the cin to check the input is 42
Your code doesn't ever enter while since the condition is always false.
Just use,
....
while(1)
{
....
This will run your loop indefinitely, and break whenever 42 is encountered.
Your code does not even run when you pass 42, because input is greater than i.
while(input<=i) // input = 1, i = 0, 1 > 0
What you probably want is an infinite loop:
while (true)
Your Code has these Problems:
It won't enter the while loop as input is initialized to 1 and i is initialized to 0.While checking the condition *while(input<=i)*i.e., while(1<=0) which is false it won't execute the statements below.
-
My Solution:
#include<iostream>
using namespace std;
int main()
{
int input=0;
int i= 1;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Initializing input with 0 and i with 1 will give you the desired output.
First of all, Sorry for my basic question. The loop is not initiated. Because the input is 1 and i is 0. But the condition I given is input<=i. Because of the false condition, the control is not entering in loop
I was working on the following problem from Project Euler.
What is the largest prime factor of the number 600851475143?
I have come up with the following solution.
#include<iostream>
#include<cstdlib>
#include <limits>
#include <math.h>
/**Project Euler:What is the largest prime factor of the number 600851475143**/
using namespace std;
bool isPrime(int n){
int sq,a=2;
sq=sqrt(n);
while(a<sq)
{
if(n%a==0)
return false;
}
return true;
}
int main() {
int c=2,max_prime;
long long d=600851475143;
int e=sqrt(d);
while(c<e)
{
if(isPrime(c))
{
if(d%c==0)
max_prime=c;
}
c++;
}
cout<<max_prime;
return 0;
}
There is no compilation error in the program but it is taking a lot of time in running. I have looked at other solutions but am not able to find out the mistake in my solution.I suspect there is something wrong because of the large number involved. What could that be? Any help appreciated.
Your isPrime function never increments a, so you're in an endless loop there.
In your isPrime method you're not changing any of the variables that control the while condition within the body of the while statement, so the loop wil run forever.
I used the following code for solving problem#14 but for some strange reason it gives no output.Maybe its taking too long to run???
P.S.I know that max is not supposed to be the answer but still there is no output anyways whereas for smaller values like i<100 I get the output.
#include <iostream>
long collatz(long);
int main()
{
using namespace std;
long i=3,max;
for(i=3;i<1000000;i++)
{
max=collatz(i-1);
if(collatz(i)>collatz(i-1))
{
max=collatz(i);
}
else
{
max=collatz(i-1);
}
}
cout<<max<<endl;
cin.clear();
cin.get();
}
long collatz(long n)
{
int count=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
count+=1;
}
else
{
n=3*n+1;
}
}
return count;
}
If you call collatz with n = 113383, you get overflow and n becomes negative from which it never recovers. So you have an infinite loop as it will never be 1. You need to use a long long inside collatz.
However, your collatz functions has other problems as pointed out by others. Also, your logic for the loop in main is not correct. You are resetting max each time through the loop. So, the result you report would be either collatz(999999) or collatz(999998). But that is not be the correct answer.