It is a simple program to calculate the next number of a given number by adding one to it. By during execution it asks more inputs than needed and gives a wrong result. But when I give input through file, it gives right answer. I tried debugging but only thing i found was my input loop was running more than the number of times i specified. I am a newbie to this. Please help.
//Simple program to add 1 to the number given. Digits in number can be upto 10^9
#include <iostream>
#include <cstdio>
#define DD cout<<"Working!"
using namespace std;
void JNEXT()
{
long n;//total length of the digits, you will give
cin>>n;
long all_nine=0;//for checking whether all are nine or not
long a[n+1]={0};//initializing the array with zero
for(long i=0;i<n;i++)//taking input and storing in a[]
{
cin>>a[i];
if(a[i]==9)
all_nine++;
DD;
}
if(all_nine==n)//if all digits are nine then print next number by adding one
{
cout<<1;
for(long i=0;i<n;i++)
cout<<0;
cout<<endl;
}
else
{
int carry=1;
for(long i=n-1;i>=0;i--)
{
a[i]+=carry;
if(a[i]==10)
a[i]=0;
else carry=0;
}
for(long i=0;i<n;i++)
cout<<a[i];
cout<<endl;
}
}
int main()
{
int t;//Number of Queries you want to check(process)
cin>>t;
while(t-- > 0)
JNEXT();
return 0;
}
Edits: I got the answer Thanks!
Related
Ive tried to use an 'odd' sorting algorithm just to see if i can manage make it work, basically the algorithm finds the smallest number and places it first, then it repeats this loop but starting from the second number, ignoring the already sorted one completely, but for some reason it seems to not ignore the first sorted number and constantly swaps it out.
#include<iostream>
using namespace std;
int main()
{
int n,i,j;
int min;
int a[10];
cin>>n;
for(i=0;i<n;i++)
{
cout<<"a["<< i <<"]=";
cin>>a[i];
}
min=a[0];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(min>a[j])
min=a[j];
}
a[j]=a[i];
a[i]=min;
min=100;
}
for(i=0;i<n;i++)
cout<<a[i];
return 0;
}
int main()
{
unsigned long long int i,t,N;
cin>>t; //INPUT NUMBER OF TEST CASE
while(t--)
{
count=0;
cin>>N;
if(N==0 || N==1) //IF FACTORIAL=0 OR 1
cout<<"1"<<endl;
else
{
i=1;
while(N!=1)
{
i++;
N/=i; //REDUCING THE NUMBER
}
cout<<i<<endl;
}
}
return 0;
}
I coded this solution, which gives the correct output for small integers.
I am getting the partially correct output. (Time Limit Exceeded is 2nd case). I think that there can be another approach to solve this question of dynamic programming. Please correct me if I'm wrong.
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
uint128_t N = 1;
After including the library in your program.
This stores the input without any problem.
ps: these libraries are not supported by all online judges.
The problem is from a recent competition on Codechef.
According to the problem, for a given array of length N, find the maximum average of the numbers of any contiguous sub-array of length between A and B.
Now here is the logic that I have used. I have in a loop iterated over the possible contiguous sub-array length K from A to B. Then for every such K, I have used the sliding window mechanism to find the maximum sum of contiguous
elements of the array. Then I check if the already stored answer (the average) is smaller than the current maximum/K and update the answer.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b,a;
cin>>n>>b>>a;
long long arr[n+1];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
double ans=0;
for(int k=a;k<=b;k++)
{
long long maxi=0;
for(int i=1;i<=k;i++)
{
maxi=maxi+arr[i];
}
long long l=maxi;
for(int i=k+1;i<=n;i++)
{
l=l+arr[i]-arr[i-k];
maxi=max(maxi,l);
}
double x=(double)(((double)(maxi))/(double)(k));
ans=max(ans,x);
}
cout<<ans<<endl;
}
}
However, after implementing my logic over as shown, I am getting wrong answers even though I do not know how different they are the actual answer since I have not obtained anything wrong in my processes of debugging.
The following is an edit:
Are my previous code and the following the same?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b,a;
cin>>n>>b>>a;
long long arr[n+1];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
long double ans=0;
for(int k=a;k<=b;k++)
{
long long l=0;
for(int i=1;i<=k;i++)
{
l=l+arr[i];
}
ans=max(ans,(long double)(((long double)(l))/(long double)(k)));
for(int i=k+1;i<=n;i++)
{
l=l+arr[i]-arr[i-k];
ans=max(ans,(long double)(((long double)(l))/(long double)(k)));
}
}
cout<<ans<<endl;
}
}
Thanks for helping guys. I tried out the following and it sort of works:
cout<<fixed<<setprecision(10);
setprecision(10)
I want to reverse the number given by the user. I wrote the code in such a way that it takes the number of digits of number and the input and reverses it. As even the limit of 'long long' is 19 digits, what can I do so that the code works even if the number of digits in the input are greater than 20? [Without using third party libraries]
#include<iostream>
using namespace std;
void make_int(int a[],long long int n)
{
int i=0;
while(n)
{
a[i]=n%10;
i++;
n=n/10;
}
for(int j=0;j<i;j++)
cout<<a[j];
}
int main()
{
int N;
cin>>N;
int *tc = new int[N];
long long int num;
cin>>num;
make_int(tc,num);
return 0;
}
Read the number in as a std::string and reverse that, since it's actual numerical value is unimportant for what you want to do.
I took it upon myself to learn C++ a few days ago. I have just written a program to find prime numbers, up to a user inputted value, and write these values to a file. The program works fine with numbers up to the order of 100,000 - 500,000. But, if I try to go to 1,000,000 the program freezes. Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
long pp, z,counter,lim,indyCounter;
bool isPrime=false;
ofstream myfile;
myfile.open("E:\\Program Files (x86)\\C++ Programs\\PrimeFinder\\Primes.txt");
cout<<"up to what number would you like to calculate primes? ";
cin>>lim;
cout<<endl;
long ps[lim]; //real-time array of primes
pp=3; //prospective prime
ps[0]=2; //initializing prime array with first prime number
counter=1;
indyCounter=1;
for(int y=1; y<=lim;y++)
{
ps[y]=1;
}
for(int z=0; z<=lim; z++)
{
for(int x=0;x<counter;x++)
{
if(pp%ps[x]!=0)
{
isPrime = true;
}
if(pp%ps[x]==0 && ps[x]!=1)
{
isPrime=false;
break;
}
}
if(isPrime)
{
ps[indyCounter]=pp;
indyCounter++;
}
counter++;
pp++;
}
for(int y=0; y<=lim-1;y++)
{
if(ps[y]!=1)
{
myfile<<ps[y]<<endl;
}
}
myfile.close();
return 0;
}
please excuse my beginners code, and all advice is much appreciated!
Thanks,
Steve
The default stack size on Windows is only 8MB, and an array of 1000000 longs requires 8MB, so you're overflowing the stack. You need to allocate your array on the heap instead.