424 - Integer Inquiry

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Mon Sep 23, 2002 1:11 pm

char type has a -128..127 value range. you only deal with carry at the end, so it may happen that e[x] has value range overflow earlier (like in the example: 15*9=135 > 127)

CCC
New poster
Posts: 2
Joined: Tue Nov 19, 2002 1:13 pm

424, Compile ERROR??

Post by CCC » Tue Nov 19, 2002 1:20 pm

[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?

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

Post by ec3_limz » Thu Dec 12, 2002 12:22 pm

Hi,

Sometimes, even if a program is compilable in VC++, it may not be able to compile in the online judge because the online judge is using GCC, a different C++ compiler. Try compiling your program with DJGPP or Bloodshed Dev-C++.

Also, will you mind posting the replies from the judge's compiler?

CCC
New poster
Posts: 2
Joined: Tue Nov 19, 2002 1:13 pm

Post by CCC » Tue Dec 17, 2002 10:25 am

Thx a lot, i will try using these compiler

lendlice
New poster
Posts: 22
Joined: Thu Nov 21, 2002 10:50 am

424(W.A)

Post by lendlice » Sat Mar 15, 2003 10:04 am

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]

Nick
Learning poster
Posts: 53
Joined: Sun Jan 12, 2003 4:49 am

Post by Nick » Sun Mar 23, 2003 6:58 am

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....

Code: Select all

char num[102];
int res[105];
that should be enough

Hope it'll help you guys,
Nick

Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

424 Why WA

Post by Rene » Sat May 10, 2003 1:38 pm

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]

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion » Sat May 17, 2003 5:35 am

Hi!

your code still wrong, try this:
93
93
93
93
0

Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

Post by Rene » Sun May 18, 2003 10:08 am

i have checked my code and got AC
thanx :D

Calvin
New poster
Posts: 5
Joined: Sat Mar 08, 2003 12:50 pm

424 WA ...

Post by Calvin » Sat Jun 07, 2003 5:00 pm

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

User avatar
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 » Tue Mar 09, 2004 4:36 pm

I think You have solve this.
If yet you don't, then make your 'tmp' array size little big :-?
and then remove your code from here :evil:
it is almost AC code!

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

calvin

Post by osan » Tue Mar 09, 2004 7:54 pm

just change tmp[100]
hope u will get Ac if u use tmp[1000]
& pls remove ur code from forum
this time WA
what next...............?

raihan_aiub
New poster
Posts: 1
Joined: Wed May 26, 2004 12:50 pm
Location: Mohammadpur, Dhaka
Contact:

Problem 424 - Dont know whats's Wrong ?

Post by raihan_aiub » Fri Jun 18, 2004 6:38 pm

I tried every possible input for this problem. It works perfectly well but the online judge gives a " Wrong Answer ". Please help !!

jagadish
Learning poster
Posts: 90
Joined: Mon Feb 16, 2004 8:53 pm
Location: Bangalore INDIA

Post by jagadish » Fri Jun 18, 2004 8:47 pm

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..
if u can think of it .. u can do it in software.

wolf
New poster
Posts: 34
Joined: Sun Aug 22, 2004 4:20 am
Location: Poland

Post by wolf » Sun Aug 29, 2004 11:28 pm

Hi! The only thing you have to do is to change the tmp[100] for tmp[101]. Remember that strings are being kept with the end of the string character (just zero) :-D. I had the same problem before.

P.S. Remove the code form forum, please

Post Reply

Return to “Volume 4 (400-499)”