首页 > 试题广场 >

三角形

[编程题]三角形
  • 热度指数:6881 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定三条边,请你判断一下能不能组成一个三角形。

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


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

输入

1 2 3
2 2 2

输出

No
Yes
// 最短两边大于最长边即可。核心代码总量:7行 AC!
importjava.math.BigInteger;
importjava.util.Arrays;
importjava.util.Scanner;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner in = newScanner(System.in);
        while(in.hasNext()){
            BigInteger[] arr=newBigInteger[3];
            for(inti = 0; i < 3; i++) {
                arr[i]=newBigInteger(in.next());
            }
            Arrays.sort(arr);
            if(arr[0].add(arr[1]).compareTo(arr[2])>0){
                System.out.println("Yes");
            }
            else{
                System.out.println("No");
            }
        }
    }
}

发表于 2017-07-27 08:25:27 回复(0)
import java.util.*;
import java.io.*;
import java.math.*;

 public class  Main{
     public static void main(String args[]){
         Scanner in=new Scanner(System.in);
         boolean k;
         BigInteger a,b,c;
         while(in.hasNext()){
             a=in.nextBigInteger();
             b=in.nextBigInteger();
             c=in.nextBigInteger();
             k=true;
             if((a.add(b)).compareTo(c)<=0) k=false;
             if((b.add(c)).compareTo(a)<=0) k=false;
             if((a.add(c)).compareTo(b)<=0) k=false;
             if(k) System.out.println("Yes");
             else  System.out.println("No");
         }
     }
     
     
     
     
 }

发表于 2015-09-14 20:05:40 回复(3)
 判断三个数是否构成三角形,条件是:设三数为a.b.c必须满足|a-b|<c<|a+b|即两数之和大于第三数,它们的差(大减小)也小于第三数。
int main()
{		
	printf("请输入三条边,用空格隔开\n");
	while (1)
	{
		int a[3] = {0};
		scanf("%d %d %d",&a[0],&a[1],&a[2]);
		for (int i = 0;i < 3; i++)
		{
			if ((a[i] < a[(i+1)%3] + a[(i+2)%3]) && (a[i] > a[(i+1)%3] - a[(i+2)%3]) && (a[i] > a[(i+2)%3] - a[(i+1)%3]))
			{
			}
			else{
				printf("NO\n");
				break;
			}
			printf("YES\n");
			break;
		}
	}
	return 0;
}

发表于 2015-08-16 09:18:06 回复(2)

Python two line solution:

while True:
    try:
        a, b ,c=sorted(map(int,input().split()))
        print("Yes" if a+b>c else "No")
    except:
        break
发表于 2017-10-11 10:03:17 回复(1)
注意范围,需要用double类型
#include<iostream>
using namespace std;

