713 - Adding Reversed Numbers

All about problems in Volume 7. 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
User avatar
Tariq Shahriar
New poster
Posts: 17
Joined: Wed Mar 01, 2006 8:34 pm
Location: 2nd floor

Post by Tariq Shahriar » Sun Oct 15, 2006 9:58 am

the problem description mentioned
Numbers will be at most 200 characters long.
and also the number may contain leading zeroes.

Code: Select all

03425 3455
i solved by this way-
  • input in a string
  • normally we add two number right to left. here add left to right. as a result this add reversely.
  • the number may contain leading zeroes. But don
[ Common thing of every man is that, everyone thinks that he is uncommon ]

sfelixjr
New poster
Posts: 9
Joined: Wed Apr 25, 2007 3:29 pm
Location: Brazil
Contact:

Please, here is some test, give me the right output

Post by sfelixjr » Tue May 29, 2007 7:16 pm

Guys, My program receveid WA, and i don't know why, i have generated some test and my output for these test, but it's not right, Does anybody can give me the right output?

Input

Code: Select all

500
139144 39097
186290 72395
265374 15315
252337 299805
112027 383722
254786 101464
65053 192248
150698 196960
440760 312400
305979 285456
313564 333849
309019 141815
368890 267849
216173 262773
150326 61110
375936 67036
376378 95972
311248 151250
29394 324927
132454 131380
321546 346713
333896 248003
294732 441492
171118 89249
154480 433109
239199 293352
331379 37096
408296 92050
137378 469040
32316 473418
347200 450963
398419 144658
272712 165791
88000 264547
365904 27612
286896 486329
492327 468804
391013 106355
266668 100933
199208 475299
332535 74319
335558 21651
276239 395077
443899 141052
495763 7361
193641 49056
372445 229132
112345 183847
418020 130759
52669 91458
270664 28673
279394 256020
434216 420124
302470 79026
68139 26864
25424 141137
252249 86514
82023 80081
462542 360309
422717 140863
303178 220038
406732 266695
134126 284291
10096 157782
148631 137143
273545 137638
449883 176707
223714 91279
267559 66830
165631 351320
15775 291141
58387 228464
229727 33271
158326 4541
237116 475349
458031 150199
299634 336083
192509 354360
299747 394552
177869 62211
294707 298840
470888 201852
5396 487023
364540 444929
161073 93221
463647 274538
144871 16660
203159 19115
285910 56613
77699 171716
487410 44630
494060 304083
376199 226407
49934 62803
216105 355634
65645 438684
328919 417640
395413 447751
392305 185838
365511 358971
424828 100019
416977 218121
79753 389452
366335 490784
235495 432324
280960 957
415089 341877
31060 476616
114274 100183
333683 79807
418875 103348
186469 234144
188521 91898
355579 323979
273948 153284
70286 358124
23990 37332
392969 428121
104344 229465
114609 359865
5749 193578
107757 395595
422640 88888
19880 333268
375624 174596
317612 292269
220278 492404
100530 227495
109342 89463
483868 122029
162708 164064
155468 343239
317428 438175
121008 258711
30512 157916
490053 305514
319162 468142
44218 230722
169187 343073
28262 121009
486728 191220
477513 487557
353218 358392
218975 384213
115418 481329
225535 25896
145427 208401
73760 474386
305253 189969
207897 257273
402210 132077
471980 171264
239077 94608
52596 119214
330913 166972
105741 375793
321901 1145
282344 469413
156168 150175
400374 106244
97952 364129
295330 440322
237352 98578
344231 287624
393127 237112
474781 33482
305646 92468
315442 126563
187469 472497
9172 43853
314818 285866
359558 77655
291583 280998
365808 296253
481668 32501
145626 433257
261637 81321
190027 9712
115995 3171
14447 118383
56944 312581
197925 483599
169824 331639
153606 265410
325044 393314
451363 468950
333441 211510
464825 430515
26261 233521
67486 246241
261681 327143
445349 180946
133032 347880
495941 439935
212458 436306
1445 386276
260720 440912
108539 36532
434811 386027
387730 88393
324442 34141
349395 205173
238376 493144
338716 304671
441552 31717
4492 144010
203988 468882
14359 101691
79670 217337
196915 302057
282640 367367
442351 44112
207501 25335
56926 209821
304766 142502
33812 290201
173357 395584
310764 175934
342149 175346
147566 287117
101828 165991
302535 36893
474872 219053
444082 167435
163502 72402
76360 285471
3477 402449
158871 182618
137086 324915
386961 291918
411242 85185
288116 25155
167995 18412
203090 402000
317944 402257
80364 147735
202816 311786
57527 104995
367147 401861
494125 420066
287392 147355
486099 373045
5334 18813
65139 319073
32152 457226
193366 265886
81729 225079
162327 454241
180810 60146
76253 3262
320031 449552
288769 411624
498758 126496
359544 193406
189668 373668
354114 246468
12227 122065
133811 179544
225100 435079
190345 105469
266179 191948
471361 348450
445608 140610
118029 121325
10951 183109
239456 218594
174430 430992
400469 471196
209245 454921
185785 456301
403667 352151
426924 153375
272150 335412
24861 189132
466918 282505
195967 457620
41670 370321
189809 193074
369621 312785
431625 355163
483166 197840
106219 209759
64125 440253
476759 181607
324017 116683
340764 19184
78200 338061
391939 178042
392046 348167
208517 198427
19814 322694
66055 5214
205806 440441
384747 112036
74430 324687
394244 429826
9429 213409
49608 164452
100744 267602
66251 44069
292126 28497
434480 124913
275479 356332
425299 365917
495196 395160
332119 418583
134853 68806
201612 29445
130464 136333
386125 121428
32877 292422
200133 409
189757 426658
489283 99121
359594 442378
491326 91733
477263 695
81144 318182
13440 287939
203991 313828
275945 20448
427695 162861
294535 64090
210517 253309
190516 214816
496858 279640
168662 94604
103680 357938
484128 262629
241187 8743
199904 427596
434737 87041
127427 16568
360786 262312
376687 328527
144161 338065
159982 282560
285686 207952
268378 2145
82394 111901
240045 64484
418493 190188
289405 173748
29791 159252
402706 297927
459361 154133
310397 462817
303999 173819
187410 8630
433895 150688
137997 323395
299815 444482
249083 340323
116327 203831
185737 459261
61791 383069
93374 477079
6429 484016
63621 404191
2574 300678
423245 463586
437251 33088
211917 9152
428052 363510
223890 320224
482655 173908
411985 222963
382933 62751
83693 237998
34110 197373
461087 341958
124176 160835
195862 274515
412286 386970
362961 314477
4618 443778
484012 429999
275653 307300
188001 119371
130019 470623
96859 70474
376540 463871
228176 226234
8252 59722
307169 203661
467455 465109
89409 294258
214414 97021
40981 80638
237779 358899
187666 425169
106846 456337
345301 295600
483811 325887
72381 69571
7100 252471
69554 223859
310899 163389
289230 166630
408117 86199
486446 366649
132121 12324
255347 25770
426920 490215
272234 58137
463217 304087
364828 30151
285208 291607
193416 264926
63887 80576
325334 102099
155551 478334
271497 117044
437190 497882
485078 14989
436421 339999
271057 381762
390811 433199
84345 120232
202065 350898
488079 106073
19716 463537
50118 358727
138663 407086
378875 28577
162286 383703
479622 251781
319729 183954
211091 276526
393216 311346
474505 450600
437006 236414
451509 122764
467825 342613
94177 311255
41842 366503
436186 272854
380509 159009
125591 43519
305577 26126
192225 164836
26581 354084
415031 282459
22210 364085
382565 374664
478809 469171
445753 126903
329195 84601
54517 281342
298201 152090
214739 277414
421675 61226
181387 434777
63301 203571
59126 416823
421907 180584
353627 195966
143307 56094
479341 89179
305218 328650
137895 167805
116163 256433
186486 169186
252955 395513
476935 34724
220932 433443
471850 258326
186311 383859
417059 343095
264946 200528
38336 475875
162536 391190
187878 3081
96893 235486
496851 57974
458267 423578
244536 150402
366546 458332
152170 110595
227112 203447
9594 286870
373462 366198
161143 149548
129788 457341
166266 172829
446924 342722
234405 461323
329563 415332
214501 423052
279441 488250
206310 172835
497394 407220
25390 305211
12597 366474
219514 202182
173858 20861
240830 379049
315177 477896
258179 485267
475460 435190
My Wrong output:

