首页 > 试题广场 >

三角形的边

[编程题]三角形的边
  • 热度指数:7154 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给定三个已知长度的边,确定是否能够构成一个三角形,这是一个简单的几何问题。我们都知道,这要求两边之和大于第三边。实际上,并不需要检验所有三种可能,只需要计算最短的两个边长之和是否大于最大那个就可以了。 这次的问题就是:给出三个正整数,计算最小的数加上次小的数与最大的数之差。

输入描述:
每一行包括三个数据a, b, c,并且都是正整数,均小于10000。


输出描述:
对于输入的每一行,在单独一行内输出结果s。s=min(a,b,c)+mid(a,b,c)-max(a,b,c)。上式中,min为最小值,mid为中间值,max为最大值。
示例1

输入

1 2 3
6 5 4
10 20 15
1 1 100
0 0 0

输出

0
3
5
-98
#include<iostream>
using namespace  std;
int main(){
    int a,b,c;
    while(cin>>a>>b>>c){
        int max;
        max=a>b?a:b;
        max=max>c?max:c;
        cout<<a+b+c-max-max<<endl;
    }
    return 0;
}
发表于 2018-05-18 11:06:12 回复(2)

python solution, simple enough:


while True:
    try:
        a,b,c=sorted(map(int,input().split()))
        if a!=0:
            print(a+b-c)
    except:
        break
发表于 2017-10-03 18:15:07 回复(0)
//第一次没有看别人写的,没有用编译器调试做出来的,加油!
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int a[3],b[1000],i=0;
    while(cin>>a[0]&&a[0]!=0){
        cin>>a[1]>>a[2];
        stable_sort(a,a+3);
        b[i]=a[0]+a[1]-a[2];
        i++;
    }
    for(int j=0;j<i;j++)
        cout<<b[j]<<endl;
    return 0;
}

编辑于 2017-12-05 14:51:19 回复(0)
三个数加和减去2倍的max就好了。
def Triangle():
    listStr = input().split()
    for i in range(len(listStr)):
        listStr[i] = int(listStr[i])
    max = listStr[0]
    sum = 0
    for i in range(len(listStr)-1):
        if listStr[i+1] > max:
            max = listStr[i+1]
    for i in range(len(listStr)):
        sum += listStr[i]
    s = sum - 2 * int(max)
    print(s)
Triangle()

发表于 2018-07-30 13:03:43 回复(0)
#include<iostream>

using namespace std;

class CTriangle{
    public:
        int a_,b_,c_;
        
        int Max(){
            if(a_>b_&&a_>c_)    return a_;
            if(b_>a_&&b_>c_)    return b_;
            if(c_>a_&&c_>b_)    return c_;
        }
        int Mid(){
            if(a_>b_&&a_<c_)    return a_;
            if(b_>a_&&b_<c_)    return b_;
            if(c_>a_&&c_<b_)    return c_;
        }
        int Min(){
            if(a_<b_&&a_<c_)    return a_;
            if(b_<a_&&b_<c_)    return b_;
            if(c_<a_&&c_<b_)    return c_;            
        }
        
        void init(int a,int b,int c){
            a_ = a;
            b_ = b;
            c_ = c;
        }
};

int main(){
    CTriangle Triangle;
    int a,b,c;
    while(cin >> a >> b >> c){
        Triangle.init(a,b,c);
        cout << Triangle.Min() + Triangle.Mid() - Triangle.Max() << endl;
    }
    
}

发表于 2018-04-20 17:25:12 回复(0)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int a[3];
	while(cin >> a[0] >> a[1] >> a[2])
	{
		if(a[0]==0 && a[1]==0 && a[2]==0)
			break;
		sort(a, a+3);
		cout << a[0]+a[1]-a[2] << endl;
	}
	return 0;
}

发表于 2021-02-13 19:09:10 回复(0)
#include<stdio.h>//找到最大的数放在c中
int main()
{
    int a,b,c,d,t;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
      {t=a;a=b;b=t;}
    if(b>c)
      {t=b;b=c;c=t;}   
    d=a+b-c;
    printf("%d\n",d);
}

