大数(+)

PS:发现C++里有个很好玩的 函数 string(),头文件必须是<string>,不能是<string.h> ,下面因为memset(),所以我又定义了<string.h>,string()可以直接定义 字符串,但是需要C++强大的输入流,scanf无法使用, 字符串长度 a.lenth();  C++里面函数很棒,想从C 转型到C++了,以后打代码,尽量使用C++

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
const int c = 2001;
using namespace std;

int r[c];
int main()
{
    string a,b;
    int st=0,len1,len2,i=0,num=0,t;
   while(cin>>a>>b)
   {
    memset(r,0,sizeof(r));
    len1=a.length();
    len2=b.length();
    st=0,i=0,num=0;

    while(1)
    {
        t=a[--len1]-'0'+b[--len2]-'0' + st;
        if(len1==-1 || len2==-1)
            break;
        (t > 9)?(st = 1):(st = 0);
        r[num++]=t%10;
    }
    if(len2 !=-1)
    {
        for(i=len2;i>=0;--i)
        {
            t=b[i]-'0'+st;
            (t > 9)?(st = 1):(st = 0);
            r[num]=t%10;
            num++;
        }
    }
    else if(len1!=-1)
    {
        for(i=len1;i>=0;--i)
        {
            t=a[i]-'0'+st;
            (t > 9)?(st = 1):(st = 0);
            r[num]=t%10;
            num++;
        }
    }
    if(st!=0)
        cout<<st;
    for(i=num-1;i>=0;i--)
        cout<<r[i];
        puts("");
   }
    return 0;
}

大数(+)

时间: 2024-08-02 15:42:33

大数(+)的相关文章

最短的计算大数乘法的c程序

#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[j++]*t[k---1]; if(i)r(i-1,c/10); printf("%d",c%10); } void main() { gets(s);gets(t); while(s[n])s[n++]-=48; while(t[m])t[m++]-=48; r(m+n-1,0); }

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

各类大数模板

ps:转自http://blog.csdn.net/y990041769/article/details/20116995 大数加法模板(本人验证过,其他还没用,只是写在这) 1 string sum(string s1,string s2) 2 { 3 if(s1.length()<s2.length()) 4 { 5 string temp=s1; 6 s1=s2; 7 s2=temp; 8 } 9 int i,j; 10 for(i=s1.length()-1,j=s2.length()-

【51Nod】1005 大数加法

给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 68932147586 468711654886 Output示例 537643802472 ==================================================================================================== 问题解法

HDU 1018 大数(求N!的位数/相加)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35382    Accepted Submission(s): 16888 Problem Description In many applications very large integers numbers are required. Some of these

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);

大数乘法

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 相关问题 大数加法 0 大数开平方 640 大数进制转换 320 大数除法 320 大数乘法 V2 80 代码: 1 #inclu

菲波那契数的余数------大数

Description 菲波那契数大家可能都已经很熟悉了: f(1)=0 f(2)=1 f(n)=f(n-1)+f(n-2) n>2 因此,当需要其除以某个数的余数时,不妨加一些处理就可以得到. Input 输入数据为一些整数对P.K,P(1 < P < 5000)表示菲波那契数的序号,K( 1 <= K < 15)表示2的幂次方.遇到两个空格隔开的0时表示结束处理. Output 输出其第P个菲波那契数除以2的K次方的余数. Sample Input 6 2 20 10 0