1 / 10
Dec 2012

Hi,

I tried some random test cases on my PC, like:
10
2
1000
2
9999999999999999
999999999999
1000000000000000000000000000000000
500000000000000000000000000000000
60
40
9998
4000
100002
22222
500
300
232323232
121212120
100
20
and my program output was:
6
4
501
499
5000499999999999
4999500000000000
750000000000000000000000000000000
250000000000000000000000000000000
50
10
6999
2999
61112
38890
400
100
176767676
55555556
60
40

I confirmed tat the output is right as below:
klaudia = (total + extra) / 2
natalia = total - klaudia

But on SPOJ, I get WA. If you could help me with one of the test cases for which it could go wrong, then I will try to fix it.
One probl with my code is, it doesn't handle cases where the total_apples + klaudia_extra_apples is Odd.. I was hoping this ain't the problem.. if this is, then i need to work on tat. Plz help..

My program id on SPOJ is 8235671. Let me know if i need to paste the code here..

Your test cases appear to be correct. My AC program doesn't handle odd sums of input, too (it just outputs the integer part).

Are you sure to handle the detection of "end-of-input" correctly? Print a newline after last number?

Yes. I print newline after every number I display.
It is like: klaudia\nnatalia\nklaudia\nnatalia\n

End-of-Input? From the problem statement, there is no specific end-of-input, input stops exactly after 20numbers/20lines right? (i.e, total_apples\nextra_apples\n.......10times.)

Any other way to figure out why we get WA? Not just this problem, in general.

you're right, I didn't remember that this problem has got exactly 10 test cases. You tested cases with numbers in the range of 10**100?
The test cases you showed above have 35 digits at max...

Yes I've. I did test for some numbers around 10pow100. Its just that I din't test on the standard version which takes 10inputs, I tested it on my debug version which takes 1input, so I couldn't paste it here. Do you by any chance have sample data and output for huge numbers so that I can test and do diff and confirm?

Or is there anyway for someone to look at my Code and point out any possible bugs? Can the SPOJ admin help? I thought, they could, so I posted over here. Din't know I was on my own here as well.

Well, I do have an accepted solution, which I could use to provide test cases. I will do that later, with a little bit more time.

Currently it is quite "silent" here in this forum. I don't know why, I saw competent comments of experienced users posted regularly some weeks ago. But currently user activity appears to have been decreased... neutral_face

Sure.. Thanks a lot for doing tat.. Hope I can get this right..!!

Ok, her are some test cases using 100 digits.

 input:
100
56
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777
9876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210
109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876548
output:
78
22
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
4993209877099320987709932098770993209877099320987709932098770993209877099320987709932098770993209879
4883333333888333333388833333338883333333888333333388833333338883333333888333333388833333338883333331

Result is not really surprising wink If this shouldn't help you: Do not hesitate to post your code. Usually there will be a helping comment...

Thank you..
I tried with your input, my output is exactly same as yours..

