424 - Integer Inquiry
Moderator: Board moderators
424, Compile ERROR??
[cpp]#include <iostream>
using namespace std;
struct Number
{
char Carry, charShow;
};
Number add(char, char, char);
void main()
{
int NumInputed = 0, i, lengthSum = 0, lengthInput = 0;
char curr, *Input, *Sum, PreCarry = '0';
bool invalid;
Number PreNumber;
Sum = new char;
Input = new char;
while((curr=getchar()) !=10)
{
Sum[lengthSum] = curr;
lengthSum++;
}
while (lengthSum !=0 || Sum[0] != '0')
{
invalid = false;
lengthInput = 0;
while((curr=getchar()) !=10)
{
Input[lengthInput] = curr;
lengthInput++;
}
if(lengthInput == 1 && Input[0] == '0')
break;
for(i=0; i<lengthInput;i++)
if (Input < '0' || Input > '9')
invalid = true;
if (invalid)
continue;
if (lengthInput <= lengthSum)
{
for (i=lengthInput-1; i>=0; i--)
Input[i+(lengthSum-lengthInput)] = Input;
for(i=0; i<lengthSum - lengthInput ; i++)
Input = '0';
lengthInput = lengthSum;
}
else
{
for (i=lengthSum-1; i>=0; i--)
Sum[i+(lengthInput-lengthSum)] = Sum;
for(i=0; i<lengthInput - lengthSum ; i++)
Sum = '0';
lengthSum = lengthInput;
}
for(i=lengthSum-1; i>=0; i--)
{
PreNumber = add(Sum,Input,PreCarry);
Sum = PreNumber.charShow ;
PreCarry = PreNumber.Carry;
}
if (PreCarry == '1')
{
for (i = lengthSum; i>0 ; i--)
Sum = Sum[i-1];
Sum[0] = PreCarry;
lengthSum++;
}
NumInputed++;
}
for (i=0; i<lengthSum;i++)
cout << Sum[i] ;
cout << "\n";
}
Number add(char a, char b, char c)
{
Number ReturnNumber;
int CharShow;
CharShow = (int)a+(int)b+(int)c-3*(int)'0';
if(CharShow >= 10)
{
CharShow -= 10;
ReturnNumber.Carry = '1';
}
else
ReturnNumber.Carry = '0';
ReturnNumber.charShow = (char)(CharShow+(int)'0');
return ReturnNumber;
};[/cpp]
i can Ccompile it and run in Visual C++ sucessfully, it says it is compile error when i submit it to online judge......can any one help me?
using namespace std;
struct Number
{
char Carry, charShow;
};
Number add(char, char, char);
void main()
{
int NumInputed = 0, i, lengthSum = 0, lengthInput = 0;
char curr, *Input, *Sum, PreCarry = '0';
bool invalid;
Number PreNumber;
Sum = new char;
Input = new char;
while((curr=getchar()) !=10)
{
Sum[lengthSum] = curr;
lengthSum++;
}
while (lengthSum !=0 || Sum[0] != '0')
{
invalid = false;
lengthInput = 0;
while((curr=getchar()) !=10)
{
Input[lengthInput] = curr;
lengthInput++;
}
if(lengthInput == 1 && Input[0] == '0')
break;
for(i=0; i<lengthInput;i++)
if (Input < '0' || Input > '9')
invalid = true;
if (invalid)
continue;
if (lengthInput <= lengthSum)
{
for (i=lengthInput-1; i>=0; i--)
Input[i+(lengthSum-lengthInput)] = Input;
for(i=0; i<lengthSum - lengthInput ; i++)
Input = '0';
lengthInput = lengthSum;
}
else
{
for (i=lengthSum-1; i>=0; i--)
Sum[i+(lengthInput-lengthSum)] = Sum;
for(i=0; i<lengthInput - lengthSum ; i++)
Sum = '0';
lengthSum = lengthInput;
}
for(i=lengthSum-1; i>=0; i--)
{
PreNumber = add(Sum,Input,PreCarry);
Sum = PreNumber.charShow ;
PreCarry = PreNumber.Carry;
}
if (PreCarry == '1')
{
for (i = lengthSum; i>0 ; i--)
Sum = Sum[i-1];
Sum[0] = PreCarry;
lengthSum++;
}
NumInputed++;
}
for (i=0; i<lengthSum;i++)
cout << Sum[i] ;
cout << "\n";
}
Number add(char a, char b, char c)
{
Number ReturnNumber;
int CharShow;
CharShow = (int)a+(int)b+(int)c-3*(int)'0';
if(CharShow >= 10)
{
CharShow -= 10;
ReturnNumber.Carry = '1';
}
else
ReturnNumber.Carry = '0';
ReturnNumber.charShow = (char)(CharShow+(int)'0');
return ReturnNumber;
};[/cpp]
i can Ccompile it and run in Visual C++ sucessfully, it says it is compile error when i submit it to online judge......can any one help me?
424(W.A)
I get W.A
Anyone can help me
[cpp]
#include<stdio.h>
#include<string.h>
main()
{
char num[100000],out[100000],ch;
int i=0,n=0,carry=0,ans[100000],k=0,tr=0,j=0;
while(1)
{
n=strlen(out);
for(i=0;i<n;i++)
{
ans=0;
out='\0';
}
while(scanf("%s",num)==1)
{
n=strlen(num);
if(num[0]=='0'&&n==1)
break;
i=0;
for(n=n-1;n>=0;n--)
{
ans=num[n]-'0'+carry+ans;
out=ans+'0';
i++;
}
}
n=strlen(out);
for(j=0;j<n;j++)
{
ch=out[j]+carry;
out[j]=(out[j]-'0'+carry)%10+'0';
carry=(ch-'0')/10;
}
if(carry!=0)
{
printf("%d",carry);
tr++;
}
n=strlen(out);
for(n=n-1;n>=0;n--)
{
if(tr!=0||out[n]!='0')
{
printf("%c",out[n]);
tr++;
}
}
printf("\n");
carry=0;
}
}[/cpp]
Anyone can help me
[cpp]
#include<stdio.h>
#include<string.h>
main()
{
char num[100000],out[100000],ch;
int i=0,n=0,carry=0,ans[100000],k=0,tr=0,j=0;
while(1)
{
n=strlen(out);
for(i=0;i<n;i++)
{
ans=0;
out='\0';
}
while(scanf("%s",num)==1)
{
n=strlen(num);
if(num[0]=='0'&&n==1)
break;
i=0;
for(n=n-1;n>=0;n--)
{
ans=num[n]-'0'+carry+ans;
out=ans+'0';
i++;
}
}
n=strlen(out);
for(j=0;j<n;j++)
{
ch=out[j]+carry;
out[j]=(out[j]-'0'+carry)%10+'0';
carry=(ch-'0')/10;
}
if(carry!=0)
{
printf("%d",carry);
tr++;
}
n=strlen(out);
for(n=n-1;n>=0;n--)
{
if(tr!=0||out[n]!='0')
{
printf("%c",out[n]);
tr++;
}
}
printf("\n");
carry=0;
}
}[/cpp]
I use one string (array char) to store the num each time.... add it to an array of integer.......then calculate the carry at the last time
U dont need big arrays....
that should be enough
Hope it'll help you guys,
Nick
U dont need big arrays....
Code: Select all
char num[102];
int res[105];
Hope it'll help you guys,
Nick
424 Why WA
Please help me!
why WA?
[cpp]
#include <iostream.h>
#include <stdio.h>
int max = 104;
int k;
int sum[105];
void main()
{
char c;
int i,j,k1,x;
int sum1[105];
k = max;
while (1)
{
for (i = 0;(c = getchar()) != '\n';i++)
sum1 = c - 48;
if (!sum1[0]) break;
k1 = i - 1;
j = max;
x = 0;
for (i = k1;i >= 0;i--)
{
x = sum[j] + sum1 + x;
sum[j] = x % 10;
x /= 10;
j--;
}
if (k > j)
k = j + 1;
while (x > 0)
{
k--;
sum[k] = x % 10;
x /= 10;
}
}
for (i = k;i <= max;i++)
cout << sum;
cout << endl;
}
[/cpp]
why WA?
[cpp]
#include <iostream.h>
#include <stdio.h>
int max = 104;
int k;
int sum[105];
void main()
{
char c;
int i,j,k1,x;
int sum1[105];
k = max;
while (1)
{
for (i = 0;(c = getchar()) != '\n';i++)
sum1 = c - 48;
if (!sum1[0]) break;
k1 = i - 1;
j = max;
x = 0;
for (i = k1;i >= 0;i--)
{
x = sum[j] + sum1 + x;
sum[j] = x % 10;
x /= 10;
j--;
}
if (k > j)
k = j + 1;
while (x > 0)
{
k--;
sum[k] = x % 10;
x /= 10;
}
}
for (i = k;i <= max;i++)
cout << sum;
cout << endl;
}
[/cpp]
-
- Experienced poster
- Posts: 192
- Joined: Sat Nov 30, 2002 5:14 am
424 WA ...
hi!
watch my code please. I got a WA for this, but i try so much input.. can you give me some input which create a WA ??
[c]#include <stdio.h>
#include <string.h>
int main()
{
int somme[110]={0};
char tmp[100],i,j,max=0;
scanf("%s",tmp);
while (strlen(tmp)>1 || tmp[0]!=48)
{
j=0;
for (i=strlen(tmp)-1;i>=0;i--)
{
somme[j]+=(tmp-48);
j++;
}
scanf("%s",tmp);
}
for (i=0;i<110;i++)
{
if (somme>9) somme[i+1]+=somme/10;
somme=somme%10;
}
max=109;
while (!somme[max] && max>0) max--;
while (max>=0) printf("%d",somme[max--]);
printf("\n");
return 0;
}[/c]
Thanks
watch my code please. I got a WA for this, but i try so much input.. can you give me some input which create a WA ??
[c]#include <stdio.h>
#include <string.h>
int main()
{
int somme[110]={0};
char tmp[100],i,j,max=0;
scanf("%s",tmp);
while (strlen(tmp)>1 || tmp[0]!=48)
{
j=0;
for (i=strlen(tmp)-1;i>=0;i--)
{
somme[j]+=(tmp-48);
j++;
}
scanf("%s",tmp);
}
for (i=0;i<110;i++)
{
if (somme>9) somme[i+1]+=somme/10;
somme=somme%10;
}
max=109;
while (!somme[max] && max>0) max--;
while (max>=0) printf("%d",somme[max--]);
printf("\n");
return 0;
}[/c]
Thanks
calvin
just change tmp[100]
hope u will get Ac if u use tmp[1000]
& pls remove ur code from forum
hope u will get Ac if u use tmp[1000]
& pls remove ur code from forum
this time WA
what next...............?
what next...............?
-
- New poster
- Posts: 1
- Joined: Wed May 26, 2004 12:50 pm
- Location: Mohammadpur, Dhaka
- Contact:
Problem 424 - Dont know whats's Wrong ?
I tried every possible input for this problem. It works perfectly well but the online judge gives a " Wrong Answer ". Please help !!
if u need any help on a problem please go through the previous posts.. in most cases u can figure out what you are missing and more importantly be specific ..how can anyone make out why you are getting WA unless you give your algo or code?
anyway for this problem try changing the size of array to large value like 11000..
anyway for this problem try changing the size of array to large value like 11000..
if u can think of it .. u can do it in software.