424 - Integer Inquiry

Moderator: Board moderators

dootzky
New poster
Posts: 36
Joined: Tue Apr 12, 2005 12:20 am
Contact:

424 WA

i've checked the forum, and found several posts about p424.
but none of them seems to help my program.

could someone tell me what am i doing wrong?
i simply read one line, go from end to begining, and if my last digit is >9, i take step like digit = digit - 10, and simply transfer that one level (number?) up on the next digit. that's all. it SHOULD work, works for all of my test inputs, even for those posted on this forum, but i still get WA.

here's my code, i hope someone can help me.

Code: Select all

``````#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void main() {

int i,c,cmax,br[11000];
char a[11000];
cmax=0;
for (i=0;i<101;i++) br[i]=0;

while (gets(a)) {

if ((strlen(a)==1) && (a[0]=='0')) break; // end sequence
c=0;

for (i=strlen(a)-1;i>-1;i--) {
if (( ((int)a[i]-48) + br[c]) > 9) {
br[c] += ((int)a[i]-48) - 10;
br[c+1]++;
cmax=c+1;
while (br[cmax]>9) {br[cmax] -= 10; br[++cmax]++;}
}
else br[c] += ((int)a[i]-48);
c++;
}

} // end of while

c=1;
for (i=100;i>-1;i--) if ((br[i]!=0)&&(c==1)) {cmax=i+1; c=0;}

for (i=cmax-1;i>-1;i--) cout << br[i];
cout << endl;

}
``````

Sanny
Learning poster
Posts: 78
Joined: Sat Feb 14, 2004 3:59 pm
Location: BUET
Contact:
Results can be more than 101 digits.

Regards
Sanny

dootzky
New poster
Posts: 36
Joined: Tue Apr 12, 2005 12:20 am
Contact:
you know what... nevermind!

forget that code. i was really hoping to do it correctly my way, with reading and processing one line at the time, but since i couldn't, i used another approach:

i read all the lines, and add ALL the numbers in one big integer array.
then i simply say somethin' like:

array[c+1] = array[c] / 10;
array[c++] %= 10;

and that worked!! i got ACC.
i hate when i can't get the problem from the first time, but hey - that's life!

Piotr
New poster
Posts: 2
Joined: Mon Jun 13, 2005 2:12 pm

Problem 424... Please help, I really don't know what to do now :/// I still got WA

program p424;

{\$APPTYPE CONSOLE}

uses
SysUtils;
var
t: array[1..100,1..100] of byte;
t2: array[1..200] of byte;

i,j,poz: integer;
s:ansistring;
b: boolean;

begin

for i:=1 to 100 do
for j:=1 to 100 do
t[i,j]:=0;

for i:=1 to 200 do t2:=0;

poz:=1;
s:='';

b:=false;
while not(s='0') do
begin
if (s='0')and(b=false) then break;

b:=true;
i:=101-length(s);
for j:=1 to length(s) do
begin
t[poz,i]:=ord(s[j])-48;
inc(i);
end;
inc(poz);
end;

poz:=200;

for i:=100 downto 1 do
begin
for j:=1 to 100 do
t2[poz]:=t2[poz]+t[j,i];
dec(poz);
end;

for i:=199 downto 1 do
t2:=t2+(t2[i+1] div 10);

for i:=2 to 200 do
t2:=t2 mod 10;

j:=1;

while t2[j]=0 do inc(j);

s:='';
for i:=j to 200 do
s:=s+chr(t2+48);

writeln(s);

end.

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Contact:

424-pls help

can any1 pls help me abt 424-integer inquiry my code below :
#include<stdio.h>
#include<string.h>
int main(void)
{

char p[101][101],q[101][101],res[101];
int i,j,t,max,len[100],sum=0,c=0,k=0;

for(i=0;;i++) {scanf("%s",&p);if(strcmp(p,"0\0")==0) break; }
max=i;
for(i=0;i<max;i++) len=strlen(p);

for(i=0;i<max;i++){
k=0;
for(j=0;j<100-len;j++){
q[j]='0';}
for(;j<100;j++){
q[j]=p[k];k++;
}

}

/*for(i=0;i<max;i++) {
for(j=0;j<100;j++){
printf("%c",q[j]);}printf("\n");}
*/

j=0;

for(k=99;k>=0;k--){
sum=0;
for(i=0;i<max;i++) {

sum+=q[k]-'0'; }
sum+=c;
c=sum/10;
t=sum%10;

res[j]=t+'0'; j++;

}

for(i=j-1;i>=0;i--) if(res[i]!='0') break;

for(;i>=0;i--) printf("%c",res[i]);

return 0;
}
aaa

