三角形

题目描述
给定三条边,请你判断一下能不能组成一个三角形。


输入描述:
输入包含多组数据,每组数据包含三个正整数a、b、c(1≤a, b, c≤10^100)。




输出描述:
对应每一组数据,如果它们能组成一个三角形,则输出“Yes”;否则,输出“No”。


输入例子:
1 2 3
2 2 2


输出例子:
No

Yes

算法:

#include<string>
#include<iostream>
using namespace std;
//比较字符串的大小 
bool compare(string s1,string s2){
    if(s1.size() > s2.size()){
        return true;
    }else if(s1.size() < s2.size()){
        return false;
    }else{
        for(int i = 0;i < s1.size();++i){
            if(s1[i] > s2[i]){
                return true;
            }else if(s1[i] < s2[i]){
                return false;
            }
        }
    }
    return true;
}
//由大至小排列三个边长
void sort(string &a,string &b,string &c){ 
    if(!compare(a,b)){
        string temp = a;
        a = b;
        b = temp;
    }
    if(!compare(a,c)){
        string temp = a;
        a = c;
        c = temp;
    }
    if(!compare(b,c)){
        string temp = b;
        b = c;
        c = temp;
    }
}
int main(){
    string a,b,c;
    while(cin >> a >> b >> c){
        sort(a, b, c); 
        int add = 0, i = c.size() - 1, j = b.size() - 1;
        //求b与c的和放入b中 
        for(;i >= 0 && j >= 0;i--, j--){
            int temp = (c[i] - '0') + (b[j] - '0') + add;
            if(temp >= 10){
                temp %= 10;
                add = 1;
            }else{
                add = 0;
            }
            b[j] = temp + '0';
        }
		bool t= false;
        //进位处理 
        if(j == -1 && add == 1){
            b = "1" + b;
        }else if(j != -1 && add == 1){
			bool t= true;
            for(i = b.size() - c.size() - 1;i >= 0;i--){
                int temp = (b[i] - '0') + add;
                if(temp >= 10){
                    temp %= 10;
                    add = 1;
                    b[i] = temp + '0';
                }else{
                    b[i] = temp + '0';
                    break;
                }
            }  
        }
        if(t && i == -1 && add == 1){
            b = "1" + b;
        }
        if(compare(a,b)){
            cout << "No" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值