Code: Select all

420125
800251
319425
4422421
495749
3551511
743778
247569
75217
5804361
6463141
4409241
5267401
478847
761426
946207
236109
462498
518877
263735
667269
571999
636135
964509
587589
4235421
6022401
338796
596419
796578
797173
4331771
338414
55547
532134
6632261
8512311
497368
3665021
5655971
85626
541178
5623071
5848421
132963
584112
591677
2951921
548779
440281
457305
426415
8543301
3631
840041
395377
28389
63005
7238411
5625801
5231071
662438
319318
257653
275874
3012831
5166901
145415
826959
417951
349891
702345
551547
503526
6035551
5091211
526718
447869
5843001
799979
483657
6716411
917723
709479
4283
6381851
201581
3942001
74215
848617
82481
798044
5926961
2847
561839
94145
735669
733274
4781431
614592
5248371
624109
87092
7570201
6677101
14896
7568571
786226
214358
32754
5112241
3116041
7512
6784591
3271331
60094
50333
7111901
3238001
4635741
668488
4923531
211531
422178
4402211
5009711
6136721
327926
993082
5068871
2277621
4986971
7456931
379719
452146
795567
777305
672803
4032511
304629
578948
8551701
6026011
592298
5967371
473506
343928
112096
4842231
4541711
534287
543155
185158
634284
496895
470545
435411
642857
2072341
506519
244749
636652
123143
522955
521339
709512
239237
431016
5509571
35583
5996841
36119
4724821
5521611
707678
5788731
84847
72227
422106
252854
871032
5715251
4905631
319026
619358
810324
544951
8943401
495141
811112
588725
5262951
470913
825986
6487541
421876
601642
463959
711938
17177
665852
544569
6225111
632497
758623
58331
6618711
244192
904147
4989621
540018
883471
450951
768191
446278
529321
4698321
485609
4184851
3257731
2667201
660575
683926
502518
887522
949081
749159
231599
4510021
578889
272003
430766
242126
60509
7191021
940485
5135031
679176
768909
8151811
325748
7501351
61263
960464
778746
3591531
423601
517568
78128
9873
769583
6993931
5152551
4430501
4533371
5906721
244236
203465
650279
2957051
3580281
710812
585228
2393441
282719
4470511
505333
8715561
653276
532196
755719
5792001
508562
328842
6494231
543697
786031
2739731
671417
786788
571017
3050781
91404
5583661
4307901
432515
21161
4600811
6312041
3960441
411835
19195
645257
4967731
78987
7141701
165319
551533
367356
13111
477007
558304
5228021
7802171
781357
7407921
713914
495072
266797
4085431
511203
609133
5064161
381593
7919631
319656
73363
139523
312449
5167201
479336
589467
835445
4638161
3053321
666509
15703
450629
6477471
26487
5175011
215157
282118
523009
6942151
472227
332553
482649
472978
934851
884885
5095721
3532531
347272
6996331
504594
7721151
4767191
94151
5834841
4503931
634308
589307
319168
535009
990089
3118101
37916
40402
557088
8867221
767042
136127
781662
543025
5565631
633859
900553
73939
439473
7039361
2849021
360487
798167
676349
805588
8040121
572063
297472
5016321
672341
730422
4444011
32352
5008211
8236541
99249
194624
215201
5856791
5038261
5522831
531011
709609
32953
962471
8193001
4731891
34696
279018
7431951
255361
403157
817145
754505
7672941
665348
4778051
3583421
443641
4273241
524985
3884321
825083
525969
7654211
5537101
7249011
963682
5528541
5841431
551797
859808
5357401
554656
446989
621414
4927831
487518
6055521
825115
6635101
5732731
700548
262926
774033
6099311
4306081
550782
566738
2570611
510994
6974801
586185
6571301
8380801
561766
175206
727413
341391
4812531
43836
5160651
836581
908093
5024911
4495931
604257
271142
623968
2947011
362696
2466631
548478
714285
653385
620286
4602701
7501451
4644741
759146
454627
485088
104427
966602
8718361
394938
715978
262666
420659
14638
6306511
2017811
5761301
2390951
788656
695728
734995
637553
658791
378155
894615
558121
481455
411796
371578
510979
7820741
6344371
801651
Thanks

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed May 30, 2007 8:48 am