编辑于 2020-03-26 22:02:52 回复(0)
//再次sort大法好
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[3];
    while(cin>>a[0]>>a[1]>>a[2]){
        if(a[0]==0&&a[1]==0&&a[2]==0)
            break;
        sort(a,a+3);
        cout<<a[0]+a[1]-a[2]<<endl;
    }
}

发表于 2020-01-13 21:00:39 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[3]={0};
    while(scanf("%d %d %d",&a[0],&a[1],&a[2])!=EOF){
        sort(a,a+3);
        printf("%d\n",a[0]+a[1]-a[2]);
    }
    return 0;
}
发表于 2019-03-04 14:10:54 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int[] data = new int[3];
            for (int i = 0; i < data.length; i++) {
                data[i] = scan.nextInt();
            }
            Arrays.sort(data);
            System.out.println(data[0] + data[1] - data[2]);
        }
    }
}
发表于 2018-05-12 13:33:19 回复(2)
#include<stdio.h>
int main (int argc, char *argv[]){// the shorter,the better.
    int a,b,c;for(;~scanf("%d%d%d",&a,&b,&c)&&a&&~printf("%d\n",a+b+c-2*((a>b?a:b)>c?(a>b?a:b):c)););
}

发表于 2018-01-12 12:45:54 回复(0)
Java解法
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] record = new int[3];
        while (scanner.hasNext()) {
            for (int i = 0; i < 3; i++) {
                record[i] = scanner.nextInt();
            }
            Arrays.sort(record);
            System.out.println(record[0] + record[1] - record[2]);
        }
    }
}


发表于 2020-03-06 11:41:27 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int num[3];
	while(cin>>num[0]>>num[1]>>num[2])
	{
		if(!num[0]&&!num[1]&&!num[2])	break;
		sort(num,num+3);
		cout<<num[0]+num[1]-num[2]<<endl;
	}
	return 0;
}

编辑于 2024-03-19 16:07:38 回复(0)
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    while (cin >> a >> b >> c) {
        cout << a + b + c - 2 * max(max(a, b), c) << endl;
    }
    return 0;
}

编辑于 2024-03-04 10:21:00 回复(0)
#include <stdio.h>

int main() {
    int a,b,c;
    int temp;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF){
        if(a>=c){
            temp = a;
            a = c;
            c = temp;
        }
        if(a>=b){
            temp = a;
            a = b;
            b = temp;
        }
        if(b>=c){
            temp = c;
            c = b;
            b = temp;
        }
        printf("%d\n",a+b-c);
    }
    
    return 0;
}

发表于 2023-03-12 21:37:18 回复(0)
#include <iostream>
#include <algorithm>
using namespace std;

int calDiff(int a,int b,int c){
    int diff;
    int maxn = max({a,b,c});
    diff = (a + b + c) - 2*maxn;
    return diff;
}

int main() {
    int a, b, c;
    while(cin >> a >> b >> c) {
        if(!(a == 0 && b == 0 && c == 0)){
            cout << calDiff(a, b, c) << endl;
        }
    }
}

发表于 2023-01-22 20:45:58 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            String[] ss = s.split(" ");
            ArrayList<Integer> list = new ArrayList<>();
            list.add(Integer.parseInt(ss[0]));
            list.add(Integer.parseInt(ss[1]));
            list.add(Integer.parseInt(ss[2]));
            list.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 - o2;
                }
            });
            System.out.println(list.get(0) + list.get(1) - list.get(2));

        }
    }

}


发表于 2021-04-03 13:48:54 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c;
    while(cin>>a>>b>>c)
    {
        int temp,ans;
        temp=max(a,b);
        ans=max(temp,c);
        cout<<a+b+c-ans-ans<<endl;
    }
}

发表于 2021-03-16 17:28:03 回复(0)
#include <iostream>
#include <algorithm>

using namespace std;



int main() {
    int arr[3];
    while (cin >> arr[0] >> arr[1] >> arr[2]) {
        sort(arr, arr + 3);
        cout << arr[0] + arr[1] - arr[2] << endl;
    }
    return 0;
}

发表于 2021-01-12 16:16:01 回复(0)

问题信息

难度:
70条回答 5241浏览

热门推荐

通过挑战的用户

查看代码