模板,大数相加

char a[Max],b[Max],c[Max],sum[Max];

void  jia(char str1[],char str2[])
{
    int i,j,k,z;
    k=0;z=0;
    for(i=strlen(str1)-1,j=strlen(str2)-1;i>=0||j>=0;i--,j--)   //核心,加法以及进位
    {
        if(i>=0)
            z+=str1[i]-'0';
        if(j>=0)
            z+=str2[j]-'0';
       c[k++]=z%10+'0';
       z=z/10;
    }
    if(z) c[k++]='1';
    c[k]='\0';
    i=0;
    for(--k;k>=0;k--)
        sum[i++]=c[k];        //转换成一个能完整方便输出的字符串数组sum
    sum[i]='\0';
}

模板,大数相加,布布扣,bubuko.com

时间: 2024-10-13 09:39:10

模板,大数相加的相关文章

【模板小程序】大数相加(十进制),包含合法性检查

1 //大数相加(十进制),用string处理 2 #include <iostream> 3 #include <string> 4 #include <algorithm> 5 6 using namespace std; 7 8 //输入数据合法性检查,数字必须在0-9范围内 9 bool IsVaild(const string& num1,const string& num2) 10 { 11 for(auto val:num1) 12 { 1

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

HDU 1250 Hat&#39;s Fibonacci(大数相加)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12952    Accepted Submission(s): 4331 Problem Description A Fibonacci sequence

HDU 1297 Children’s Queue (递推、大数相加)

Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17918    Accepted Submission(s): 5976 Problem Description There are many students in PHT School. One day, the headmaster whose na

java-两个大数相加

题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a="8888899999999888";  String b="88888888888888";  String str=new BigInteger(a).add(new BigInteger(b)).toString();  System.out.println(str);

高精度问题之大数相加(原来就是用字符串相加,模拟手算这么简单!)

解题心的: 就是基本的一对一模拟手算..借助c++的string 不用逆序运算了.很方便的补0.  最后处理下前导0的问题. #include <iostream> #include <string> using namespace std; // 实现大数相加 结果存放在num中 void bigIntergerAdd(string &num, string add) { int goBit = 0; // 存放进位 // 先交换下顺序 加数的位数要比较少 if (num

HDU 1047 Integer Inquiry 大数相加 string解法

本题就是大数相加,题目都不用看了. 不过注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h

两个大数相加 ----Javascrit 实现

(function(){ var addLarge = function(n1,n2){ var over = 0; var ret = ""; var len = Math.min(n1.length,n2.length); var sln1 = n1.substr(n1.length - len,n1.length ); var sln2 = n2.substr(n2.length - len,n2.length ); for(var i = len;i > 0; i--)

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -