are you calculating the result on-the-fly or using array?
if you're using array, maybe your array size is not big enough. the result will be around 500 digits.
any tricky inputs for this question ?
I've deleted all leading zeros....
and it seems to work well with all numbers (including zero and negative number)
and it can deal with greater then 2 ^ 31 numbers...but it still got WA..
int main() {
bool lead;
char str1[252], str2[252];
int i;
while (scanf("%s%s", &str1, &str2) == 2) {
getdigits(str1, str2);
multiply();
lead = true;
for (i = 0; i < 502; i++) {
if (lead && product > 0)
lead = false;
if (!lead)
printf("%d", product);
}
if (lead)
printf("0");
printf("\n");
}
I also got WA.... but i think i can deal with a variety of inputs
please help me...
<have made some changes of the program>
[pascal]
var s1, s2:string;
ans:array[-10..1000] of integer;
i, j, k, m, lb:longint;
check1, check2, neg:boolean;
begin
while not eof do
begin
lb:=1;
check1:=true;
check2:=true;
neg:=false;
readln(s1);
readln(S2);
while pos(' ', s1) <> 0 do
delete(s1, pos(' ',s1), 1);
while pos(' ', s2) <> 0 do
delete(s2, pos(' ',s2),1);
for i:=1 to length(s1) do
If s1 in ['1'..'9'] then
begin check1:=false; break; end
else if s1='-' then
begin
delete(s1, i, 1);
neg:=not neg;
dec(i);
end;
If not check1 then delete(s1, 1, i-1);
for i:=1 to length(s2) do
If s2 in ['1'..'9'] then
begin check2:=false; break; end
else if s2='-' then
begin
delete(s2, i, 1);
neg:=not neg;
dec(i);
end;
If not check2 then delete(s2, 1, i-1);
If (not check1) and (not check2) then begin
m:=0;
for i:=1 to 1000 do
ans:=0;
k:=0;
for i:= length(s2) downto 1do
begin
for j:=length(s1) downto 1 do
begin
ans[i+j]:=ans[i+j]+(ord(s2)-48)*(ord(s1[j])-48)+k;
If ans[i+j] >= 10 then
begin
k:=ans[i+j] div 10;
ans[i+j]:=ans[i+j] mod 10;
end;
If i+j > m then m:=i+j;
end;
If k > 0 then
begin
inc(ans[i+j-1], k);
If i+j-1 > m then m:=i+j-1;
k:=0;
If i+j-1 < lb then lb:=i+j-1;
end;
end;
If neg then write('-');
i:=m+1;
repeat
dec(i);
If ans >= 10 then
begin
inc(ans[i-1], ans div 10);
ans:=ans mod 10;
end;
until (i<=lb) and (ans[i-1] < 10);
while ans[lb] = 0 do inc(lb);
for i:=lb to m do
write(ans[i]);
writeln;
end
else writeln(0);
end;
end.
[/pascal]
Last edited by route on Sat Jan 11, 2003 4:31 am, edited 2 times in total.
I have doubts about the input format:
Doesn't it have two seperate lines for a pair of inputs?
e.g.
12
12
144<--- output
And are there any tricky inputs other than trailing and leading spaces in inputs, negative numbers, zero in inputs (these are what my problem has solved)?
you have right - one input test is pair of line I don't remember about it, but If you read input like scanf("%s",...) it doesn't matter ....
I don't know other tricks .... Perhaps zero is only special case ...
I think, that you must have error in functions computing result ... Maybe some carry sign is missing . I don't know ....
I have later discovered my mistake. My mistake is that in adding the subtotal, I FORGOT TO CARRY OVER WHEN A DIGIT IS MORE THAN 10. And now I got this problem Accepted
For those of you having trouble with this problem, here is my Accepted source code.
[cpp]#include <stdio.h>
#include <string.h>
char n1[503], n2[503], product[503];
void getdigits(char s1[252], char s2[252]) {
int i, j;
int main() {
bool lead;
char str1[252], str2[252];
int i;
while (scanf("%s%s", &str1, &str2) == 2) {
getdigits(str1, str2);
multiply();
lead = true;
for (i = 0; i < 502; i++) {
if (lead && product > 0)
lead = false;
if (!lead)
printf("%d", product);
}
if (lead)
printf("0");
printf("\n");
}
hmm, let's see... have you tried multiplying with zero? and what's the upper limit for the numbers you can deal with? I got AC on it and I prepared to deal with any number less than 10^1000... maybe you can't use long long and those kinda things :)
Dealing with failure is easy: Work hard to improve.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.