I thought it is quite easy problem but i cannot get Accepted..
Does anyone know any tricky inputs for this task ??
Thanx in advance
![:wink:](./images/smilies/icon_wink.gif)
Moderator: Board moderators
Code: Select all
[pascal]program n763;
var
a,b,res : array[1..1000] of integer;
i,j : integer;
s : string;
len : integer;
begin
assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);
while not eof do
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(res,sizeof(res),0);
readln(s);
for i := length(s) downto 1 do
begin
case s[i] of
'0' : a[length(s)+1-i] := 0;
'1' : a[length(s)+1-i] := 1;
end;
end;
len := length(s);
readln(s);
readln;
for j := length(s) downto 1 do
begin
case s[j] of
'0' : b[length(s)+1-j] := 0;
'1' : b[length(s)+1-j] := 1;
end;
end;
if length(s) > len then len := length(s);
for i := 1 to len do res[i] := a[i] + b[i] ;
i := 1;
while i <= len + 5 do
begin
if res[i] = 0 then
begin
inc(i);
continue;
end;
if (res[i] = 1)and(res[i+1] =1 ) then
begin
res[i] := 0;
res[i+1] := 0;
inc(res[i+2]);
inc(i);
continue;
end;
if res[i] > 1 then
begin
if i > 2 then
begin
dec(res[i]);
dec(res[i]);
inc(res[i-2]);
inc(res[i+1]);
i := i -2;
end;
if i < 2 then
case i of
1 :
begin
res[1] := res[1] - 2;
inc(res[3]);
end;
2 :
begin
res[2] := res[2] - 2;
inc(res[3]);
end;
end;
continue;
end;
inc(i);
end;
i := len + 10;
while res[i] = 0 do dec(i);
while i <> 0 do
begin
write(res[i]);
dec(i);
end;
writeln;
writeln;
end;
end.
[/pascal]
Code: Select all
1000010
100001001
10
1
1000101010
0
1001
1000101010101010100101
Code: Select all
0
1
0
0
1
1
10
1
10
10
11
11
11
10
10
11
101
100
101
101
1010101
101010
1000101
101001
101010101010101
1010101000101010
101010101010101010101010
101000101000101000101000101000101000101000
1010101010101010101010101010001010001010001010001
101000101000101000101000101000101000101000
Code: Select all
1
0
10
100
101
1001
1000
1000
1010
10000
10101001
10010010
10101010010101001
1010001010001010010001010001010001010010001
10000000101000101000101000100010100010100010100001
Code: Select all
1
0
10
100
101
1001
1000
1000
1010
10000
10101001
10010010
10101010010101001
101000101000101010001000101000101000100101
10000000101000101000101000100010100010100010100001
Code: Select all
#include<stdio.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define DIGITMAX 100
void fibsum(char n1[DIGITMAX], char n2[DIGITMAX]);
void fibinc(char n1[DIGITMAX + 2], int d);
int main()
{
char line[DIGITMAX + 1];
char a[DIGITMAX + 2];
char b[DIGITMAX + 2];
int len;
int i;
while(gets(line) != NULL) // get a
{
memset(a, '0', DIGITMAX + 2);
memset(b, '0', DIGITMAX + 2);
len = strlen(line);
memcpy(a + (DIGITMAX + 2 - len), line, len);
// get b
gets(line);
len = strlen(line);
memcpy(b + (DIGITMAX + 2 - len), line, len);
// get blank line
gets(line);
// sum (result is still stored in variable a)
fibsum(a, b);
// output result
i = 0;
while(a[i] == '0')
i++;
if(i == DIGITMAX + 2)
i--;
while(i < DIGITMAX + 2)
{
putchar(a[i]);
i++;
}
putchar('\n');
// output blank line
putchar('\n');
}
return 0;
}
void fibsum(char n1[DIGITMAX + 2], char n2[DIGITMAX + 2])
{
int i;
int c = 0;
for(i = 2; i <DIGITMAX + 2; i++)
{
if(n2[i] == '1')
fibinc(n1, i);
}
}
void fibinc(char n1[DIGITMAX + 2], int d)
{
int c = 0;
if(n1[d] == '0')
{
if(d < 2)
n1[d] = '1';
else if(d == 2)
{
if(n1[0] == '0' && n1[1] == '1')
{
n1[0] = '1';
n1[1] = '0';
n1[2] = '0';
}
else
n1[2] = '1';
}
else
{
if(n1[d-1] == '1')
{
n1[d-1] = '0';
n1[d] = '0';
fibinc(n1, d-2);
}
else
n1[d] = '1';
}
}
else
{
n1[d] = '0';
fibinc(n1, d-1);
if(d < DIGITMAX + 2 - 2)
fibinc(n1, d+2);
else if(d == DIGITMAX + 2 - 2)
fibinc(n1, d+1);
else if(d == DIGITMAX + 2 - 1)
;
}
}
I consider it means the sum of two values is at most 100 digits at first, but always got WA.Write a program that takes two valid Fibinary numbers and prints the sum in Fibinary form. These numbers will have at most 100 digits.