Problem Statement
***Note: Please keep programs under 7000 characters in length. Thank you
Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int
Define the function S(x) as the sum of the squares of the digits of x.
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.
Define the set T(x) to be the set of unique numbers that are produced by
repeatedly applying S to x. That is: S(x), S(S(x)), S(S(S(x))), etc...
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42.
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37.
Note this sequence will repeat so we can stop calculating now and:
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x.
Implement a class SquareDigits, which contains a method smallestResult. The
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.
The method signature is (be sure your method is public):
int smallestResult(int n);
TopCoder will ensure n is non-negative and is between 0 and 199 inclusive.
Examples:
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.
If n=2: T(0) through T(10) do not contain the value 2. If x=11, however:
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.
If n=10: T(0) through T(6) do not contain 10. If x=7:
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat...
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.
n=1 -> x=1
n=19 -> x=133
n=85 -> x=5
n=112 -> x=2666
Definition
Class:
SquareDigits
Method:
smallestResult
Parameters:
int
Returns:
int
Method signature:
int smallestResult(int param0)
(be sure your method is public)
topcoder练习第二题(500分),应该说也不难,但由于太久没编程也让我搞得一团糟。问题主要集中在两个方面,ArrayList的使用和数字与字符串之间的相互转换:
- ArrayList用来表示题目中提到的T集合,对循环构造T集合的终止条件开始没把握清楚,应该是不包含重复元素时循环。最初的想的条件是集合不空,结果导致死循环。
- 数字字符之间转换问题还是比较典型,我也看了很多网上的资料。在本题中由于需要按位求平方和,需要取没位的数字值,在总位数不确定的情况下用取模取余的方法显然不太合适,因此想到用转换的方法。以下是求平方和代码段:
- public static int S(int num) {
int sum = 0;
String snum = String.valueOf(num);
for (int i = 0; i < snum.length(); i++) {
String t=String.valueOf(snum.charAt(i));
int temp=Integer.parseInt(t);
sum += Math.pow(temp, 2);
}
return sum;
}
- 主要用了valueof和parseInt两个方法,先从数字转为字符再从字符转为数字。前一个没什么好说,parseInt却让我郁闷了一阵,因为其参数只能是字符串而我一直以为包括字符从而得不到正确结果。
从网上又搜了其他人的做法,大差不差:
- 最简单的办法: String s=""; int i=1; s=i+"";
- 将一个已知的字符如‘56’转换成可计算的数字56 :
'56'必须是字符数组: char[] ch = {'5', '6'}; String s = String.valueOf(ch); int i = Integer.parseInt(s); System.out.println(i); 字符的话: char ch = '5'; String s = String.valueOf(ch); int i = Integer.parseInt(s); System.out.println(i); 字符串的话直接: String s = "56"; int i = Integer.parseInt(s); System.out.println(i);
-
各种数字类型转换成字符串型: String s = String.valueOf( value); // 其中 value 为任意一种数字类型。 字符串型转换成各种数字类型: String s = "169"; byte b = Byte.parseByte( s ); short t = Short.parseShort( s ); int i = Integer.parseInt( s ); long l = Long.parseLong( s ); Float f = Float.parseFloat( s ); Double d = Double.parseDouble( s );
本题源代码:
public class SquareDigits {
/** public static int smallestResult(int n) {
for(int i=0; ;i++) { if (T(i).contains(n))
return i;
} }
public static ArrayList T (int n) { ArrayList Tlist=new ArrayList(); int temp=S(n); while(!Tlist.contains(temp)) { Tlist.add(temp); temp=S(temp); } return Tlist;
}
public static int S(int num) { int sum = 0; String snum = String.valueOf(num); for (int i = 0; i < snum.length(); i++) {
String t=String.valueOf(snum.charAt(i)); int temp=Integer.parseInt(t); sum += Math.pow(temp, 2); }
return sum;
}
public static void main(String[] args) { // TODO code application logic here int number = 2; int result=smallestResult(number); System.out.println(result); } }
相关推荐
python教程学习
tdac034.pdf
veclibm111111111111
nssm工具,进行2次解压后即可
视讯镜头优化,操作不走
【数据结构】的资源,包括书籍、在线课程和网站
北京大学-DeepSeek系列-提示词工程和落地场景.pdf.png
【财信国际经济研究院-2025研报】2025年2月CPI和PPI数据点评:通胀低于预期,内需亟待提振.pdf
【毕业设计】java-springboot+vue精品在线试题库系统源码(完整前后端+mysql+说明文档+LunW).zip
python安装-25.求 a 的 b 次方——有点不好意思哈.py
代码实现了自适应IMM(交互式多模型)算法,专注于对目标状态进行估计,并结合了匀速(CV)和匀加速(CA)运动模型。它使用自适应观测噪声估计和扩展卡尔曼滤波器(EKF)作为滤波主体,并在最后提供误差统计特性输出。
中产品工业物联网联网的基础功能,是企业开启数字化转型的入门之选。它能帮助企业快速搭建起基础的设备数据采集与监控体系,以较低成本实
北京大学DeepSeek系列-DeepSeek与AIGC应用.pdf.png
【毕业设计-java】springboot-vue基于多维分类的知识管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
## 一、数据介绍 本数据参考C刊《管理评论》佟岩(2024)老师的做法,使用各年度省级政府工作报告中“减碳”关键词的词频总数来测度地方政府对“减碳”的重视程度,“减碳”关键词包括二氧化碳、低碳、减排、节能、能耗、环境保护(环保)、生态、绿色。 本数据包含:原始数据、参考文献、代码do文件、最终结果。 ## 二、参考文献: 佟岩,李鑫,徐国铨.企业集团碳减排压力与债务分布-来自高耗能上市公司的经验证据[J].管理评论,2024,36(02):210-221.DOI:10.14120/j.cnki.cn11-5057/f.2024.02.014 ## 三、相关数据:城市、城市编码、年份、减碳重视程度词频。
python教程学习
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
python学习一些项目和资源
锐捷设备命令最全大合集
uniapp-machine-learning-from-scratch-05.rar