|
|
вернуться в форумLucky Tickets prblm help Послано ashwin 18 ноя 2010 17:21 Hello all i am new to programming problems and i want to improve. I am having trouble understanding this problem. The ticket is lucky if the sum of first half of digits is equal to the sum of next half of digits rite?? that is if the number is 23 then 2+0 must be equal to 3+0 rite??? if that is the case i am having trouble regarding my program in my program i break up the digits for example for 2 digit numbers i have two vars d1 and d2 i have a loop from n=10 to n<=99 if the number is 24(i am considering only two digit numbers) then d1 is 2 and d2 is 4 and then i check the condition using if((d1+0)==(d2+0)) count++; i know my code would be very long for nums with more than 2 digits but even for this i am not getting the answer..I just dont understand the mathematics part of this program on how to split the numbers and how to run the loop. any help would be greatful Re: Lucky Tickets prblm help Послано Artem 19 ноя 2010 02:01 No. You are some wrong: E.g.: number 0000 is lucky - couse 0 + 0 = 0 + 0 numbe 1221 is lucky - couse 1 +2 = 2 +1 see ? Sum of first half of digits must be uqual to sum of second half. understand ? So if to do it with brut-force you'll have O(n^n) as I see. Re: Lucky Tickets prblm help Послано ashwin 20 ноя 2010 09:06 Hi thanks for replying... For two digit numbers how do i split them???? if the number is 24 then i should check whether 2+0==4+0?? How do i initiate a loop then?? Also i read in the discussions here that many program uses pow() fucntion and they calculate with N/2 numbers.. I dont understand that can anybody plz explain that?? Re: Lucky Tickets prblm help Послано Ilya 4 июн 2011 17:19 You shouldn't check whether 2+0==4+0, use one of these ways: 1. int count = 0; for( int number = 0; number <= 99; number++ ){ //Loop through all tickets int d1 = number/10; // High bit int d2 = number%10; // Low bit if( d1==d2 ) count++; } 2. int count = 0; for( int d1 = 0; d1 <= 9; d1++ ) for( int d1 = 0; d1 <= 9; d1++ ) count++; But for n>2 it will be impossible to use this solution. I think you can found solution of this problem in Google. |
|
|