前些日子朋友问我这样一道题: 八枚硬币中存在着一枚假币,假币特点:要么轻要么重,用程序描述下来  要求取出假币,并判断轻重。
如下程序,如有不对,敬请批正
 
package run;
/**
 * 八枚硬币中存在着一枚假币,假币特点:要么轻要么重,用程序描述下来
 *
 * 要求取出假币,并判断轻重
 *
 * @author JRunner
 *
 * Email:jrunner@126.com
 */
public class Test_1 {
 /**
  * 方法一
  */
 public static void main(String[] args) {
  int[] c = { 4, 4, 4, 4, 4, 4, 4, 10 };
  int j = 0;// 记录两枚不同的硬币中的一个 范围1-7
  for (int i = 1; i < 8; i++) {
   if (c[i] != c[0]) {
    j = i;
    break;
   }
  }
  // 如果运气好,第一次就取出不同点两枚c[0] 、 c[1]
  if (j == 1) {
   if (c[0] == c[2]) {
    print(c[1], c[2], 1);
   } else {
    print(c[0], c[2], 0);
   }
  } else {
   print(c[j], c[0], j);
  }
 }
 private static void print(int jia, int zhen, int i) {
  if (jia > zhen) {
   System.out.println("位置为" + (i + 1) + "是假币,且偏重");
  } else {
   System.out.println("位置为" + (i + 1) + "是假币,且偏轻");
  }
 }
}