2013年10月31日星期四

Find great God, three java questions . Solution !

6 , there are A, B, C, D and E of five schools . In an inspection and assessment , known E certainly not the first 2 or 3 , they speculate with each other , A school was said , E school is definitely the first one ; B school has been said that our school is probably the first two ; C school was said , A school worst ; D school was said , C school is not the best ; E school was said , D -School won first place . Results of only the first and second school who guessed right . Programming pointed out that the five school term.

7, a digital 5,6,7,8,9 , any combination of the five numbers into two numbers, such as 57 and 689 , each number can not be reused , will be composed of two numbers are multiplied , will get a product . Please programming consisting of computing the product of two numbers which maximum .

8, will be 1-9 this nine digit fill nine spaces , each composed of a rampant three -digit number , if you want the second line is the first line of two -digit times , the third line is the first line of the three-digit digit three times , what should fill the number ? As shown below:

1 9 2
3 8 4
5 7 6
------ Solution ------------------------------- -------------
question 3 is not stupid method

/**
 * 8、将1-9这九个数字填入九个空格中,每一横行的三个数字组成一个三位数,如果要使第二行的三位数是第一行的两倍,第三行的三位数是第一行三位数的3倍,
 * 应该怎样填数?如下图所示:
 * 
 * 1 9 2 
 * 3 8 4 
 * 5 7 6
 */
public class Test014 {
public static void main(String[] args) {
fun1();
}

static void fun1() {
for (int i = 129; i <= 327; ++i) {
if (i % 5 == 0)
continue;
String str = "" + i + i * 2 + i * 3;
if (check(str)) {
System.out.println("***find***");
System.out.println(i + "\n" + i * 2 + "\n" + i * 3);
}
}
}

static boolean check(String str) {
int[] arr = new int[10];
char[] chars = str.toCharArray();
for (char c : chars) {
++arr[c - '0'];
}
boolean result = true;
for (int i = 1; result && i < arr.length; ++i) {
result &= (arr[i] == 1);
}
return result;
}
}


------ Solution ------------------------------------- -------
question 2
The answer is right, do not know will not appear anything unusual ,


/**
 * 7、有数字5、6、7、8、9,将五个数字任意组合成两个数字,比如57和689,每个数字不能重复使用,将组成的两个数字相乘,会得到一个乘积。
 * 请编程计算组成的哪两个数字的乘积最大。
 * 
 * 
 */
public class Test013 {
public static void main(String[] args) {
findMax();
}

static void findMax() {
int[] arr = new int[] { 9, 8, 7, 6, 5 };
int n1 = 0,n2 = 0;
for (int i : arr) {
if(n1 == 0) n1 = i;
else if(n2 == 0) n2 = i;
else if (n1 > n2) n2 = n2 * 10 + i;
else if (n1 < n2) n1 = n1 * 10 + i;
}
System.out.printf("%d * %d = %d", n1 ,n2 ,n1 * n2);
}
}


------ Solution ------------------------------------ --------
first inscribed a little complicated

import java.util.Arrays;

/**
 * 6、有A、B、C、D和E共5所学校。在一次检查评比中,已知E肯定不是第2或第3名,他们互相进行推测,A校有人说,E校肯定是第1名;B校有人说,
 * 我校可能是第2名
 * ;C校有人说,A校最差;D校有人说,C校不是最好;E校有人说,D校会获得第一名。结果只有第一名和第二名的学校的人猜对了。编程指出这5所学校的名词。
 * 
 */
public class Test016 {

public static void main(String[] args) {
int[][] arr = getArr();
ranking(arr);
}

static int[][] getArr() {
int[][] arr = new int[5][5];
boolean[] booleanArr;
// 探测法,每次认为2个人说真话
for (int i = 0; i < 5; ++i) {
booleanArr = new boolean[5];
booleanArr[i] = true;
for (int j = i + 1; j < 5; ++j) {
booleanArr[j] = true;
if (check(booleanArr, arr)) {
//因为本题只会有唯一答案,所以找到就可以返回了
return arr;
}
booleanArr[j] = false;
}
}
return arr;
}

static boolean check(boolean[] booleanArr, int[][] arr) {
//判断条件
arr[0][4] = booleanArr[0] ? 1 : 0;
arr[1][1] = booleanArr[1] ? 1 : 0;
arr[4][0] = booleanArr[2] ? 1 : 0;
arr[0][2] = booleanArr[3] ? 0 : 1;
arr[0][3] = booleanArr[4] ? 1 : 0;
boolean result = true;
for (int i = 0; i < 2; ++i) {
int count = 0;
for (int j = 0; j < arr.length; ++j) {
//每个名次只有一个学校,并且前2名学校的人说的话与其学校名次匹配才为真
if (arr[i][j] == 1) {
result &= booleanArr[j];
++count;
}
}
result &= (count == 1);
}
return result;
}

static void ranking(int[][] arr) {
char[] rank = new char[5];
//根据题意,在这里可以获取A,B,C的名次
for (int i = 0; i < arr.length; ++i) {
for (int j = 0; j < arr.length; ++j) {
if (arr[i][j] == 1) {
rank[i] = (char) ('A' + j);

}
}
//获取E的名次
if(rank[0] != '\0' && rank[4] != '\0') {
rank[3] = 'E';
} else if(rank[0] != '\0' && rank[3] != '\0') {
rank[4] = 'E';
}
else if(rank[3] != '\0' && rank[4] != '\0') {
rank[0] = 'E';
}
//剩下的就是D的名次
for(int i = 0;i < rank.length;++i) {
if(rank[i] == '\0') {
rank[i] = 'D';
}
}
System.out.println(Arrays.toString(rank));
}
}


------ For reference only ----------------------------------- ----
first question

#include "stdio.h"
void main()
{ int a,b,c,d,e,i,m,b0,b1;
  int x[6];
  for(i=1;i<6;i++)x[i]=1;
  for(a=1;a<=5;a++) //A校名次由1测到5
  { x[a]=0;
    for(b=1;b<=5;b++) //B校名次由1测到5
      if(x[b])
      { x[b]=0;         
        for(c=1;c<=5;c++) //C校名次由1测到5
          if(x[c])
          { x[c]=0;
            for(d=1;d<=5;d++) //D校名次由1测到5
              if(x[d])
              { x[d]=0;
                e = 15-a-b-c-d;
               for(e=1;e<=5;e++) //e校名次由1测到5
    if(x[e])
    {
    {
   b0=(e!=2)&&(e!=3);
   m=(e==1)+(b==2)+(a==5)+(c!=1)+(d==1);
   b1=(e==1)&&(a!=2);                            
         b1=b1+((a==5)&&(c!=1)&&(c!=2));                  
       b1=b1+((c!=1)&&(d!=1)&&(d!=2));                  
        b1=b1+((d==1)&&(e!=2));
    }
    if(b0==1 && m==2 && b1==0)
    printf("A:%d B:%d C:%d D:%d E:%d/n",a,b,c,d,e); 
    }                                    
                x[d]=1; 
              }
              x[c]=1;                     
          }
          x[b]=1; 
      }    
      x[a]=1;   
   } 
}

can Baidu to these problems, do not ask . . .
------ For reference only -------------------------------------- -
brother , I want java version
------ For reference only --------------------------- ------------
2 questions, three questions I simply do a bit , such a high degree of complexity . .

sorry posted out , mark, waiting optimal solution
------ For reference only ----------------------- ----------------
this and JAVA relationship with P .
------ For reference only -------------------------------------- -
second question to give you an idea , the first product of the maximum , you have to ensure that the two numbers as large as possible , how to make it as large as two numbers ? Is to allow a larger number of rows in the highest median position. . . So you have a lot of programs exclude unnecessary judgments and calculations. . . The next step is traversed . . .
------ For reference only -------------------------------------- -
question 3 stupid way .
public static void main(String[] args) {
int num2;
int num3;
for (int num1 = 123; num1 < 333; num1++) {
num2 = num1 * 2;
num3 = num1 * 3;
char[] str = (String.valueOf(num1) + String.valueOf(num2) + String
.valueOf(num3)).toCharArray();
if (checkDup(str)) {
System.out.println("第一个" + num1 + "    第二个" + num2 + "   第三个"
+ num3);
}
}
}

static boolean checkDup(char[] str) {
for (int i = 0; i < str.length - 1; i++) {
for (int j = i; j < str.length - 1; j++) {
if (str[i] == str[j + 1] || str[j + 1] == '0') {
return false;
}
}
}
return true;
}

------ For reference only ----------------------------------- ----
answer to the second question : is too much trouble , seeking optimization
public static void main (String [] args)
{
/ / known data 5,6,7,8,9
int [] num = {5,6,7,8,9};
/ / define the second number
int num1 = 0;
int num2 = 0;
/ / analysis shows that Italy 2-digit and 3-digit area of ​​maximum

/ / Select the first number :
/ / 0-4
int max = 0;
for (int i = 0; i {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
a = num [i];
for (int j = 0; j {
if (j! = i)
{
b = num [j];
}
else
{
continue;
}
for (int k = 0; k {
if (k! = i && k! = j)
{
c = num [k];
}
else
{
continue;
}
for (int l = 0; l {
if (l! = i && l! = j && l! = k)
{
d = num [l];
}
else
{
continue;
}

for (int m = 0; m {
if (m! = i && m! = j && m! = k && m! = l)
{
e = num [m];
String temp1 = a + "" + b;
String temp2 = c + "" + d + "" + e;
int mc = Integer.parseInt (temp1) * Integer.parseInt (temp2);
if (mc> max)
{
max = mc;
num1 = Integer.parseInt (temp1);
num2 = Integer.parseInt (temp2);
}
}
else
{
continue;
}
}
}
}
}
}
System.out.print (num1 + "\ t" + num2);
}

------ For reference only ---------------------------------- -----

------ For reference only ---------------------------------------

--- --- For reference only ---------------------------------------


2 , the judge wrote so complicated conditions
optimize what

/**
 * 7、有数字5、6、7、8、9,将五个数字任意组合成两个数字,比如57和689,每个数字不能重复使用,将组成的两个数字相乘,会得到一个乘积。
 * 请编程计算组成的哪两个数字的乘积最大。
 * 
 * 
 */
public class Test013 {
public static void main(String[] args) {
findMax();
}

static void findMax() {
int[] arr = new int[] { 9, 8, 7, 6, 5 };
int n1 = 0,n2 = 0;
for (int i : arr) {
if (n1 > n2) n2 = n2 * 10 + i;
else n1 = n1 * 10 + i;
}
System.out.printf("%d * %d = %d", n1 ,n2 ,n1 * n2);
}
}


------ For reference only ----------------------------------- ----
public static void main (String [] args) {
int [] num = {5, 6, 7, 8, 9};
int length = num.length;
int firstnum = 0;
int secondnum = 0;
int result = 0;
int finalresult = 0;
Map map = new HashMap ();
for (int i = 0; i <5; i + +) {
firstnum = num [i];
secondnum = 0;
for (int j = 4; j> = 0; j -) {
if (num [j] == firstnum) {
continue;
} else {
secondnum = secondnum * 10 + num [j];
result = firstnum * secondnum;
String s = "" + firstnum + "*" + secondnum;
map.put (result, s);
}
}
if (finalresult finalresult = result;
System.out.println (firstnum + "*" + secondnum + "=" + result);
}

}

for (int a = 0; a for (int b = 0; b if (a firstnum = num [b] * 10 + num [a];
} else if (a> b) {
firstnum = num [a] * 10 + num [b];
} else {
continue;
}
secondnum = 0;
for (int c = 4; c> = 0; c -) {
if (num [c] == num [a] | | num [c] == num [b]) {
continue;
} else {
secondnum = secondnum * 10 + num [c];
}
}
result = firstnum * secondnum;
String s = "" + firstnum + "*" + secondnum;
map.put (result, s);
if (finalresult finalresult = result;
}

}
}
System.out.println (" The end result :" + map.get (finalresult) + "="
+ finalresult);
}
------ For reference only --------------------------------- ------
second question
public static void main (String [] args) {
int [] num = {5, 6, 7, 8, 9};
int length = num.length;
int firstnum = 0;
int secondnum = 0;
int result = 0;
int finalresult = 0;
Map map = new HashMap ();
for (int i = 0; i <5; i + +) {
firstnum = num [i];
secondnum = 0;
for (int j = 4; j> = 0; j -) {
if (num [j] == firstnum) {
continue;
} else {
secondnum = secondnum * 10 + num [j];
result = firstnum * secondnum;
String s = "" + firstnum + "*" + secondnum;
map.put (result, s);
}
}
if (finalresult finalresult = result;
System.out.println (firstnum + "*" + secondnum + "=" + result);
}

}

for (int a = 0; a for (int b = 0; b if (a firstnum = num [b] * 10 + num [a];
} else if (a> b) {
firstnum = num [a] * 10 + num [b];
} else {
continue;
}
secondnum = 0;
for (int c = 4; c> = 0; c -) {
if (num [c] == num [a] | | num [c] == num [b]) {
continue;
} else {
secondnum = secondnum * 10 + num [c];
}
}
result = firstnum * secondnum;
String s = "" + firstnum + "*" + secondnum;
map.put (result, s);
if (finalresult finalresult = result;
}

}
}
System.out.println (" The end result :" + map.get (finalresult) + "="
+ finalresult);
}
------ For reference only --------------------------------- ------
are big God, ha ha ha

------ For reference only ---------------------------------- -----
ask the first question of the java solution !
------ For reference only -------------------------------------- -
mark, slowly thinking of ~
------ For reference only --------------------------- ------------
public static boolean isTrue(char oneBody,char a[])
{
//oneBody 为 那五个人(A B C D E)
// 已知E肯定不是第2或第3名,
// E!=2&&E!=3
// 五层循环,且各元素都不相同时,验证五个人说的话
//A校有人说,E校肯定是第1名; e=1;(a!=1)
//B校有人说,我校可能是第2名;b=2;(b!=1)
//C校有人说,A校最差;  a=5;
//D校有人说,C校不是最好;c!=1
//E校有人说,D校会获得第一名。 d=1;
boolean res ;
//两个正确,三个错误
//下面是五个人的猜测
switch(oneBody)
{
case 'A':
res = ( a[0]== 'E');
break;
case 'B':
res = (a[1] == 'B');
break;
case 'C':
res = (a[4] == 'A');
break;
case 'D':
res = (a[0] != 'C');
break;
case 'E':
res = (a[0] == 'D');
break;
default:
res = false;
}
return res;
}
public static void main(String[] args)
{

char[] a = {'A','B','C','D','E'}; //初始化


//a[0]代表第一名
//a[1]代表第二名
//a[2]代表第三名
//a[3]代表第四名
//a[4]代表第五名
//五层循环,且各元素都不相同时,验证五个人说的话
for(a[0] = 'A'; a[0] <= 'E'; ++ a[0] )
{
for(a[1] = 'A' ;  a[1] <= 'E'; ++ a[1] )
{
if(a[1] != a[0])
{
for(a[2] = 'A' ; a[2] <= 'E'; ++ a[2] )
{
 if((a[2]!= a[1] ) && (a[2] != a[0]))
 {
  for(a[3] = 'A' ; a[3] <= 'E'; ++ a[3] )
{
if((a[3] != a[2]) && (a[3] != a[1]) && (a[3] != a[0] ))
{
for(a[4] = 'A' ; a[4] <= 'E'; ++ a[4] )
{
if((a[4]!= a[3]) && (a[4] != a[2] ) && (a[4] != a[1]) && (a[4] != a[0]))
{
//只有第一第二为真,其它人为假,并且E不是第二也不是第三
if(isTrue(a[0],a) && isTrue(a[1],a)&&!isTrue(a[2], a)&&!isTrue(a[3], a)&&!isTrue(a[4], a)&&(a[1] != 'E' && a[2] != 'E'))
{
System.out.printf("排名为:%c %c %c %c %c\n ",a[0],a[1],a[2],a[3],a[4]);
}
}
}
}
}
 }
}
}

}
}

没有评论:

发表评论