n00i3
New poster
Posts: 20
Joined: Thu Sep 15, 2005 8:49 pm
Contact:
i dont understand the language sorry but i think that the idea is to input as a string and then add the two strings character by character :p

scidong
New poster
Posts: 45
Joined: Sat Jan 21, 2006 12:55 pm
Location: the four-dimensional world

424! Why WA?!

Code: Select all

``````#include<iostream.h>
#include<string.h>
#include<memory.h>

char s[10001];

void main(){
int i,j,c,cl;
char a[10001];
while(cin >> a){
c=strlen(a);
if(c == 1 && a[0]=='0') break;
else{
for(i=0; i<c; i++){
s[i]+=a[c-i-1]-'0';
for(j=0; j<i; j++){
if(s[j]>=10){
s[j+1]++;
s[j]-=10;
}
}
}
}
}
for(i = 0; i<10001; i++){
if(s[i]!=0) cl=i;
}
for(i=cl; i>=0; i--) cout << int(s[i]);
cout << endl;
}``````
Oh...
Plz give me a hint(or why am I WA).
plz...
All living things are amazing thing.

scidong
New poster
Posts: 45
Joined: Sat Jan 21, 2006 12:55 pm
Location: the four-dimensional world
I did. but still WA!
All living things are amazing thing.

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Hi,

Why do you break on c==1?

mosaick2
New poster
Posts: 21
Joined: Wed Mar 08, 2006 4:05 am

424. Compile Error. Help me~

My code is a little long. but, Actually, I have already written my BigInteger class. So, I just want to reuse my class.

My program is well-working at VC and G++.
However, online-judge give me C.E(compile error).

Code: Select all

`` deleted ``
----
Thankx, bro : )
Last edited by mosaick2 on Tue Aug 15, 2006 11:33 am, edited 1 time in total.

surya ss
New poster
Posts: 22
Joined: Sat Jun 11, 2005 7:31 pm

Re: 424. Compile Error. Help me~

mosaick2 wrote:My code is a little long. but, Actually, I have already written my BigInteger class. So, I just want to reuse my class.
here is the error :
04831250_24.c: In method `class BigInt BigInt::operator +(const BigInt
&)':
04831250_24.c:47: implicit declaration of function `int max(...)'

in the jugde compiler, it don't have function named max()

you must declare it yourself

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
in the jugde compiler, it don't have function named max()

you must declare it yourself
Actually, it does have it, you just have to #include <algorithm> to use it.

ahah2002
New poster
Posts: 5
Joined: Mon Sep 18, 2006 4:09 pm

424 Got a WA...Pls help!!!!!

I got WA for 424, but I don't know what is wrong of my code, pls help...

#include <iostream>
#include <string>
using namespace std;
int main(){
string str[110];
string str2[110];
string result;
char* ch;
char p;
int i,j,k;
int maxl;
int x10,x1;
int n;

i=0;
do{

cin>>str;
if (str[0]=='0' && (str.length()==1)){
break;
}
i++;
}while (1);

maxl=str[0].length();
for (j=0;j<i;j++){
if (str[j].length()>maxl){
maxl=str[j].length();
}
for (k=0;k<str[j].length();k++){
str2[j]+=str[j][str[j].length()-1-k];
}
}

n=0;
x10=0;
x1=0;
for (j=0;j<maxl;j++){
for (k=0;k<i;k++){
if (j<str2[k].length()){
p=str2[k][j];
ch=&p;
n+=atoi(ch);
}
}
n+=x10;
x1=n%10;
x10=n/10;
result[j]='0'+x1;
if (j==maxl-1){
if (n>=10){
result[++j]='0'+x10;
}
}

n=0;
}

for(i=j;i>=0;i--){
cout<<result;
}

return 0;
}

Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

If the input is

Code: Select all

``````5
5
5
0``````
The output is

Code: Select all

``15``

Code: Select all

`` 15``

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Contact:
input:

Code: Select all

``````99
99
99
99
99
99
99
99
99
99
99
99
99
0``````
`` <87``