版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/86530994
PAT 1120 Friend Numbers
java 版
1.题意
如果两个数的各个位数和加在一起相等,则称这两个数是朋友数。
现在给出一串数字,然后求出有多少个朋友数,那么这些朋友数的每位数的和是多少?
2.分析
使用一个方法专门求每个数的和。
因为输入的数不超过10000,所以最大的和是9*4 = 36。 那么使用一个大小为50的数组保存所有数字的所有位数和就ok。报错的方式是:以数的各个位的和作为下标,默认为0,如果有值,则设置为1。最后输出值为1的下标即可。
3.代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class P1120 {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int n = Reader.nextInt();
int i = 0;
int num ;
int sum ;
int count = 0; //表示和数目
//9999 => 36
int array[] = new int[50];
for(i = 0;i < n;i++) {
num = Reader.nextInt();
sum = calSum(num);
if (array[sum] != 1) {
count++;
array[sum] = 1;
}
}
System.out.println(count);
for(i = 0;i< 50;i++) {
if (array[i] != 0) {
if (count > 1) {
System.out.print(i+" ");
count--;
}
else
System.out.print(i);
}
}
}
//计算每个数字的 各个位数的和
static int calSum(int number) {
int sum = 0 ;
int temp ;
while (number != 0) {
temp = number%10;
number = number / 10;
sum += temp;
}
return sum;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) throws IOException {
reader = new BufferedReader(new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
//这里初始化tokenizer只是为了进入下面的while()循环,而不是别的原因。
//那么还有优化的空间么?
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {//如果后面还有数据,则直接返回
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine() );//否则,读取下一行
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
//获取字符 => 因为 next()方法返回的是一个String
static char nextChar() throws IOException {
return next().charAt(0);
}
}
4.执行结果
5.优化总结
没想到就这个答案,还有129ms 的耗时,但是我也想不到有哪里可以改进的了。望过路大神指出。