关于字符串与数字转换

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却让我郁闷了一阵,因为其参数只能是字符串而我一直以为包括字符从而得不到正确结果。

从网上又搜了其他人的做法,大差不差:

  1. 最简单的办法: String s=""; int i=1; s=i+"";
  2. 将一个已知的字符如‘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);
  3.  各种数字类型转换成字符串型: 
    
    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);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值