Why am I getting a runtime error on the following code? Its running successfully in www.ideone.com
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long int t,i,temp,ans;
int z,x,p=0;
cin>>t;
temp=t;
while(temp!=0)
{
temp=temp/10;
p++;
}
long int n[p];
for(i=0;i<t;i++)
{
cin>>n[i];
}
if(t<=100000)
{
for(i=0;i<t;i++)
{ if(n[i]<=1000000000)
{x=int(log10(n[i])/log10(2));
ans=pow(2.0,x);
cout<<ans<<endl;
}
else
break;}
}
return 0;
}
your n array is strangeā¦
I tried on ideone, you are not getting RE, but you have to print t numbers as a result and your program prints only first few - see http://ideone.com/NlGNdu (4. test case with t = 1000)
1 Like
I see what wrong I did. I corrected the code. Didnāt have the problem u mentioned when compiled on ideone. But now submission returns āTime Limit Exceededā. And I thought this would probably take the least time since the logic is small.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long int t,i,temp,ans;
int z,x,p=0;
cin>>t;
long int n[t];
for(i=0;i<t;i++)
{
cin>>n[i];
}
if(t<=100000)
{
for(i=0;i<t;i++)
{ if(n[i]<=1000000000)
{
x=int(log10(n[i])/log10(2));
ans=pow(2.0,x);
cout<<ans<<endl;
}
else
break;}
}
return 0;
}
Please check the answer I posted below.
Iām not sure, but in worst case you have to read 10^5 numbers, so cin
could be too slow, try to replace cin
with scanf()
ā¦
Finally Done. Thanks a lot. Without your help I would still have been confused.Worked with scanf and print f.
#include
//#include<conio.h>
using namespace std;
void sort(int*,int);
int* merge(int ,int,int,int);
int main()
{
int *a,*b,*c,size_a,size_b;
cout<<āenter size_a\nā;
cin>>size_a;
cout<<āenter size_b \nā;
cin>>size_b;
a=new int[size_a];
b=new int[size_b];
cout<<āenter a arr \nā;
for(int i=0;i<size_a;i++)
{
cin>>a[i];}
cout<<āenter b arr\nā;
for(int i=0;i<size_b;i++)
cin>>b[i];
sort(a,size_a);sort(b,size_b);
c=merge(a,b,size_a,size_b);
for(int i=0;i<size_a+size_b;i++)
cout<<c[i]<<"\t";
return 0;
}
void sort(int*x,int size)
{
int temp;
for(int k=0;k<size-1;k++)
{for(int i=0;i<size-1-k;i++)
if(x[i]<=x[i+1])
{ temp=x[i];
x[i]=x[i+1];
x[i+1]=temp;}
}}
int * merge(int a,intb,int size_a,int size_b)
{int *c=new int[size_a +size_b];
if (a[0]>b[0]){
c[0]=a[0];
}
else {c[0]=b[0];}
int i=1,j=1,k=1;
while(j!=size_b || i!=size_a)
{
if (a[i]==b[j])
{
if(a[i]==c[k-1])
{i++;j++;}
else
{c[k]=a[i];k++;i++;}}
else if(a[i]>b[j])
{{if(a[i]==c[k-1] )
i++;
else
c[k]=a[i];i++;k++;}}
else
{if (b[j]==c[k-1])
j++;
else
{c[k]=b[j];k++;j++;}
}}
if(i==size_a)
while(j!=size_b)
{c[k]=b[j];
k++;j++;}
else
while(i!=size_a)
{c[k]=a[i];
i++;k++;}
if(k!=(size_a+size_b))
{
while(k!=size_a+size_b)
{c[k]=0;k++;}
}
return c;
}
runtime error plz help
1 Like