求助 一个很简单的ACM题 java代码 自己电脑上测都是正确的 但提交总是wrong answer 先谢谢了

题意大致是,给出直角三角形a,b,c(c为斜边)中两个的值(都是正数),求值为-1的那个边。比如3 4 -1,就是求c的长度,输入0 0 0表结束,输出格式如下加粗部分,我自己测自己的运行结果都是正确的,不知为什么总是wa,求大神指点!

Mathematics can be so easy when you have a computer. Consider the following example. You probably know that in a right-angled triangle, the length of the three sides a, b, c (where c is the longest side, called the hypotenuse) satisfy the relation a*a+b*b=c*c. This is called Pythagora‘s Law.

Here we consider the problem of computing the length of the third side, if two are given.

Input

The input contains the descriptions of several triangles. Each description consists of a line containing three integers a, b and c, giving the lengths of the respective sides of a right-angled triangle. Exactly one of the three numbers is equal to -1 (the ‘unknown‘ side), the others are positive (the ‘given‘ sides).

A description having a=b=c=0 terminates the input.

Output

For each triangle description in the input, first output the
number of the triangle, as shown in the sample output. Then print "Impossible."
if there is no right-angled triangle, that has the ‘given‘ side lengths.
Otherwise output the length of the ‘unknown‘ side in the format "s = l", where s
is the name of the unknown side (a, b or c), and l is its length. l must be
printed exact to three digits to the right of the decimal point.

Print a blank line after each test case.

Sample Input

3 4 -1
-1 2 7
5 -1 3
0 0 0


Sample Output

Triangle #1
c = 5.000

Triangle #2
a =
6.708

Triangle #3
Impossible.

我的代码

import java.text.DecimalFormat;
import java.util.Scanner;

public class Geometry1241 {

static double a, b, c, result;

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int temp = 1;
String No = "Impossible.";
DecimalFormat df = new DecimalFormat("#.000");
while (sc.hasNext()) {
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if (a == b && b == c && c == 0)
break;
if (a == -1) {
result = c * c - b * b;
if (result <= 0 || c <= 0 || b <= 0) {
System.out.println("Triangle #" + temp);
System.out.println(No);
continue;
} else {
a = Math.sqrt(result);
System.out.println("Triangle #" + temp);
System.out.println("a = " + df.format(a));
}
}
if (b == -1) {
result = c * c - a * a;
if (result <= 0 || c <= 0 || a <= 0) {
System.out.println("Triangle #" + temp);
System.out.println(No);
continue;
} else {
b = Math.sqrt(result);
System.out.println("Triangle #" + temp);
System.out.println("b = " + df.format(b));
}
}
if (c == -1) {
if (a <= 0 || b <= 0) {
System.out.println("Triangle #" + temp);
System.out.println(No);
}
result = a * a + b * b;
c = Math.sqrt(result);
System.out.println("Triangle #" + temp);
System.out.println("c = " + df.format(c));

}
temp++;
System.out.println();
}

}

}

时间: 2024-08-04 09:13:22

求助 一个很简单的ACM题 java代码 自己电脑上测都是正确的 但提交总是wrong answer 先谢谢了的相关文章

Mybatis_reveiw之Mybatis官方的一个很简单的Demo

上学的时候,一个老师讲了个故事,这个故事的大意是,我们有很多种方式去削苹果,第一种方式,使用指甲刀,第二种方式,使用机床,第三种方式,使用手摇的那种削平果小工具.我们当然都能够完成这个简单的需求,但是使用指甲刀削出来的苹果一定比较坑坑洼洼,不够美观,而且可能会让人感觉到有点没啥食欲.使用机床呢?可能会造成大量的浪费,原本一个美观大方的苹果变成了只能啃几口的正方形.第三个,因为是专门为了削苹果皮而设计的,理论上是最合适用来解决削苹果这个问题的解决方案. 一个好的架构,其实要做的事情是非常简单的,除

