TopCoder 250 points 13-SRM 150 DIV 1 82.63/250 33.05%
来源:互联网 发布:linux svn权限控制 编辑:程序博客网 时间:2024/06/07 04:46
这道题有点意思,虽然花得时间有点长,不过还是一次做对的,所以打算写点感想。
题目的大概意思是这样的:
首先告诉你在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
Class: InterestingDigits Method: digits Parameters: int Returns: int[] Method signature: int[] digits(int base) (be sure your method is public)
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)10
Returns: { 3, 9 }
All other candidate digits fail for base=10. For example, 2 and 5 both fail on 100, for which 1+0+0=1. Similarly, 4 and 8 both fail on 216, for which 2+1+6=9, and 6 and 7 both fail for 126, for which 1+2+6=9.1) 3
Returns: { 2 }
2)
9
Returns: { 2, 4, 8 }
3)
26
Returns: { 5, 25 }
4)
30
Returns: { 29 }
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;elsereturn false;}}
- TopCoder 250 points 13-SRM 150 DIV 1 82.63/250 33.05%
- TopCoder 250 points 14-SRM 150 DIV 2 86.25/250 34.50%
- TopCoder 250 points 3-SRM 145 DIV 1 84.71/250 33.88%
- TopCoder 250 points 9-SRM 148 DIV 1 75/250 30%
- TopCoder 250 points 11-SRM 149 DIV 1 86.62/250 34.65%
- TopCoder 250 points 15-SRM 151 DIV 1 121.73/250 48.69%
- TopCoder 250 points 17-SRM 152 DIV 1 106.51/250 42.60%
- TopCoder 250 points 19-SRM 153 DIV 1 84.72/250 33.89%
- TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%
- TopCoder 250 points 6-SRM 146 DIV 2 162.67/250 65.07%
- TopCoder 250 points 8-SRM 147 DIV 2 176.36/250 70.54%
- TopCoder 250 points 10-SRM 148 DIV 2 167.20/250 66.88%
- TopCoder 250 points 12-SRM 149 DIV 2 103.92/250 41.57%
- TopCoder 250 points 16-SRM 151 DIV 2 97.63/250 39.05%
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%
- TopCoder 250 points 20-SRM 153 DIV 2 216.58/250 86.63%
- TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%
- TopCoder 300 points 24-SRM 155 DIV 2 75/250 30%
- NSArray 和 NSMutableArray 的区别
- 深度学习: 推进人工智能的梦想
- Android去掉标题的方法
- Android杂谈-RelativeLayout中的baseline是什么?
- 3ds max 导出插件——md5Exporter完结篇
- TopCoder 250 points 13-SRM 150 DIV 1 82.63/250 33.05%
- 查找数组中相同的元素
- java中源代码和lib库中有包名和类名都相同的类
- 下拉框spinner:获取选中值的几种方法 .
- Android应用在不同版本间兼容性处理 .
- Maven仓库管理-Nexus
- 利用折半查找,寻找元素在数组中合适恰当的位置
- alsa的snd_pcm_readi()函数和snd_pcm_writei()
- linux io分析