这是一个简单的程序,通过向其添加一个来计算给定数字的下一个数字。在执行期间,它会输出比所需更多的输入,并给出错误的结果。但是当我通过文件提供输入时,它会给出正确的答案。我试过调试,但只有我发现的是我的输入循环运行超过我指定的次数。我是这个的新手。请帮忙。
//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;
}
编辑:我得到了答案谢谢!