import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[3]; arr[0] = sc.nextInt(); arr[1] = sc.nextInt(); arr[2] = sc.nextInt(); Arrays.sort(arr); while (arr[2] >= arr[0] + arr[1]) { arr[2]--; } System.out.println(arr[0] + arr[1] + arr[2]); } }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); int a[]=new int [3]; for(int i=0;i<3;i++) { a[i]=cin.nextInt(); } Arrays.sort(a); if(a[0]+a[1]<=a[2])a[2]=a[2]-(a[2]-(a[0]+a[1]))-1; int out=0; out=a[0]+a[1]+a[2]; System.out.print(out); } }
三角形的构成规则: 任意两边之和大于第三边,任意两边之差小于第三边 转化为 最小的两边之和大于最大的边 所以,先对三条边进行排序,若满足,则直接输出;若不满足,则缩短最大的边 #include <iostream> #include <algorithm> using namespace std; int main() { int t[3]={0}; for(int i=0;i<3;i++) { cin>>t[i]; } sort(t,t+3); if(t[0]+t[1]>t[2]) { cout<<t[0]+t[1]+t[2]<<endl; } else { t[2]=t[0]+t[1]-1; cout<<t[0]+t[1]+t[2]<<endl; } return 0; }
#include<iostream> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; while(1){ if(a+b<=c) c=a+b-1; if(a+c<=b) b=a+c-1; if(b+c<=a) a=b+c-1; if(a+b>c&&b+c>a&&a+c>b) break; } cout<<a+b+c<<endl; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] lens = new int[3]; for (int i=0; i!=3; i++) { lens[i] = sc.nextInt(); } Arrays.sort(lens); if (lens[0] + lens[1] > lens[2]) { System.out.println(lens[0] + lens[1] + lens[2]); } else { System.out.println(2 * (lens[0] + lens[1]) - 1); } } }
import java.util.*;
string input = string.Empty; while (!string.IsNullOrEmpty(input = Console.ReadLine())) { int[] arr = input.Split(' ').Select(x => int.Parse(x)).OrderBy(x => x).ToArray(); int a = arr[0]; int b = arr[1]; int c = arr[2]; if (a + b > c) { Console.WriteLine(a + b + c); } else { c = a + b - 1; Console.WriteLine(a + b + c); } }
#include<iostream> #include<algorithm> using namespace std; int main() { int a[3],i; for(i=0;i<3;++i) cin>>a[i]; sort(a,a+3); if(a[0]+a[1]>a[2]) printf("%d",a[0]+a[1]+a[2]); else printf("%d",2*(a[0]+a[1])-1); }