// 最短两边大于最长边即可。核心代码总量: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");}}}}
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"); } } }
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; }
用字符串表示整数,定义加法和比较大小的函数 #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; } }
#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; } }
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"); } } }
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; }
很暴力很弱智一看就懂的代码
#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; }
//使用字符串存储数据,然后写字符串相加函数,和比较的仿函数 #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; }
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(); }
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"); } } } }
对三角形判断成立条件,学过一点小学数学的都知道任意两条边之和大于第三边
,不过还有另外一种判定方式任意两边之差小于第三边
,其实两者核心思想是一样的,只是判断形式不同,比如a + b > c
,可以称为 a > c - b
或者 b > c - a
。
由于数据比较大,这里我们采用任意两边之差小于第三边
的方式进行判断。
注意:由于a > c - b
与 b > 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
#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; }