编程~任意输3个数,判断能否构成直角三角形
- 提问者网友:疯子也有疯子的情调
- 2021-08-15 00:38
- 五星知识达人网友:人類模型
- 2021-08-15 02:05
这是C++版的
#include <iostream>
using namespace std;
bool Istriangle(float &a,float &b,float &c) //实参做成引用,节省空间
{
if((a+b)>c &&(a+c)>b &&(b+c)>a) //构成三角形的条件
{
return true;
}
cout<<"不能构成三角形"<<endl;
return false;
}
bool IsRighttringle(float &a,float &b,float &c)
{
if ((a*a==b*b+c*c) || (b*b==a*a+c*c) || (c*c==a*a+b*b))
{
return true; //直角三角形
}
else
return false; //非直角三角形
}
void main()
{
float a,b,c;
cout<<"请输入三条边长a,b,c:"<<endl;
while (1)
{
cin>>a>>b>>c;
if (a>0&&b>0&&c>0) //第一步判断,边必须大于0.
{
break;
}
cout<<"边必须为正数,请重新输入a,b,c:"<<endl;
}
if (Istriangle(a,b,c))
{
cout<<"边长为"<<a<<","<<b<<","<<c<<"的三角形";
if (IsRighttringle(a,b,c))
{
cout<<"是直角三角形"<<endl;
}
else
cout<<"不是直角三角形"<<endl;
}
}
- 1楼网友:老鼠爱大米
- 2021-08-15 07:26
三角形判断: package pack_triangle; import java.io.*; public class Triangle { public static void main(String[] args)throws IOException { double a,b,c; BufferedReader in1=new BufferedReader(new InputStreamReader(System.in)); BufferedReader in2=new BufferedReader(new InputStreamReader(System.in)); BufferedReader in3=new BufferedReader(new InputStreamReader(System.in)); System.out.print("请输入边长a: "); String line1=in1.readLine(); a=Double.parseDouble(line1); System.out.print("请输入边长b: "); String line2=in2.readLine(); b=Double.parseDouble(line2); System.out.print("请输入边长c: "); String line3=in3.readLine(); c=Double.parseDouble(line3); if(a+b>c && a+c>b && b+c>a) System.out.println("a、b、c可以构成三角形!"); else System.out.println("a、b、c不可以构成三角形!"); }
}
- 2楼网友:风格不统一
- 2021-08-15 06:35
#include<stdio.h>
int main()
{
int a,b,c;
printf("please input 3 num:");
scanf("%d%d%d",&a,&b,&c);
if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(a*a==b*b+c*c))
return 1;
else return 0;}
- 3楼网友:千杯敬自由
- 2021-08-15 05:23
#include <iostream> #include <conio.h> using namespace std;
int main() { int x,y,z; cin>>x>>y>>z; if (x+y<=z) { cout<<"No"<<endl; return -1; } else if (x+z<=y) { cout<<"No"<<endl; return -1; } else if (y+z<=x) { cout<<"No"<<endl; return -1; } else cout<<"Yes"<<endl; getch(); return 0; }//不就是判断一下三个边吗,
- 4楼网友:持酒劝斜阳
- 2021-08-15 04:17
- 5楼网友:青灯有味
- 2021-08-15 03:35
完整程序
package test;
import java.util.InputMismatchException; import java.util.Scanner;
public class Test {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请输入三个数字 用空格分隔:"); try { Double d1 = scanner.nextDouble(); Double d2 = scanner.nextDouble(); Double d3 = scanner.nextDouble(); if (d1 <= 0 || d2 <= 0 || d3 <= 0) throw new Exception("三角形的边长不能小于0!");// 抛出异常 输入不合法 else if (d1 * d1 == d2 * d2 + d3 * d3 || d2 * d2 == d1 * d1 + d3 * d3 || d3 * d3 == d2 * d2 + d1 * d1) System.out.println("可以构成直角三角形");
else System.out.println("不能构成直角三角形"); break; } catch (InputMismatchException e) { System.out.println("输入不为数字!"); scanner = new Scanner(System.in); }catch (Exception e) { System.out.println(e.getMessage()); scanner = new Scanner(System.in); } } }
}