题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1114
题意:给定一个数n,求满足条件大于等于n的最小的完数。
分析:我们先把前13个完数存进数组,然后比较即可。因为第13个完数的位数已经大于100位了,所以再大的就用不着了。
再利用Java的BigInteger进行比较很方便。
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String[] str = {
"6",
"28",
"496",
"8128",
"33550336",
"8589869056",
"137438691328",
"2305843008139952128",
"2658455991569831744654692615953842176",
"191561942608236107294793378084303638130997321548169216",
"13164036458569648337239753460458722910223472318386943117783728128",
"14474011154664524427946373126085988481573677491474835889066354349131199152128",
"23562723457267347065789548996709904988477547858392600710143027597506337283178622239730365539602600561360255566462503270175052892578043215543382498428777152427010394496918664028644534128033831439790236838624033171435922356643219703101720713163527487298747400647801939587165936401087419375649057918549492160555646976"
};
BigInteger data[] = new BigInteger[15];
for(int i=0;i<13;i++)
data[i] = new BigInteger(str[i]);
while(cin.hasNextBigInteger()){
BigInteger n = cin.nextBigInteger();
for(int i=0;i<13;i++){
if(data[i].compareTo(n) == 0 || data[i].compareTo(n) == 1){
System.out.println(data[i]);
break;
}
}
}
}
}