题目链接:http://ac.jobdu.com/problem.php?pid=1003

 

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

 

参考代码:

//
//  1003 A+B.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 2017/4/14.
//  Copyright © 2017年 PengFei_Zheng. All rights reserved.
//
 
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
 
using namespace std;
 
char a[21],b[21];
int num1,num2;
 
int main(){
    while(scanf("%s%s",a,b)!=EOF){
        int lena = (int)strlen(a);
        int lenb = (int)strlen(b);
        num1 = num2 = 0;
        int size1 = 0;
        int size2 = 0;
        bool flag1 = (a[0]>='0'&&a[0]<='9') ? true : false;
        bool flag2 = (b[0]>='0'&&b[0]<='9')? true : false;
        for(int i = lena -1 ; i >= 0 ; i--){
            if(a[i]>='0' && a[i]<='9'){
                num1+=(a[i]-'0')*pow(10,size1);
                size1++;
            }
        }
        for(int i = lenb -1 ; i >= 0 ; i--){
            if(b[i]>='0' && b[i]<='9'){
                num2+=(b[i]-'0')*pow(10,size2);
                size2++;
            }
        }
        if(flag1&&flag2) printf("%d\n",num1+num2);
        else if(flag1 && !flag2) printf("%d\n",num1-num2);
        else if(flag2 && !flag1) printf("%d\n",num2-num1);
        else if(!flag1 && !flag2) printf("%d\n",0-num1-num2);
    }
}
/**************************************************************
    Problem: 1003
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1608 kb
****************************************************************/

 

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-11-06
  • 2021-10-27
  • 2022-02-03
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2021-08-15
  • 2021-07-19
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2021-06-25
  • 2021-11-10
相关资源
相似解决方案