Your cases are correct.
Ami ekhono shopno dekhi...
HomePage

sfelixjr
New poster
Posts: 9
Joined: Wed Apr 25, 2007 3:29 pm
Location: Brazil
Contact:

What should i do?

Post by sfelixjr » Wed May 30, 2007 2:01 pm

Man, what the hell is going on with UVA ???
I get WA over and over again.
Can u give any advice to fix this problem?
please, help me

Does anybody can see my Java code? I give up

Code: Select all

Solved
Last edited by sfelixjr on Wed May 30, 2007 2:54 pm, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: What should i do?

Post by Jan » Wed May 30, 2007 2:26 pm

sfelixjr wrote:Man, what the hell is going on with UVA ???
I get WA over and over again.
Can u give any advice to fix this problem?
please, help me
There is no problem with uva (for this problem) I think. May be your code is not fully correct. Try the cases.

Input:

Code: Select all

2
1000 1000
2000 2000
Output:

Code: Select all

2
4
Hope these help.
Ami ekhono shopno dekhi...
HomePage

sfelixjr
New poster
Posts: 9
Joined: Wed Apr 25, 2007 3:29 pm
Location: Brazil
Contact:

Post by sfelixjr » Wed May 30, 2007 2:53 pm

Jan, thanks, it was the only case that was wrong!!
I got AC

