PAT A1024题解——高精度大数相加模板

#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef long long ll;
using namespace std;
struct bign{
    int d[1000];
    int len;
    bign(){
        memset(d,0,sizeof(d));
        len = 0;
    }
};

bign change(char str[]){     //将整数转换为bign
    bign a;
    a.len = strlen(str);
    for(int i=0;i<a.len;i++){
        a.d[i] = str[a.len-1-i] - ‘0‘;
    }
    return a;
}

bool judge(bign a){         //判断是否是回文
    for(int i=0; i <= a.len/2; i++){
        if(a.d[i] != a.d[a.len-1-i]){
            return false;
        }
    }
    return true;
}

bign add(bign a,bign b){               //高精度a+b
    bign c;
    int carry = 0;
    for(int i=0; i < a.len || i < b.len; i++){
        int temp = a.d[i] + b.d[i] + carry;
        c.d[c.len++] = temp % 10;
        carry = temp / 10;
    }
    if(carry != 0){
        c.d[c.len++] = carry;
    }
    return c;
}

void print(bign a){            //输出bign
    for(int i = a.len-1; i >= 0; i--){
        printf("%d",a.d[i]);
    }
    printf("\n");
}

int main(){
    char str[1000];
    int k,num=0;
    while(scanf("%s %d",str,&k)!=EOF){
        bign a = change(str);
        while(num<k && judge(a)==false){
            bign b = a;
            reverse(b.d,b.d+b.len);  //将字符串倒置
            a = add(a,b);
            num++;
        }
        print(a);
        printf("%d\n",num);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/mxj961116/p/10347064.html

时间: 2024-10-06 14:54:38

PAT A1024题解——高精度大数相加模板的相关文章

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

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

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

模板,大数相加

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

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

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

【大数类模板】hdoj 4927 Series 1

题目很简单:分析发现满足杨辉三角,有通项公式,但是是高精度,大数题目. 记录一个大数类模板:以后好用 代码: #include<cstdio> #include<cstring> using namespace std; #define MAXN 9999 #define MAXSIZE 10 #define DLEN 4 class BigInt { private: int a[500]; int len; public: BigInt() {len = 1; memset(a

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

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

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