每日一题10

小绵羊 2011-05-27 08:49:48
求下面100个50位数字之和的前10位

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690
...全文
814 53 打赏 收藏 转发到动态 举报
写回复
用AI写文章
53 条回复
切换为时间正序
请发表友善的回复…
发表回复
后街男孩 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhuyucheng123 的回复:]

用String.subString()方法截取每个数字的前十位,然后用Integer.praseInt(String)方法将截出来的String对象转型成整型,然后你想做什么就可以当成整数去用了!
[/Quote]
不好意思,应该是把前十一位截出来。因为一百个数相加最有高位的相加进位为最小是两位,而最低位相加进位最多是三位,而结果只要前十位,所以必须再取一位才能满足进位条件,最后所要结果精度不丢失,所以是截取十一位。最后把运算结果的结果前十位截出来就好了。
当然后来的老兄说截取十三位也是没错的,因为位数越多无用的计算就越多,结果也是对的。因为你也可以不截取,直接计算……
xxxxxx130 2011-05-30
  • 打赏
  • 举报
回复
呵呵,php版的
$dataArray = array("37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690");
$times = strlen($dataArray[0])/10;
$length = 10;
$result = 0;
for ($i=0;$i<$times;$i++){
$numbers = null;
foreach ($dataArray as $data){
$numbers[] = substr($data, $length*($times-$i-1) , $length);
}
foreach ($numbers as $number){
$result += $number;
}
$result = $i == ($times - 1)?substr($result, 0, 10):substr($result, 0, -10);
}
echo $result;
iamsheldon 2011-05-30
  • 打赏
  • 举报
回复
用字符数组 再加上四则运算的基本原理就行了, 以前用C语言写过,用java就更加简单了。
  • 打赏
  • 举报
回复

arr[5000];
int x = 0;
String y[]= new String[50];
for(int i = 1; i <= 50; i ++){
int sum = 0;
for(int j = 1; j <= 100; j++){
sum = sum + arr[50*j - i];
}
int temp = x + sum;
x = temp / 10;
y[i - 1] = String.valueOf(temp % 10);
}
for(int k = y.length - 1; k >= 0; k--){
System.out.print(y[k]);
}
janchin 2011-05-28
  • 打赏
  • 举报
回复
13737623039087663730204874683298597177365[9831892726]
TKD03072010 2011-05-28
  • 打赏
  • 举报
回复
[Quote=引用 49 楼 x19881216 的回复:]

引用 39 楼 softroad 的回复:

引用 33 楼 softroad 的回复:

Java code
BigDecimal sum = new BigDecimal(0);

for (int i = 0; i < values.length; i++) {
sum = sum.add(new BigDecimal(values……
[/Quote]
哈哈 期待 第11题的出现
小绵羊 2011-05-28
  • 打赏
  • 举报
回复
[Quote=引用 39 楼 softroad 的回复:]

引用 33 楼 softroad 的回复:

Java code
BigDecimal sum = new BigDecimal(0);

for (int i = 0; i < values.length; i++) {
sum = sum.add(new BigDecimal(values[i]));
}

……
[/Quote]
大数运算就失去算法的意义了
pmars 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 e9876 的回复:]

100个不超过10的数字的最大值肯定不超过1000
这样的话,取出前13位计算就OK了。
[/Quote]
的确是这样子的!
跑出来的结果
553737623039036
取前十位就是答案了,按照ls的答案来说,应该是正确的!
pmars 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 e9876 的回复:]

100个不超过10的数字的最大值肯定不超过1000
这样的话,取出前13位计算就OK了。
[/Quote]
是一个好方法!
pmars 2011-05-27
  • 打赏
  • 举报
回复
高精度可以算出来!
但有没有不用算出来的好方法?
求算法大神来解答!!!!!
e9876 2011-05-27
  • 打赏
  • 举报
回复
100个不超过10的数字的最大值肯定不超过1000
这样的话,取出前13位计算就OK了。
shine333 2011-05-27
  • 打赏
  • 举报
回复
刚刚又测试了一下,有问题,需要打补丁。
shine333 2011-05-27
  • 打赏
  • 举报
回复

String[] array = new String[100];
for (int i = 0; i < array.length; i++) {
array[i] = RandomStringUtils.random(1, "123456789")
+ RandomStringUtils.randomNumeric(49);
}
int n = 10;
long result = 0;
int remainder = 0;
BigInteger bi = BigInteger.ZERO;
for (String str : array) {
result += Long.parseLong(str.substring(0, n + 2));
remainder += Integer.parseInt(str.substring(n + 2, n + 4));
bi = bi.add(new BigInteger(str));
}
System.out.println(result);
System.out.printf("%16d%n", remainder);
System.out.println("-----------------");
if (remainder + (result % 100) * 100 >= 10000) {
result += 100;
}
System.out.println(result / 100);
System.out.println(bi);
String actual = new Long(result / 100).toString();
String expected = bi.toString();
if (!expected.startsWith(actual)) {
throw new RuntimeException();
}


写抛一个,答案貌似正确的(测试了很多次随机数组)。不过我现在还是不能100%确保。
理论,后面被舍弃的40位,最大可能值9999...99*100也就是9999...9900,也就是说前10位,可能受到第11-12的影响。
yuanxl33 2011-05-27
  • 打赏
  • 举报
回复
这题是不是在考查
统计学的概念?
我记得 四舍五入,5前面是偶数则舍,奇数则入
即 12.5 ~=12 ,13.5 ~=14,
然后这题就是
截取前12位,四舍五入成11位。
最后结果第11位数值是不准确,但是前10位有效
aboutin 2011-05-27
  • 打赏
  • 举报
回复
学习了

脱管态的狗 2011-05-27
  • 打赏
  • 举报
回复
结果是:

top 10 bit:5537376230
sum=5537376230390876637302048746832985971773659831892672
脱管态的狗 2011-05-27
  • 打赏
  • 举报
回复
考虑到四舍五入则取前十位加起来,结果四舍五入的前十位即可
如果不是四舍五入而是舍去的话,就要全加了
我的全加,舍后面的,只要前十位的答案:


/********************************************************************
purpose: 求下面100个50位数字之和的前10位
*********************************************************************/

#include <iostream>

using namespace std;

void SumString(string* strs, int len, int perNumLen, int* sumBuf, int extLen)
{
int sumBit = 0, i;
while(perNumLen--)
{
i = len;
while(i--)
sumBit += strs[i][perNumLen]-'0';
sumBuf[perNumLen+extLen] = sumBit % 10;
sumBit /= 10;
}
while(extLen--)
{
sumBuf[extLen] = sumBit % 10;
sumBit /= 10;
}
}

void Test_Top10BitOfSumBigNum()
{
string strs[] = {
"37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690"
};
const int PerNumLen = 50;
const int NumCount = 100;
const int ExtLen = 2;

int sumBuf[PerNumLen+ExtLen];

SumString(strs, NumCount, PerNumLen, sumBuf, ExtLen);

cout<<"top 10 bit:";
int j = -1;
while(!sumBuf[++j]);
for (int i = 0; i < 10; i++)
{
cout<<sumBuf[j+i];
}
cout<<endl;

cout<<"sum=";
for (; j < PerNumLen+ExtLen; j++)
{
cout<<sumBuf[j];
}
cout<<endl;

}
chen09 2011-05-27
  • 打赏
  • 举报
回复
输出:
5537376230390876637302048746832985971773659831892672


public class LargeNumber {

public static String add(String str1, String str2) {
StringBuffer sb = new StringBuffer();

byte[] bytes1 = str1.getBytes();
for (int i = 0; i < bytes1.length; i++) {
bytes1[i] -= '0';
}

for (int i = 0; i < bytes1.length / 2; i++) {
byte tmp = bytes1[i];
bytes1[i] = bytes1[bytes1.length - i - 1];
bytes1[bytes1.length - i - 1] = tmp;
}

byte[] bytes2 = str2.getBytes();
for (int i = 0; i < bytes2.length; i++) {
bytes2[i] -= '0';
}
for (int i = 0; i < bytes2.length / 2; i++) {
byte tmp = bytes2[i];
bytes2[i] = bytes2[bytes2.length - i - 1];
bytes2[bytes2.length - i - 1] = tmp;
}

byte carry = 0;
int i = 0;
while (true) {
if (i >= bytes1.length && i >= bytes2.length)
break;
byte sum = carry;
if (i < bytes1.length)
sum += bytes1[i];
if (i < bytes2.length)
sum += bytes2[i];
carry = (byte) (sum / 10);
sb.append(sum % 10);
i++;
}
if (carry > 0)
sb.append(carry);

return sb.reverse().toString();

}

public static void main(String[] args) {
String[] strs = { "37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690" };

String str = strs[0];
for (int i = 1; i < strs.length; i++) {
str = add(str, strs[i]);
}
System.out.println(str);

}

}

zhrubin 2011-05-27
  • 打赏
  • 举报
回复
感觉伪代码就是:
0.i = 0;carry[0]=0;long resultNumber[0]=0;long number1=0,number2=0;
1.从后面截取数1的第i个20位放入number1
2.从后面截取数2的第i个20位放入number2
3.resultNumber[i] = number1+number2+carry[i];
4.截取resultNumber从第10位开始的数,作为进位放到carry[i+1]
5.i++;
6.while(i < 2) goto 1
7.从前面截取数1的第1个10为,放入number1.
8.从前面截取数2的第1个10为,放入number2.
9.result = number1+number2+carry[2]
诚朴勇毅 2011-05-27
  • 打赏
  • 举报
回复

import java.io.*;
public class AddLongNum {

public static void main(String[] args){
try {
BufferedReader readNum=new BufferedReader(new FileReader("num.txt"));
String num[]=new String[100];
for(int i=0;i<100;i++){
try {
num[i]=readNum.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
int[] carry1=new int[50],carry2=new int[50],//每位对应的进位
result=new int[50];//该位结果
for(int i=0;i<50;i++){//从第0位到第49位
int tmp=0;
for(int j=0;j<100;j++){
char[] c ={num[j].charAt(i),};
String bit=new String(c);
tmp+=new Integer(bit);
}
if(i>=2) tmp+=carry1[i-1]+carry2[i-2];//加前2位的进位
else if(i>=1) tmp+=carry2[i];//第一位,只加第0位的进位
carry1[i]=tmp/100;
carry2[i]=(tmp%100)/10;
result[i]=tmp%10;
}
System.out.print(carry1[49]);
System.out.print(carry2[49]);
for(int i=0;i<8;i++){
System.out.print(result[49-i]);
}

} catch (FileNotFoundException e) {

e.printStackTrace();
}
}
}


输出:
4336259487
加载更多回复(33)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