int main()
{
    double a, b, c;
    while(cin>>a>>b)
    {
        if(a > b)
            swap(a,b);
        cin>>c;
        if(b > c)
            swap(b,c);
        if(a + b > c)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
发表于 2019-12-07 12:03:18 回复(1)
用字符串表示整数,定义加法和比较大小的函数
#include <iostream>
#include <string>
using namespace std;

string add(string s1, string s2) {
    int len1 = s1.size();
    int len2 = s2.size();
    string res = "";
    int i, j, k , t = 0;
    for (i=len1-1, j=len2-1; i>=0 && j>=0; i--, j--) {
        k = (s1[i]-'0') + (s2[j] - '0') + t;
        res = (char)(k % 10 + '0') + res;
        t = k / 10;
    }
    while (i>=0) {
        k = (s1[i]-'0') + t;
        res = (char)(k % 10 + '0') + res;
        t = k / 10;
        i--;
    }
    while (j>=0) {
        k = (s2[j]-'0') + t;
        res = (char)(k % 10 + '0') + res;
        t = k / 10;
        j--;
    }
    if (t>0) {
        res = (char) (t + '0') + res;
    }
    
    return res;
}

bool great(string s1, string s2) {
    int len1 = s1.size();
    int len2 = s2.size();
   	if (len1 > len2)
        return true;
    else if (len1 < len2)
        return false;
    else {
        return s1 > s2;
    }
    
}

int main() {
    string a, b, c;
    while (cin>>a>>b>>c) {
        if (great(add(a,b), c) && great(add(a,c),b) && great(add(b,c),a))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
}

发表于 2015-11-26 10:15:00 回复(1)
       因为正整数a、b、c(1≤a, b, c≤10^100),所以不能用int、long等数据结构保存a、b、c的值,可以用整型数组,或者字符串保存。
      判断三条边能不能组成三角形,只需要判断最大的边长度是否小于另外两条边的长度之和。假设用max mid min 分别表示最长的边,中间长度的边,最短的边。组成三角形的条件是任意两边之和大于第三边,任意两边之差小于第三边。任意max>mid>min,所以max加任意一边长度都会大于第三边,假设我们保证max<mid+min,那么min>max-mid,mid>max-min,max>mid-min.满足条件。
     假设我们输入时用字符串存储a、b、c。首先应该判断输入的a、b、c中的最大值,然后计算另外两条边的长度之和,这个地方要用到大整数加法。
   1、首先将字符串转变为数字数组:
void getNumber(char str[],int *num)//str为输入的字符串,num为转换后的数组
{
    int len=strlen(str);
    for(int i=0;i<len;i++)
        num[i]=str[len-1-i]-'0';
}
 2、比较两个大整数的大小:
int compare(int* a,int* b)
{
    for(int i=100;i>=0;i--){
        if(a[i]>b[i])//a>b,返回1
            return 1;
        else if(a[i]<b[i])//a<b,返回-1
            return -1;
    }
    return 0;//a=b,返回0
}
3、从输入的三条边中找到最大值
void compare_3(int* a,int* b,int* c,int &max)
{
    int ab=compare(a,b);
    int bc=compare(b,c);
    int ac=compare(a,c);
    if(ab>=0&&ac>=0)
            max=1;//表示输入的第一条的长度最大
    if(bc>=0&&ab<=0)
        max=2;//表示输入的第二条的长度最大
    if(ac<=0&&bc<=0)
        max=3;//表示输入的第三条的长度最大
}
4、将较短的两条边相加
void add(int *x,int *y,int *ans)
{
    int ext=0;//进位
    for(int i=0;i<101;i++){
        ans[i]=x[i]+y[i]+ext;
        if(ans[i]>9)
            {ans[i]-=10;ext=1;//ans存储相加后的结果
        }
        else ext=0;
    }
}
  5、判断能否构成三角形
bool trangle(int* a,int* b,int* c)
{
    int max;
    int temp[101];
    compare_3(a,b,c,max);
    switch(max){
        case 1:
            add(b,c,temp);
            if(compare(a,temp)<0)
                return true;
            else
                return false;
            break;
        case 2:
            add(a,c,temp);
            if(compare(b,temp)<0)
                return true;
            else
                return false;
            break;
        case 3:
            add(a,b,temp);
            if(compare(c,temp)<0)
                return true;
            else
                return false;
            break;
    }
   
}
6、主程序:
int main()
{
    vector<string> out;//保存要输出的结果
    char x[101],y[101],z[101];
    int a[101]={0},b[101]={0},c[101]={0};
        while(cin>>x>>y>>z){
            getNumber(x,a);getNumber(y,b);getNumber(z,c);
        if(trangle(a,b,c))
           out.push_back("yes");
        else
            out.push_back("no");
          memset(a,0,sizeof(a));
          memset(b,0,sizeof(b));
          memset(c,0,sizeof(c));
        }
     
    for(int i=0;i<out.size();i++)
        cout<<out[i]<<endl;
  //  system("PAUSE");
return 0;
}
编辑于 2015-08-17 18:37:52 回复(2)
//double的范围是10^-308到10^308

#include<iostream>
using namespace std;

int main()
{
    double a,b,c;
    while(cin>>a>>b>>c)
    {
        if((a+b>c)&&(a+c>b)&&(b+c>a))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

发表于 2019-07-08 11:22:03 回复(4)
简洁的大数相加,大数比较解法
#include <iostream>
#include <string>

using namespace std;

void sum(string& a, string& b, string& ab) {
    int i = a.size() - 1, j = b.size() - 1, k = ab.size() - 1;
    int carry = 0;
    while (j >= 0 || carry) {
        int n1 = i >= 0 ? a[i--] - '0' : 0;
        int n2 = j >= 0 ? b[j--] - '0' : 0;
        ab[k--] = (n1 + n2 + carry) % 10 + '0';
        carry = (n1 + n2 + carry) / 10;
    }
}


bool cmp(string& a, string& b, string& c) {
    if (b.size() < c.size() - 1) return false;   // 从数量级上先判断

    string ab(b.size() + 1, 0);         // a + b 后可能会进位,预留一位
    sum(a, b, ab);

    int i = 0, j = 0;
    if (ab[0] == 0) i++;                // 说明预留的一位没有用上
    if (ab.size() - i < c.size() - j) return false;
    else if (ab.size() - i > c.size() - j) return true;

    while (i < ab.size()) {
        if (ab[i] < c[j]) return false;
        else if (ab[i] > c[j]) return true;
        i++;
        j++;
    }
    return false;     // a + b = c

}

int main() {
    string a, b, c;
    while (cin >> a >> b >> c) {
        if (a.size() > b.size() || (a.size() == b.size() && a > b)) a.swap(b);    // 确保 a < b < c
        if (a.size() > c.size() || (a.size() == c.size() && a > c)) a.swap(c);
        if (b.size() > c.size() || (b.size() == c.size() && b > c)) c.swap(b);
        if (cmp(a, b, c)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}


发表于 2023-08-19 15:38:25 回复(0)
//long double:10^-4931----10^4932
#include <iostream>
using namespace std;

int main()
{
    long double a, b, c;
    while (cin >> a >> b >> c)
    {
        if ((a + b > c) && (a + c > b) && (c + b > a))cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

发表于 2019-05-09 09:26:36 回复(2)
import java.math.BigInteger;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            BigInteger a=sc.nextBigInteger();
            BigInteger b=sc.nextBigInteger();
            BigInteger c=sc.nextBigInteger();
            if(a.add(b).compareTo(c)>0 && a.add(c).compareTo(b)>0 && b.add(c).compareTo(a)>0)
                System.out.println("Yes");
            else
                System.out.println("No");
        }
    }
}

发表于 2018-10-01 12:54:02 回复(0)
服了,搞半天,原来是我long存的数据太小,不得不用BigIntger了。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
//            long a = sc.nextLong();
//            long b = sc.nextLong();
//            long c = sc.nextLong();
            BigInteger a = new BigInteger(sc.next());
            BigInteger b = new BigInteger(sc.next());
            BigInteger c = new BigInteger(sc.next());
            if (isTriangle(a, b, c)) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }

    public static boolean isTriangle(BigInteger a, BigInteger b, BigInteger c) {
//        return a + b > c && a + c > b && b + c > a;
        return a.add(b).compareTo(c) > 0 && a.add(c).compareTo(b) > 0 && b.add(c).compareTo(a) > 0;
    }

编辑于 2024-04-21 15:54:22 回复(0)

很暴力很弱智一看就懂的代码

#include <iostream>
#include<algorithm>
using namespace std;
bool getnum(string a, string b) {
    if (a.length() > b.length())
        return true;
    else if (a.length() == b.length()) {
        auto la = a.begin();
        auto lb = b.begin();
        while (la != a.end()) {
            if (*la > *lb)
                return true;
            if (*la < *lb)
                return false;
            la++;
            lb++;
        }
        if (la == a.end())
            return false;
    }
    else
        return false;
    return false;
}

void getred(string a, string b, string c) {
    string tmp;
    reverse(b.begin(), b.end());
    reverse(c.begin(), c.end());
    auto lb = b.begin();
    auto lc = c.begin();
    int adv = 0;
    while (lb != b.end() && lc != c.end()) {
        int pos = (*lb - '0') + (*lc - '0') + adv;
        tmp += (pos % 10 + '0');
        adv = pos / 10;
        ++lb;
        ++lc;
    }
    while (lb != b.end()) {
        int pos = (*lb - '0') + adv;
        tmp += (pos % 10 + '0');
        adv = pos / 10;
        ++lb;
    }
    while (lc != c.end()) {
        int pos = (*lc - '0') + adv;
        tmp += (pos % 10 + '0');
        adv = pos / 10;
        ++lc;
    }
    if(adv!=0)
        tmp+=(adv+'0');//这里万万不能漏掉
    reverse(tmp.begin(), tmp.end());
    getnum(tmp, a) == 1 ? cout << "Yes" << endl : cout << "No" << endl;
    return;
}
void istria(string a, string b, string c) {
    string sum, red, mid;
    if (getnum(a, b) == 1) {
        sum = b;
        red = (getnum(a, c) == 1 ? a : c);
        mid = (getnum(a, c) == 0 ? a : c);
    }
    else {
        sum = a;
        red = (getnum(b, c) == 1 ? b : c);
        mid = (getnum(b, c) == 0 ? b : c);
    }
    getred(red, mid, sum);
}

int main() {
    string a, b, c;
    while (cin >> a >> b >> c){
        istria(a, b, c);
    }
    return 0;
}
发表于 2023-08-08 22:42:11 回复(0)
//使用字符串存储数据,然后写字符串相加函数,和比较的仿函数
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>

struct Com
{
    bool operator()(const string& str1,const string& str2) const
    {
        if(str1.size() < str2.size())
        {
            return true;
        }
        else if(str1.size() == str2.size())
        {
            for(size_t i  = 0; i < str1.size(); i++)
            {
                if(str1[i] < str2[i])
                {
                    return true;
                }
                else if(str1[i] > str2[i])
                {
                    return false;
                }
            }
            
        }
        
        return false;
    }
};
string Add(const string& str1,const string& str2)
{
    //int Max =  std::max(str1.size(),str2.size());
    string res;
    //res.reserve(Max + 1);
    int index1 = str1.size() - 1;
    int index2 = str2.size() - 1;
    int sum = 0;
    int add = 0;
    while(index1 >= 0 && index2 >= 0)
    {
        int tmp = str1[index1] - '0' + str2[index2] - '0' + add;
        sum = tmp % 10;
        add = tmp / 10;
        res += sum + '0';
        index1--;
        index2--;
    }
    //cout << res << endl;
    while(index1 >= 0)
    {
        int tmp = str1[index1] - '0' + add;
        sum = tmp % 10;
        add = tmp / 10;
        res += sum + '0';
        index1--;
    }
    while(index2 >= 0)
    {
        int tmp = str2[index2] - '0' + add;
        sum = tmp % 10;
        add = tmp / 10;
        res += sum + '0';
        index2--;
    }
    //cout << res << endl;
    if(add > 0)
    {
        res += add + '0';
    }
    reverse(res.begin(),res.end());
    return res;
}
bool Judge(const vector<string>& v)
{
    string add = Add(v[0],v[1]);
    //cout << add << endl;
    if(Com()(add,v[2]) || add == v[2])
    {
        return false;
    }
    return true;
}
int main()
{
    vector<string> v(3);
    while(cin >> v[0] >> v[1] >> v[2])
    {
        sort(v.begin(),v.end(),Com());
        bool res = Judge(v);
        if(res == true)
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
    return 0;
}








发表于 2023-05-24 17:30:09 回复(0)
#include<iostream>
#include<vector>
using namespace std;
int main(){
    double a,b,c;//long long 都会爆
    while(cin>>a>>b>>c){
    if((a+b>c) && (a+c>b) && (b+c>a))
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    }
}

发表于 2022-10-23 11:44:11 回复(0)
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String a=scanner.next();
            String b=scanner.next();
            String c=scanner.next();
            if (compare(addStr(a,b),c)&&compare(addStr(a,c),b)&&compare(addStr(b,c),a))
            {
                System.out.println("Yes ");
            }
            else
            {
                System.out.println("No ");
            }
    }
    }
   private static boolean compare(String s1,String s2)
    {
        if (s1.length()==s2.length())
        {
          if (s1.compareTo(s2)>0)
          {
              return true;
          }
          else
          {
              return false;
          }
        }
        else
        {
            if (s1.length()>s2.length())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
    private static String addStr(String a, String b)
    {
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 || j >= 0 || carry != 0)
        {
            int x = i >= 0 ? a.charAt(i) - '0' : 0;
            int y = j >= 0 ? b.charAt(j) - '0' : 0;
            int sum = x + y + carry;
            sb.append(sum % 10);
            carry = sum / 10;
            i--;
            j--;
        }
        return sb.reverse().toString();

    }

发表于 2022-05-25 21:37:20 回复(0)
import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            BigDecimal a = sc.nextBigDecimal();
            BigDecimal b = sc.nextBigDecimal();
            BigDecimal c = sc.nextBigDecimal();
            if(a.add(b).compareTo(c) > 0 
              && a.add(c).compareTo(b) > 0
              &&c.add(b).compareTo(a) > 0){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
}

发表于 2022-05-07 09:58:08 回复(0)
#include <iostream>
using namespace std;
int main()
{
    double a,b,c;
    while(cin>>a>>b>>c)
    {
        if( (a+b)>c && (b+c)>a && (a+c)>b )
        {
            cout<<"Yes"<<endl;
        }
        else
        cout<<"No"<<endl;
    }
    return 0;
}

发表于 2021-03-22 10:52:59 回复(0)


对三角形判断成立条件,学过一点小学数学的都知道任意两条边之和大于第三边,不过还有另外一种判定方式任意两边之差小于第三边,其实两者核心思想是一样的,只是判断形式不同,比如a + b > c,可以称为 a > c - b 或者 b > c - a

由于数据比较大,这里我们采用任意两边之差小于第三边的方式进行判断。
注意:由于a > c - bb > c - a是等效的,所以三个条件不能写重复。

#include <iostream>
using namespace std;

int main() {
    //注意输入范围是10^100级别,所以int、long long类型会超出
    double a = 0, b = 0, c = 0;
    //scanf返回值为正确输入数据的变量个数,当一个变量都没有成功获取数据时,此时返回-1
    while (scanf("%lf %lf %lf", &a, &b, &c) != - 1) {
        //a < b + c, b < a + c, c < b + a任意两边之和大于第三版(任意两边之差小于第三边,注意别写重复了)
        //a > c - b 与 b > c - a 都是判断 a + b > c
        if (a - b < c && b - c < a && c - a < b) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }
    }
    return 0;
}
————————————————
版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://hestyle.blog.csdn.net/article/details/104699291
发表于 2020-03-06 17:00:15 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void exchange(char*, int*, int);//字符变数字并转置

int add(int*, int*, int*, int, int);//大数相加

int is_true(int*, int*, int, int);//判断是否两边之和大于第三边

int main()
{
    char a[110], b[110], c[110];
    while(~scanf("%s %s %s", a, b, c))
    {
        int len_a = strlen(a), len_b = strlen(b), len_c = strlen(c);
        int a_digit[110] = {0}, b_digit[110] = {0}, c_digit[110] = {0};
        exchange(a, a_digit, len_a);
        exchange(b, b_digit, len_b);
        exchange(c, c_digit, len_c);
        int sum1[110] = {0}, sum2[110] = {0}, sum3[110] = {0};
        int len_sum1 = add(a_digit, b_digit, sum1, len_a, len_b);
        int len_sum2 = add(a_digit, c_digit, sum2, len_a, len_c);
        int len_sum3 = add(b_digit, c_digit, sum3, len_b, len_c);
        int temp = is_true(sum1, c_digit, len_sum1, len_c) + is_true(sum2, b_digit, len_sum2, len_b) + is_true(sum3, a_digit, len_sum3, len_a);
        if(temp == 3)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

void exchange(char str[], int digit[], int len)
{
    for(int i = 0; i < len; i++)
        digit[i] = str[len - i - 1] - '0';
}

int add(int a[], int b[], int sum[], int len_a, int len_b)
{
    int l = (len_a > len_b) ? len_a : len_b;
    int count = 0;
    int i;
    for(i = 0; i < l; i++)
    {
        sum[i] = a[i] + b[i] + count;
        count = sum[i] / 10;
        sum[i] %= 10;
    }
    if(count)
    {
        sum[l] = count;
        l++;
    }
    return l;
}

int is_true(int sum[], int c[], int len_sum, int len_c)
{
    if(len_sum > len_c)
        return 1;
    else if(len_sum < len_c)
        return 0;
    else
    {
        for(int i = len_c - 1; i >= 0; i--)
        {
            if(sum[i] > c[i])
                return 1;
            else if(sum[i] < c[i])
                return 0;
        }
    }
    return 0;
}

编辑于 2020-03-03 20:19:30 回复(0)