A+B and C(java版本)

题目描述:
给出三个整数A,B,C,如果A+B>C,则输出true;否则,输出false。
由于long的范围是[-2^63 ,2^63),因此题目会出现相加可能越界的情况所以定义long类型而不是int类型。

package 算法笔记;
import java.util.Scanner;
//A+B and C(注意本题目中的int型变量容易越界,所以改用Long型)
public class test0004 {
	public static void main(String [] args)
	{
		Scanner sc=new Scanner(System.in);
		int n;
		n=sc.nextInt();
		int []a=new int[n];
		for(int i=0;i<n;i++)
		{
			long A,B,C;
			A=sc.nextLong();
			B=sc.nextLong();
			C=sc.nextLong();
			if(A+B>C)
				a[i]=1;
			else a[i]=0;
		}
		for(int i=0;i<n;i++)
		{
			if(a[i]==0)
				System.out.println("Case#:"+(i+1)+"false");
			else
				System.out.println("Case#:"+(i+1)+"true");
		}
	}
}

输入样例:

3
1 2 3 
2 3 4
9223372036854775807 -9223372036854775808 0

输出样例:

Case#:1false
Case#:2true
Case#:3false

运行结果截图如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
```python a = float(input()) b = float(input()) c = float(input()) if a+b > c and a+c > b and b+c > a: s = (a+b+c)/2 area = (s*(s-a)*(s-b)*(s-c))**0.5 perimeter = a+b+c print("area = {:.2f}; perimeter = {:.2f}".format(area, perimeter)) else: print("These sides do not correspond to a valid triangle") ``` ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double a = in.nextDouble(); double b = in.nextDouble(); double c = in.nextDouble(); if (a+b > c && a+c > b && b+c > a) { double s = (a+b+c)/2; double area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); double perimeter = a+b+c; System.out.printf("area = %.2f; perimeter = %.2f", area, perimeter); } else { System.out.println("These sides do not correspond to a valid triangle"); } } } ``` ```c++ #include <iostream> #include <cmath> using namespace std; int main() { double a, b, c; cin >> a >> b >> c; if (a+b > c && a+c > b && b+c > a) { double s = (a+b+c)/2; double area = sqrt(s*(s-a)*(s-b)*(s-c)); double perimeter = a+b+c; printf("area = %.2f; perimeter = %.2f", area, perimeter); } else { cout << "These sides do not correspond to a valid triangle"; } return 0; } ``` ```python a, b, c = map(float, input().split()) if a+b > c and a+c > b and b+c > a: s = (a+b+c)/2 area = (s*(s-a)*(s-b)*(s-c))**0.5 perimeter = a+b+c print("area = {:.2f}; perimeter = {:.2f}".format(area, perimeter)) else: print("These sides do not correspond to a valid triangle") ``` ```c++ #include <iostream> #include <cmath> using namespace std; int main() { double a, b, c; cin >> a >> b >> c; if (a+b > c && a+c > b && b+c > a) { double s = (a+b+c)/2; double area = sqrt(s*(s-a)*(s-b)*(s-c)); double perimeter = a+b+c; printf("area = %.2f; perimeter = %.2f", area, perimeter); } else { cout << "These sides do not correspond to a valid triangle"; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值