这道题有点意思,虽然花得时间有点长,不过还是一次做对的,所以打算写点感想。
题目的大概意思是这样的:
首先告诉你在10进制数的8个数中(0和1除外)有一些特别的数(比如3或9),把这些数乘以任何一个数,把所得到的结果的数的各位数字相加的和仍然能被这个特别的数整除,比如118*3=354,3+5+4=12,12忽仍然可以被3整除。又比如9,75*9=675, 6+7+5=18,18仍然可以被9整除。
现在的输入是:
给定N制的整数3<=N<=30,
输出:
求出这些N进制数当中的特别的数,并以int[ ] 返回。
例子:
比如像30进制的数,29就是这个特别的数
29 * 999 =28971 28971转换成30进制的数以后就是 125(21) 。1+2+5+21=29 刚好能被29整除
思路:
主要就是isCanDivided函数,用于将十进制的数转成N进制的数,并且计算各位之和。
int len = (int) (Math.log(sum) / Math.log(base));
(int)log(28971)/log(30) = 3,也就是(3+1)位
Problem Statement | |||||||||||||
The digits 3 and 9 share an interesting property. If you take any multiple of 3 and sum its digits, you get another multiple of 3. For example, 118*3 = 354 and 3+5+4 = 12, which is a multiple of 3. Similarly, if you take any multiple of 9 and sum its digits, you get another multiple of 9. For example, 75*9 = 675 and 6+7+5 = 18, which is a multiple of 9. Call any digit for which this property holdsinteresting, except for 0 and 1, for which the property holds trivially. A digit that is interesting in one base is not necessarily interesting in another base. For example, 3 is interesting in base 10 but uninteresting in base 5. Given an intbase, your task is to return all the interesting digits for that base in increasing order. To determine whether a particular digit is interesting or not, you need not considerall multiples of the digit. You can be certain that, if the property holds for all multiples of the digit with fewer than four digits, then it also holds for multiples with more digits. For example, in base 10, you would not need to consider any multiples greater than 999. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | When base is greater than 10, digits may have a numeric value greater than 9. Because integers are displayed in base 10 by default, do not be alarmed when such digits appear on your screen as more than one decimal digit. For example, one of the interesting digits in base 16 is 15. | ||||||||||||
Constraints | |||||||||||||
- | base is between 3 and 30, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
import java.util.ArrayList;
public class InterestingDigits {
public static int[] digits(int base) {
ArrayList<Integer> list = new ArrayList<Integer>();
int sum = 0;
for (int j = 2; j < base; j++) {
int i = 2;
for (; i <= 999; i++) {
sum = j * i;
if (!isCanDivided(base, j, sum))
break;
}
if (i > 999)
list.add(j);
}
int n = list.size();
int temp[] = new int[n];
for (int i = 0; i < n; i++)
temp[i] = list.get(i);
return temp;
}
private static boolean isCanDivided(int base, int currentnum, int sum) {
int len = (int) (Math.log(sum) / Math.log(base));
int transSum = 0;
int temp = 0;
int exp = 0;
for (int i = len; i >= 0; i--) {
exp = (int) Math.pow(base, i);
temp = sum / exp;
transSum += temp;
sum -= temp * exp;
}
if (transSum % currentnum == 0)
return true;
else
return false;
}
}