HDU 3787

题目链接点击打开链接

Submit Status Practice HDU 3787

此题开始wa了多次,这个代码样例过但wa

//@auther Yang Zongjun
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

string s1, s2;

int pow1(int a, int b)
{
    int ans = a;
    for(int i = 1; i < b; i++)
    {
        ans *= a;
    }
    return b == 0 ? 1 : ans;
}

long long  solve(string s)
{
    long long  ans = 0;
    int len = s.length();
    int i, cnt = 0;
    //cout << len << endl;
    //cout << s[11] << "oj" << endl;
    for(i = len-1; i > 0; i--)
    {
        if(s[i] == ',') continue;
        else
        {
            //cout << s[i] <<  (s[i] - '0')+2 << endl;
            //cout << (s[i] - '0') * pow1(10, cnt) << endl;
            ans += ((s[i] - '0') * pow1(10, cnt));
            //cout << ans << endl;
            cnt++;
        }
    }
    if(s[0] == '-') ans *= -1;
    else
    {
        ans += ((s[0] - '0') *pow1(10, cnt));
    }
    return ans;   
}

int main()
{
//    freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    while(cin >> s1 >> s2)
    {
        //cout << pow1(10, 0) << pow1(10, 1) << pow1(10, 2) << endl;
        //cout << s1.length()  << " " << s2.length() << endl;
        //cout << s1[11] << endl;
        //cout << s1 << "\n" << s2 << endl;
        //cout << solve(s1) << "\n" <<  solve(s2)<< endl;
        //int lens1 = s1.length(), lens2 = s2.length();
        printf("%I64d\n", solve(s1) + solve(s2));
        s1.clear();
        s2.clear();
    }
    return 0;
}
搜题解ac

//@auther Yang Zongjun
#include <iostream>
#include <cstdio>
#include <cmath>
//#include <string.h>
#include <string>
#include <cstring>

using namespace std;

string s1, s2;


int main()
{
//    freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    while(cin >> s1 >> s2)
    {
        int lens1= s1.length();
        int lens2 =s2.length();
        int a = 0, b  = 0;
        for(int i = 0; i < lens1; i++)
        {
            if('0'<=s1[i]&&s1[i]<='9')
            {
                a = a * 10 + (s1[i] - '0');
            }
        }
        if(s1[0] == '-') a = -a;
        for(int i = 0; i < lens2; i++)
        {
            if('0'<=s2[i]&&s2[i]<='9')
            {
                b = b * 10 + (s2[i] - '0');
            }
        }
        if(s2[0] == '-') b = -b;
        printf("%d\n", a + b);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值