Thursday, January 1, 2009

MegaCoolNumbersEasy

A positive integer is called a mega cool number if its digits form an arithmetic progression. An arithmetic progression is a sequence of numbers in which the difference between any two consecutive numbers is the same. Return the number of mega cool numbers between 1 and N, inclusive.

Hi guys

Problem is the exclusive and proprietary property of SomeCompany, Inc. Any unauthorized use or reproduction of this information without the prior written consent of SomeCompany, Inc. is strictly prohibited.

Solution I wrote:

class MegaCoolNumbersEasy{
public:
int count(int N){
int nc=N>99?99:N;
if(N<100) return nc;
int tmp=0; int td=N<1000?N/100:9;
for(int j=1; j<= td; j++){
for(int i=-4; i< 5; i++){
if( i+j >=0 && i+j <=9 && j+2*i <=9 && j+2*i >=0){
tmp=j*100+(j+i)*10+(j+i+i);
if(N>= tmp) nc++;
tmp=0;
}
}
}
return nc;
}
} ;


And solution someone find for me is:
http://hi.baidu.com/liveroom/blog/item/22d9e645d9cda33886947343.html

Please comment which one is better.


I am not using for any commercial use. Please let me know if I am still violating the Rule.

No comments: