1,思路

大数相加,若直接使用int,或者long都会超出长度,因此考虑使用String存储。

2,代码
public class LargeNumAdd
{ public static void main(String[] args)
{
String sum = "0";
for (int i = 1; i <= 2009; i++)
{
String temp = getStr(i);
sum = getSum(sum, temp);
}
System.out.println("sum: " + sum);
} public static String getSum(String a, String b)
{
if (a.length() > b.length())
{
for (int i = b.length(); i < a.length(); i++)
{
b = '0' + b;
}
} else
{
for (int i = a.length(); i < b.length(); i++)
{
a = '0' + a;
}
}
char array1[] = a.toCharArray();
char array2[] = b.toCharArray();
String sum = "";
int carrayIn = 0;
for (int i = a.length() - 1; i >= 0; i--)
{
sum = getRemain(array1[i], array2[i], carrayIn) + sum;
carrayIn = getCarry(array1[i], array2[i], carrayIn);
}
return sum;
} public static String getRemain(char a, char b, int carrayIn)
{
int sum = (a - '0') + (b - '0') + carrayIn;
String rem = sum % 10 + "";
return rem;
} private static int getCarry(char a, char b, int carryIn)
{
int sum = (a - '0') + (b - '0') + carryIn;
int carry = sum / 10;
return carry;
} public static String getStr(int N)
{
String s = "";
for (int i = 0; i < N; i++)
{
s += "1";
}
return s;
}
}

求sum=1+111+1111+........+1....111 .的更多相关文章

  1. oracle对三个列求sum

    oracle数据库对test_table表的三个列count1,count2,count3求sum的两种sql,做个记录 第一种 select sum (case when count1 is not ...

  2. SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用

    进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: ' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单 ...

  3. Python 处理EXCEL的CSV文档分列求SUM

    相对于导出EXCEL文件,PYTHON计算更为实时. import csv import sys from optparse import OptionParser def calculate_pro ...

  4. java篇 之 类型转化

    类型转换时,如果最初的数值类型是有符号的,那么就执行符号扩展:如果它是char,那么不管将要被转换成什么类型,都执行零扩展 代码执行顺序是从上至下,从右至左 强制转换: Int a =(int)(sh ...

  5. noip初赛复习总纲

    初赛复习总纲 目录 初赛复习总纲 计算机发展史 计算机的分类 计算机的应用 操作系统盘点 计算机的基本结构 中央处理器(**CPU**--**Central Processing Unit**) 存储 ...

  6. ethereum(以太坊)(六)--整型(int)

    pragma solidity ^0.4.20; /* uint8 uint16 ...uint256 int8 int16 int24 ..int256 uint => uint256 int ...

  7. 求a + aa + aaa + aaaa + aaaaa ...的值,例如:1 + 11 + 111,2 + 22 + 222 + 2222 + 22222

    #include <stdio.h> unsigned superposition(unsigned m, unsigned n); int main() { printf("1 ...

  8. [华为机试练习题]50.求M的N次方的最后三位

    题目 描写叙述: 正整数M 的N次方有可能是一个很大的数字,我们仅仅求该数字的最后三位 例1: 比方输入5和3 ,5的3次方为125.则输出为125 例2: 比方输入2和10 2的10次方为1024 ...

  9. sum()over()和count()over()分析函数

    创建测试表 ),sales ),dest ),dept ),revenue number); 插入测试数据 ); ); ); ); ); ); ); commit; 查看表记录 SQL> sel ...

随机推荐

  1. ubuntu 新手入门第一天

    在虚拟机上安装好linux之后,当前版本 ubuntu-16.04.3-desktop-amd64.iso 1. 设置root用户名和密码 sudo passwd 然后输入两次密码 2. 设置软件源 ...

  2. POJ 2187 Beauty Contest [凸包 旋转卡壳]

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36113   Accepted: 11204 ...

  3. AGC017 F - Zigzag

    传送门 Time limit : 4sec / Memory limit : 256MB Score : 1600 points Problem Statement There are N(N+1)⁄ ...

  4. 微信小程序页面跳转的问题(app.json中设置tarBar后wx.redirectTo和wx.navigateTo均不能实现跳转到指定的页面)

    1.设置的tabBar代码片段: "tabBar": { "list": [ { "pagePath": "pages/homep ...

  5. python脚本0b文件处理

    要处理的文件: 此处处理将00的数据干掉. 处理python脚本: dir_fd = open('abc.yuv','rb+') tmp_fd = open('tmp.yuv','wb+') whil ...

  6. Redis 学习(三) —— 事务、消息发布订阅

    一.Redis事务 Redis 提供的事务机制与传统的数据库事务有些不同,传统数据库事务必须维护以下特性:原子性(Atomicity), 一致性(Consistency),隔离性(Isolation) ...

  7. [SCOI2007]最大土地面积

    首先,最大四边形的四个点一定在凸包上 所以先求凸包 有个结论,若是随机数据,凸包包括的点大约是\(\log_2n\)个 然鹅,此题绝对不会这么轻松,若\(O(n^4)\)枚举,只有50分 所以还是要想 ...

  8. 关于webconsole报../website/console.go:35: undefined: ssh.InsecureIgnoreHostkey 错误解决方案

    1.首先,进入webconsole目录删除/opt/webconsole/src/golang.org/x/目录下 crypto文件夹 2.然后,在/opt/webconsole/src/golang ...

  9. 02-Nginx+MySQL+PHP7

    [安装Nginx] #先安装如下包 yum install gcc gcc-c++ kernel-devel yum -y install pcre-devel openssl openssl-dev ...

  10. kvm克隆

    virt-clone --original aming2 --name aming3 --file /data/kvm/aming3.qcow2   相关的克隆命令 克隆前必须关闭虚拟机   virs ...