高精求助,只对了3个

P1001 A+B Problem

gordon321 @ 2023-08-12 16:25:52

#include<bits/stdc++.h>
using namespace std;
int main(){
    string a;
    string b;
    cin>>a;
    cin>>b;
    int c[1000]={0};
    int jy=0;
    int sw=0;
    int x=a.length()-1;
    int y=b.length()-1;
    for(int i=0;i<max(a.length(),b.length())+1;i++){
        if(i==max(a.length(),b.length())){
            if(jy==0){
                continue;
            }else{
                c[i]=jy;
                continue;
            }
        }
        if(a.length()>b.length()&&i>=b.length()){
            c[i]=(a[x]-48+jy)%10;
            if(a[x]-48+jy>=10){
                jy=(a[x]-48+jy)/10;
            }else{
                jy=0;
            }
        }else if(a.length()<b.length()&&i>=a.length()){
            c[i]=(b[y]-48+jy)%10;
            if(b[y]-48+jy>=10){
                jy=(b[y]-48+jy)/10;
            }else{
                jy=0;
            }
        }else{
            c[i]=(a[x]-48+b[x]-48+jy)%10;
            if(a[x]-48+b[x]-48+jy>=10){
                jy=(a[x]-48+b[y]-48+jy)/10;
            }else{
                jy=0;
            }
        }
        x--;
        y--;
        sw++;
    }
    for(int i=sw;i>=0;i--){
        if(i==sw&&c[i]==0){
        }else{  
            cout<<c[i];
        }
    }
    return 0;
}

by sapo1o @ 2023-08-12 16:55:08

@gongziwen 看不懂其他人代码,就只能发自己代码


by gongziwen @ 2023-08-12 16:59:09

@red_hair_steve 可以不发,因为你又不带解释,lz还不如搜题解看


by __youzimo2014__ @ 2023-08-14 10:18:03

高精度有专门的题目P1601,而且这个是有负数的,P1601代码参考如下:

#include <iostream>
#include <cstring>
using namespace std;

int MTStrlen(char *str) {
    int length = 0;
    for (int i = 0; 1 ; i++) {
        if (str[i] == 0) {
            break;
        }
        length++;
    }
    return length;
}
void reverse_string (char* arr);
int charToInt(char ch);
int intToChar(int i);
char *add(char *a, char *b);
char *deepCopy(char *str);
int main(int argc, const char * argv[]) {
    // insert code here...

    char *a = (char *)calloc(500, sizeof(char)), *b = (char *)calloc(500, sizeof(char));
    scanf("%s", a);
    scanf("%s", b);
//    reverse_string(a);
//    reverse_string(b);
//    char *awserd = deepCopy(strlen(a) > strlen(b) ? a : b);
//    int carry = 0;
//    size_t shorts = strlen(a) > strlen(b) ? strlen(b) : strlen(a);
//    for (int i = 0; i < shorts; i++) {
//        int iPosition = charToInt(a[i]) + charToInt(b[i]) + carry;
//        awserd[i] = intToChar(iPosition % 10);
//        carry = iPosition / 10;
//    }
//    for (int i = shorts; i < strlen(awserd); i++) {
//        int iPosition = charToInt(awserd[i]) + carry;
//        awserd[i] = intToChar(iPosition % 10);
//        carry = iPosition / 10;
//    }
//    if (carry > 0) {
//        awserd[strlen(awserd)] = intToChar(carry);
//    }
//    for (int i = MTStrlen(awserd) - 1; i>=0; i--) {
//        printf("%c", awserd[i]);
//    }
    cout << add(a, b);
    return 0;
}

void reverse_string (char* arr) {
    int len = MTStrlen(arr);
    for (int i = 0; i < len/2; i++) {
        char tmp = arr[len - i- 1];
        arr[len - i- 1] = arr[i];
        arr[i] = tmp;
    }
}
/**
 * 深拷贝字符串
 */
char *deepCopy(char *str) {
    char *newString = (char *)calloc(strlen(str) + 1, sizeof(char));
    for (int i = 0; i <= strlen(str); i++) {
        newString[i] = str[i];
    }
    return newString;
}
int charToInt(char ch) {
    return (int)(ch - '0');
}
int intToChar(int i) {
    return (char)(i + '0');
}
char *add(char *a, char *b) {
    reverse_string(a);
    reverse_string(b);
    char *awserd = deepCopy(MTStrlen(a) > MTStrlen(b) ? a : b);
    int carry = 0;
    int shorts = MTStrlen(a) > MTStrlen(b) ? MTStrlen(b) : MTStrlen(a);
    for (int i = 0; i < shorts; i++) {
        int iPosition = charToInt(a[i]) + charToInt(b[i]) + carry;
        awserd[i] = intToChar(iPosition % 10);
        carry = iPosition / 10;
    }
    for (int i = shorts; i < MTStrlen(awserd); i++) {
        int iPosition = charToInt(awserd[i]) + carry;
        awserd[i] = intToChar(iPosition % 10);
        carry = iPosition / 10;
    }
    if (carry > 0) {
        awserd[MTStrlen(awserd)] = intToChar(carry);
    }
    char *acAwserd = (char *)calloc(strlen(awserd) + 1, sizeof(char));
    char *tmpString = (char *)calloc(2, sizeof(char));
    for (int i = MTStrlen(awserd) - 1; i>=0; i--) {
        tmpString[0] = awserd[i];
        strcat(acAwserd, tmpString);

    }

    return acAwserd;
}

by duanbc2022 @ 2023-08-16 16:29:15

哇,小可ai Товарищ!!!


by ge_yiyang_001_DT @ 2023-08-16 16:52:11

@caramel_qwq 你是蒟蒻吗?


by caramel_qwq @ 2023-08-17 08:26:04

@geyiyang001 啊是的


by ge_yiyang_001_DT @ 2023-08-17 15:47:54

@caramel_qwq 6


by dakelaiyi @ 2023-08-18 14:03:00

#include <iostream>
using namespace std;
string s1,s2;
int a[505],b[505],c[505];
int main()
{
    cin>>s1>>s2;
    int len1=s1.size();
    int len2=s2.size();
    for(int i=len1-1; i>=0; i--)
    {
        a[len1-1-i]=s1[i]-48;
    }
    for(int i=len2-1; i>=0; i--)
    {
        b[len2-1-i]=s2[i]-48;
    }
    int len=max(len1,len2);
    int i,x=0;
    for(i=0; i<len; i++)
    {
        c[i]=a[i]+b[i]+x;
        x=c[i]/10;
        c[i]=c[i]%10;
    }
    c[i]=x;
    while(c[i]==0&&i>0)
    {
        i--;
    }
    for(i; i>=0; i--)
    {
        cout<<c[i];
    }
    return 0;
}

by Tanleyan @ 2023-08-19 16:33:08

@red_hair_steve 怎么发代码啊,急


by Tanleyan @ 2023-08-19 16:34:50

我有超简代码


上一页 | 下一页