1 有十二个球,大小形状相同,其中有一个是劣质球,或轻或重无法用感觉判断,现有一无刻度的天平,请用三次称量确定那个劣质球。(我想了很长时间,没想出来,朋友说有算法,我也不知道怎么算,求解)2 写出一个测试输入的三个值是三角形的三个边的程序。(这个会写,想看看大家是怎么写的)
解决方案 »
- 一个myeclipse连接sqlserver2000的问题。本人跪求大侠!!!
- 自己写的jar包,怎么加密,请各位帮帮忙
- 如何获取ajax调用的页面部分
- 请问 jsp如何显示dreamwaver里的 spry选项卡式面板~小弟想不知道需要什么才能显示~万分感谢!
- jsp表单验证失败,求各位高手解答
- 急等JAVASCRIP 小问题
- 有使用WebLogic Platform 8.1的吗 请教几个问题 谢谢了
- 关于jboss3.2.4负载均衡问题,急,高分相赠!
- 现在SPRING + HIBERNET 未来JSF + EJB3.0?
- EJB 事务问题 请教高手,项目正急,在线等~~~~~~~~~~~~~~~~~
- 求救 struts2.3.1 类型转换异常
- struts2 验证的问题
三角形的。。任何2边相加都大于第3边
Scanner input = new Scanner(System.in);
int i = input.nextInt();
int j = input.nextInt();
int k = input.nextInt();
if((i+j)>k && (i+k)>j && (k+j)>i
&& i>0 && j>0 && k>0)
{
System.out.println("ok");
}
public class Tritangle {
private double a;
private double b;
private double c;
public Tritangle(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
public boolean isTritangle(){
if(a+b>c && b+c>a && c+a>b){
return true;
}
return false;
}
public static void main(String[] args){
Tritangle t = new Tritangle(2,3,5);
System.out.println("isTritangle="+t.isTritangle());
}
}
private double a;
private double b;
private double c;
public Tritangle(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
public boolean isTritangle(){
if(a+b>c && b+c>a && c+a>b && a>0 && b>0 && c>0){
return true;
}
return false;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
Tritangle t = new Tritangle(a,b,c);
System.out.println("isTritangle="+t.isTritangle());
}
}
我又找了一个链接 http://tieba.baidu.com/p/326192655?pn=1 这里也有,先声明,我自己想不出来的
1234,5678,9abc
1234--5678
相等:12345678正常,9abc不正常
123--abc
相等: 9劣质
不相等:
b--c
相等: a劣质
不相等: 根据123--abc的轻重,判断劣质球的轻重\
不相等:9abc正常
1235--4abc(abc 替换678;4与5交换)
相等: 678不正常
不相等:
若平衡发生了变化,说明4,5不同; 则4或5劣质,
若平衡没有发生变化,说明abc,678,45都是正常的。则123有问题
二选一,三选一和上面就一样了。
将劣质球所在的组分为3组球(组1,1,2个),2组对比(1对1)。如果同行。说明劣质球在第3组.
或轻的就是劣质球
公式:12=1(4)+2(4)+3(4);
第一次对比:1(4)=2(4)?Y:N 得到1组
4=1(1)+2(1)+3(2)
第二次对比:1(1)=2(1)?Y:N 如果结果为1(1)/2(1) 称重结束
2=1(1)+2(1)
第三次对比:1(1)=2(1)?Y:N 得到结果1(1)/2(1)
1) 1234=5678 -> 123=90a -> b有问题
-> 123 > 90a -> 9=0 -> a异常-轻
-> 9>0 -> 0异常-轻
2) 1234>5678 -> 4567=890a -> 1=2 -> 3异常-重
-> 1>2 -> 1异常-重
-> 4567>890a -> 1=4 -> 8异常-轻
-> 4>1 -> 4异常-重
-> 4567<890a -> 5=6 -> 7异常-轻
-> 5>6 -> 6异常-轻
private static int[] ll ;
private static int l ;
private static int j ;
private static int index;
private static String type;
public static void main(String[] args){
init();
start();
showResult();
}
private static void init(){
l = 0;
ll = new int[12];
for(int i = 0 ; i<12 ; i++){
ll[i] = 5;
}
j = new Double(Math.random()*12).intValue();
//j = 9;
while(ll[j]==5){
ll[j] = new Double(Math.random()*10).intValue()+1;
}
for(int i = 0 ; i<12 ; i++){
System.out.print(ll[i]+"--");
}
System.out.println("初始化完成");
}
private static void start(){
switch(doCompare(ll[0]+ll[1]+ll[2]+ll[3],ll[4]+ll[5]+ll[6]+ll[7],"1,2,3,4","5,6,7,8")){
case 0 : {
switch(doCompare(ll[0]+ll[1],ll[8]+ll[9],"1,2","9,10")){
case 0 :{
switch(doCompare(ll[0],ll[10],"1","11")){
case 0 : index = 12; type="未能判断轻重"; break;
case -1 : index = 11; type="重"; break;
case 1 : index = 11; type="轻"; break;
}
break;
}
case -1 :{
switch(doCompare(ll[0],ll[8],"1","9")){
case 0 : index = 10; type="重"; break;
case -1 : index = 9; type="重"; break;
case 1 : System.out.println("此为异常状况!");System.exit(0);
}
break;
}
case 1 :{
switch(doCompare(ll[0],ll[8],"1","9")){
case 0 : index = 10; type="轻"; break;
case -1 : System.out.println("此为异常状况!");System.exit(0);
case 1 : index = 9; type="轻"; break;
}
break;
}
}
break;
}
case -1 :{
switch(doCompare(ll[3]+ll[4]+ll[5]+ll[6],ll[7]+ll[8]+ll[9]+ll[10],"4,5,6,7","8,9,10,11")){
case 0 : {
switch(doCompare(ll[0],ll[1],"1","2")){
case 0 : index = 3 ; type="轻" ; break;
case -1 : index = 1 ; type="轻" ; break;
case 1 : index = 2 ; type="轻" ; break;
}
break;
}
case -1 : {
switch(doCompare(ll[0],ll[3],"1","4")){
case 0 : index = 8 ; type="重" ; break;
case -1 : System.out.println("此为异常状况!");System.exit(0);
case 1 : index = 4 ; type="轻" ; break;
}
break;
}
case 1 : {
switch(doCompare(ll[4],ll[5],"5","6")){
case 0 : index = 7 ; type="重" ; break;
case -1 : index = 6 ; type="重" ; break;
case 1 : index = 5 ; type="重" ; break;
}
break;
}
}
break;
}
case 1 :{
switch(doCompare(ll[3]+ll[4]+ll[5]+ll[6],ll[7]+ll[8]+ll[9]+ll[10],"4,5,6,7","8,9,10,11")){
case 0 : {
switch(doCompare(ll[0],ll[1],"1","2")){
case 0 : index = 3 ; type="重" ; break;
case -1 : index = 2 ; type="重" ; break;
case 1 : index = 1 ; type="重" ; break;
}
break;
}
case -1 : {
switch(doCompare(ll[4],ll[5],"5","6")){
case 0 : index = 7 ; type="轻" ; break;
case -1 : index = 5 ; type="轻" ; break;
case 1 : index = 6 ; type="轻" ; break;
}
break;
}
case 1 : {
switch(doCompare(ll[0],ll[3],"1","4")){
case 0 : index = 8 ; type="轻" ; break;
case -1 : index = 4 ; type="重" ; break;
case 1 : System.out.println("此为异常状况!");System.exit(0);
}
break;
}
}
break;
}
}
}
private static int doCompare(int a,int b,String aa, String bb){
//System.out.println
System.out.println("----左侧放入"+aa+"球,右侧放入"+bb+"球;");
System.out.print("进行第"+(++l)+"次比较,");
if(a==b){
System.out.println("左右平衡;");
return 0;
}else if(a>b){
System.out.println("左面比右面重;");
return 1;
}else{
System.out.println("左面比右面轻;");
return -1;
}
}
private static void showResult(){
//System.out.println
System.out.println("--计算结果为:第"+index+"球劣质("+type+"),应为5,实际为"+ll[index-1]);
if(j!=--index){
System.out.println("计算出现错误,跳出!");
System.exit(0);
}
}
}