anowar_jaman
New poster
Posts: 2
Joined: Mon Sep 29, 2008 4:14 am

WA 713 - Adding Reversed Numbers. Help me plz!!

Post by anowar_jaman » Mon Sep 29, 2008 4:25 am

I checked and rechecked along with debugging my code many times, but could not find where the bug is. I developed my own big_int class and tried to solve the problem with it. Here I am only giving the body of the methods used in main function. Would you plz see my code and tell me where I am doing the mistake. I also checked for several test datas found in this thread. Thanks in advance, I really need ur help.. :(

Code: Select all

#include <string>
#include <iostream>
#include <cstdio>
using namespace std;

class big_int{
public:
	string num;

protected:
	big_int operator+(string a)
	{
		big_int s;
		
		s = num + a;

		return s;
	}

	big_int operator+(char* a)
	{
		big_int s;

		s = num + a;

		return s;
	}

public:
	big_int(){num = "";}
	big_int(string a){num = a;}

	void reverse(); //ok

	void remove_0();

	size_t size(){return num.size();}

	string get(){return num;} //ok

	//*string get_ref(){return &num};

	big_int root();

	big_int operator=(big_int a); //ok
	big_int operator=(string a); //ok
	big_int operator=(int a); //ok
	big_int operator=(char *a); //ok

	big_int operator+(big_int a); //ok
	big_int operator+(int a); //ok

	big_int operator-(big_int a);
	big_int operator-(int a);

	big_int operator*(big_int a); //ok
	big_int operator*(int a);//ok

	big_int operator/(big_int a); 
	big_int operator/(int a); // ok

	bool operator<(big_int a);//ok
	bool operator>(big_int a);//ok
	bool operator==(big_int a);//ok
	bool operator!=(big_int a);//ok
	bool operator<=(big_int a);//ok
	bool operator>=(big_int a);//ok
};

big_int big_int::operator=(big_int a)
{
	num = a.num;

	return *this;
}
big_int big_int::operator=(string a)
{
	num = a;

	return *this;
}
big_int big_int::operator=(int a)
{	
	num = "";

	while(a)
	{
		num.push_back(a%10 + '0');
		a /= 10;
	}

	reverse();
	remove_0();

	return *this;
}
big_int big_int::operator=(char* a)
{
	string b = a;
	num = b;

	return *this;
}
big_int big_int::operator+(big_int a)
{

	int len, len_a, carry, i, j, add;

	len = num.size();
	len_a = a.num.size();

	big_int s("");

	carry = 0;

	i = len-1;
	j = len_a - 1;

	while(i>=0 && j>=0)
	{
		add =   num[i--] + a.num[j--] - '0' - '0' + carry;
		s.num.push_back(add%10 + '0');
		carry = add/10;
	}

	while(i>=0)
	{
		add = num[i--] - '0' + carry;
		s.num.push_back(add%10 + '0');
		carry = add/10;
	}

	while(j>=0)
	{
		add = a.num[j--] - '0' + carry;
		s.num.push_back(add%10 + '0');
		carry = add/10;
	}

	while(carry)
	{
		s.num.push_back(carry%10 + '0');
		carry /= 10;
	}

	s.reverse();
	s.remove_0();

	return s;
}

void big_int::reverse()
{
	int i, j;

	j = num.size() - 1;

	for(i=0; i<j; i++, j--)
		num[i] ^= num[j] ^= num[i] ^= num[j]; 

	return;
}

void big_int::remove_0()
{
	if(num[0] == '0')
	{
		int i = (int)num.find_first_not_of('0');
		num.erase(0, i);
	}

	if(num.size() == 0)
		num = "0";

	return;
}


char a[250], b[250];

int main()
{
	int i;
	big_int n1, n2, sum;

	scanf("%d", &i);

	while(i--)
	{
		scanf(" %s %s", a, b);

		n1 = a;
		n2 = b;

		n1.remove_0();
		n2.remove_0();

		n1.reverse();
		n2.reverse();

		sum = n1 + n2;

		sum.reverse();
		sum.remove_0();

		cout<<sum.num<<endl;
		/*int len = sum.num.size();
		for(int j=0; j<len; j++)
			printf("%c", sum.num[j]);
		printf("\n");*/
	}

	return 0;
}

naffi
New poster
Posts: 23
Joined: Wed Mar 19, 2008 12:25 pm
Location: BUET, Bangladesh
Contact:

Hints:713 - Adding Reversed Numbers

Post by naffi » Sat Oct 25, 2008 6:39 am

Those who wants some hints:

1. for input like 1000 1, my output is 2000.
2. for input like 1000 1000, output is 2000.
3. for input like 00010 9, output is 90010.
4. No need to reverse at all.

And, I got AC in 0.000. :)
Always At Your Help.