[.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office

打造一个很简单的文档转换器 - 使用组件 Spire.Office 目录 Spire.Office 介绍 库引用 界面预览 代码片段 Spire.Office 介绍 关于 Spire.Office,它是一个专门为开发人员创建,读取,写入设计的库,转换和从打印 word 文档文件.作为一个独立的 .NET组件,它不需要在机器上安装微软的 Word 等办公软件.然而,它可以将微软的“文档创建功能”集成到任何开发人员的网络应用程序中.它是一个可靠的 MS Word 的API,可以执行许多Word文档处

Java代码实现文件上传(转载)

刚刚发表了一篇Java发送电子邮件,以前真是没注意,commons里这么多常用项目,惭愧呀,直到现在回顾;要学习的真是太多了,还是缺少真正的学习能力... 这里用到的是commons-fileupload.jar与commons-io.jar;下载地址http://commons.apache.org/ 这是一个web工程,所以先在webroot下新建一个jsp,其实直接用index.jsp就行了;下面是前端代码: <%@ page language="java" import=

HDU 1840 Equations (简单数学 + 水题)(Java版)

Equations 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1840 --每天在线,欢迎留言谈论. 题目大意: 给你一个一元二次方程组,a(X^2) + b(X) + c = 0 .求X解的个数. 思路: 分别讨论二次方程与一次方程的情况,再特殊处理下 a = b = c = 0 的情况. 感想: 是时候该水水题了. Java AC代码: 1 import java.math.*; 2 import java.util.Scanner; 3

Gradle之旅-遇到的一个很简单但是用了很久才解决的问题

这个问题非常简单,但是对于我这个刚接触Gradle的人却是很难发现的一个错误,这个错误就是在使用gradle build的时候提示can not find tools.jar,我手动的添加了依赖,反复修改都没能解决这个错误,后来我无意中点到window->references->java->install jres发现这里定义的jre是jdk下的jre,不是jdk,然后我将定义的这个jre修改了为jdk的路径,然后重新运行gradle build,然后问题就解决了,虽然是个很小的问题,但

一个很简单的SqlServer生成常用C#语句工具的诞生

前言: 这个文章只要是记录一下,这个工具的诞生过程.作用.其中的技术实在是太简单可以说没有什么技术~主要是锻炼一下写文章的能力! 正文: 在开发项目的时,常常会要维护或变更一些老项目,涉及到简单的几张表的操作,大部分都是ADO操作数据库的代码!写写改改.添加.删除什么的.用那些代码生成器什么的,都需要配置不少东西,况且我真正要生成的只要一小段代码,自食其力,就自己弄一个这样的工具来帮助自己吧. 既然是对SqlServer进行操作的工具,肯定第一步要实现对SqlServer的链接了也就是登陆Sql

一个很简单的脑筋急转弯问题

你只能增加或是修改其中一个字符,使其输出20个减号.请给出3种方案int n = 20 ;for(int i=0 ; i<n ; i--){printf("-"); } 自己想了下,然后总结了大概一下5中,做下记录. ----------------i-- 改为n---i<ni+n|x| < ni++

webStrom访问只一个很简单的html文件的时候显示local host无法访问。。

直接从文件夹运行html没问题的,,,然后百度半天,,乱七八糟的答案, 1.谷歌商店安装 JB插件--插件地址 https://chrome.google.com/webstore/detail/jetbrains-ide-support/hmhgeddbohgjknpmjagkdomcpobmllji WebStorm live edit功能与浏览器实现同步实现步骤 2.更改设置 解决webstorm本地IP访问页面出错的问题 ctrl+alt+s调出设置:改一下端口,啥的 3. html文件

一个很简单小数正负数行转列问题

出处:kelvin19840813 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. 发现decode行转列之后 , 小数, 负数都会自动抹去 , 需要to_char加工一下 , 并没有找到相关文献解释为什么抹去 SQL> create table test0724 (name varchar(20),month number(2),num NUMBER(6,2));