【FOJ 1171】 Hard to Believe, but True!

思路:
题意比较简单,就是哥你一个等式,判断各个数字颠倒后,等式是否成立。
具体思路看代码,相信都能看懂,比较简单,还有注释。

AC code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string> 
#include<cstring>
#include<set>
#include<cmath>
using namespace std;
const int maxn = 1000 + 10;
string s,s1,s2,s3;
int swift(string str){
    int sum = 0;
    int len = str.size();
    int i = len - 1;
    while(str[i] == '0'){//去掉前面的0 
        --i;
    }
    while(i >= 0){
        sum += ( str[i] - '0') * pow(10,i);
        --i;            //忘记改变条件了,导致陷入死循环 
    }
    return sum;
}
int main(){
    while(cin >> s){
        s1 = s2 = s3 = "";
        int pos1 = 0, pos2 = 0;
        for(int i = 0;i < s.size();++i){//找出+,=号的位置 
            if(s[i] == '+'){
                pos1 = i;
            }
            if(s[i] == '='){
                pos2 = i;
            }
        }
        //cout <<"em " << pos1 << " "<< pos2 << endl; 
        for(int i = 0;i < pos1;++i){//获取3个数字 
            s1 += s[i];
        } 
        //cout << s1 << endl;
        for(int i = pos1+1;i < pos2;++i){
            s2 += s[i];
        }
        //cout << s2 << endl;
        for(int i = pos2+1;i < s.size();++i){
            s3 += s[i];
        }
        //cout << s3 << endl;
        if(swift(s1) + swift(s2) == swift(s3)){
            cout << "True" << endl;
        }else{
            cout << "False" << endl;
        }

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值