tanvir_cse
New poster
Posts: 9
Joined: Wed Jul 09, 2008 10:12 pm

Re HINTS : 713 - BUT Hints !!!!

Post by tanvir_cse » Sun Dec 07, 2008 12:54 am

But my AC code give different output :

for 1000 1 output = 2
for 1000 1000 output = 2
for 00010 9 output = 9001

I think my procedure according to the problem description is OK.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 713 - Adding Reversed Numbers

Post by Obaida » Fri Dec 26, 2008 7:18 am

Some one please help me i got WA!!!

Code: Select all

removed
Last edited by Obaida on Sat Jan 03, 2009 6:27 am, edited 1 time in total.
try_try_try_try_&&&_try@try.com
This may be the address of success.

ExUCI
New poster
Posts: 14
Joined: Sat Aug 12, 2006 3:31 am
Location: USA

Re: 713 - Adding Reversed Numbers

Post by ExUCI » Mon Dec 29, 2008 12:34 pm

Your code has problems with zeroes

try 0 0
Remove your code after AC :)

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 713 - Adding Reversed Numbers

Post by Obaida » Sun Jan 18, 2009 10:12 am

I think a simple mistake leading me to Wa. Some one please give me some test case.
I got several wa in this problem. :(

Code: Select all

removed
Last edited by Obaida on Mon Jan 19, 2009 7:44 am, edited 1 time in total.
try_try_try_try_&&&_try@try.com
This may be the address of success.

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 713 - Adding Reversed Numbers

Post by vahid sanei » Sun Jan 18, 2009 11:34 am

try this

Code: Select all

999 999
ans is 1998
Impossible says I`m possible

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 713 - Adding Reversed Numbers

Post by Obaida » Sun Jan 18, 2009 12:08 pm

Why the output is 1998 :o :o :o

both of the number is 999
so there reverse will be 999
now if we add them

Code: Select all

 999
 999
-----
1998
According to this line.

Code: Select all

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers.
Now if we reverse the answer then it will be 8991.
try_try_try_try_&&&_try@try.com
This may be the address of success.

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 713 - Adding Reversed Numbers

Post by vahid sanei » Sun Jan 18, 2009 1:45 pm

oh I`m sorry :oops: :D
i solved this problem and i didn`t read that again
try these cases ...

Code: Select all

23  3
and
3  23
your ans !!!

Code: Select all

53
26
Impossible says I`m possible

Post Reply

Return to “Volume 7 (700-799)”