Here, I post my code.. Wen I copied ur output, it looked like thr is an extra blank line after last output(like \n\n), so I tried adding extra newline after d last output, still I got d same WA.. Nywy, this is my original code which takes strictly 10inputs..

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void get_klaudia_apples(const char* total, const char* extra, char* klaudia) {
    int i = strlen(total) - 1;
int j = strlen(extra) - 1;
char add_result[105] = { 0 };
if(i > 100 || j > 100) return;
int k = 103;
unsigned int carry = 0;
while(i >= 0) {
	int tmp = (*(total + i) - '0');
	if(j >= 0) 
		tmp += (*(extra + j) - '0');
	if(carry) {
		tmp += carry;
		carry = 0;
	}
	if(tmp > 9) {
		carry = tmp / 10;
		tmp = tmp % 10;
	}
	*(add_result + k) = '0' + tmp;
	i --;
	j --;
	k --;
}
while(carry) {
	int tmp = carry % 10;
	carry /= 10;
	*(add_result + k) = '0' + tmp;
	k --;
}

i = 0; j = k + 1;
while( j < 104) {
	*(add_result + i) = *(add_result + j);
	i ++;
	j ++;
}
*(add_result + i) = 0;

j = 0;
unsigned int remainder = 0, first_flag = 1;
i = 0;
while(i < strlen(add_result)) {
	unsigned int dividend = 0;
	dividend = (remainder * 10);	
	dividend += *(add_result + i) - '0';
	remainder = 0;
	i ++;
	if(dividend == 0) {
		*(klaudia + j) = '0';
		first_flag = 1;
		remainder = 0;
		j ++;
		continue;
	}
	if(first_flag) {
		if((strlen(add_result) - i) >= 0 && dividend < 2) {
			dividend = (dividend * 10) + (*(add_result + i) - '0');
			i ++;
		}
		first_flag = 0;
	}
	unsigned int quotient = dividend / 2;
	
	remainder = dividend % 2;
	*(klaudia + j) = quotient + '0';
	j ++;			
}
*(klaudia + j) = 0;

return;
}
void get_natalia_apples(const char* total, const char* klaudia, char* natalia) {
    int totallen = strlen(total), klaudialen = strlen(klaudia);

unsigned int borrow = 0;
int i = totallen - 1, j = klaudialen - 1;
int k = totallen - 1;	

while(i >= 0) {
	unsigned int tmp1 = *(total + i) - '0', tmp2 = 0;
	i --;
	if(j >= 0) {
		tmp2 = *(klaudia + j) - '0';
		j --;
	}
	if(borrow) {
		if(tmp1) {
			tmp1 --;
			borrow = 0;
		} else {
			tmp1 = 9;
			borrow = 1;
		}
	}
	unsigned int tmp3 = 0;
	if(tmp2) {
		if(tmp1 < tmp2) {
			borrow = 1;
			tmp1 += 10;
		}
		tmp3 = tmp1 - tmp2;
	} else {
		tmp3 = tmp1;
	} 
	if(tmp3 == 0 && i < 0) {
	}
	else { 
		*(natalia + k) = tmp3 + '0';
		k --;
	}
}

i = 0, j = k + 1;
while( j < totallen) {
	*(natalia + i) = *(natalia + j);
	i ++;
	j ++;
}
*(natalia + i) = 0;

return;
}
int main() {
    unsigned int iters = 10;
while(iters -- > 0) {
	char total_apples[105] = { 0 };		//10 pow 100 is 101characters, 105 shud b more than enuf. 
	char extra_apples[105] = { 0 };

	scanf("%s", total_apples);
	scanf("%s", extra_apples);

	char klaudia_apples[105] = { 0 };
	char natalia_apples[105] = { 0 };

	get_klaudia_apples(total_apples, extra_apples, klaudia_apples);
	printf("%s\n", klaudia_apples);

	get_natalia_apples(total_apples, klaudia_apples, natalia_apples);
	printf("%s\n", natalia_apples);
}

return 0;
}

Sorry, poor coding.. no comments in d code either..
Input output for ref.. (with ur testcases.. Had changed no of iters to 3, just for this..)


[root@localhost spoj]# ./JULKA output_JULKA
[root@localhost spoj]# cat input_JULKA
100
56
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777
9876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210
109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876548
[root@localhost spoj]# cat output_JULKA
78
22
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
4993209877099320987709932098770993209877099320987709932098770993209877099320987709932098770993209879
4883333333888333333388833333338883333333888333333388833333338883333333888333333388833333338883333331
[root@localhost spoj]# cat output_JULKA_spoj
78
22
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
4993209877099320987709932098770993209877099320987709932098770993209877099320987709932098770993209879
4883333333888333333388833333338883333333888333333388833333338883333333888333333388833333338883333331
[root@localhost spoj]#


Plz help..

9 days later

Here, I post my code.. Wen I copied ur output, it looked like thr is an extra blank line after last output(like \n\n), so I tried adding extra newline after d last output, still I got d same WA.. Nywy, this is my original code which takes strictly 10inputs..

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 17 11d

Want to read more? Browse other topics in C and C++ or view latest topics.