このプログラムでisSetOf1toN のメソッドがしなくてはいけないことは、
整数のセットの中でセルの合計数と同じ数字があればtrue をないならfalse を返さなくてはいけないのですが、大学のパソコンでチェックするとこれが出ます。
「1、4、5、10、12」「2、5、8」「3、9、12」「7」
expected false but found true どうしてでしょうか?二つ12がありますが、どちらにしてもセルの数と同じなのでいいと思うのですが。
java
1package week04; 2 3/** 4 * Skeleton code for an array based implementation of Young's tableau. 5 * 6 * @author Iain Hewson 7 */ 8public class TableauApp { 9 10 /** 11 * The main method is just used for testing. 12 * 13 * @param args command line arguments are not used. 14 */ 15 public static void main(String[] args) { 16 final int [] [] valid = {{1,4,5,10,11},{2,6,8},{3,9,12},{7}}; 17 //System.out.println(columnValuesIncrease(valid)); 18 final int [] [] t = {{1,2,3,4,5},{6,7,8},{9}}; 19 System.out.println(columnValuesIncrease(t)); 20 System.out.println(isSetOf1toN(valid)); 21 22 23 } 24 25 /** 26 * Determines whether the array passed to it is a valid tableau or not. 27 * 28 * @param t a two-dimensional array to test for tableau-ness. 29 * 30 * @return true if the parameter is a valid tableau, otherwise false 31 */ 32 public static boolean isTableau(int[][] t){ 33 return false; 34 } 35 /** 36 * Returns true if no row is longer than a preceding row, 37 * Otherwise false. 38 * @param t a two-dimensional array which represents a tableau. 39 * @return true if no row is longer than a preceding row, 40 * otherwise false. 41 */ 42 public static boolean rowLengthsDecrease(int [] [] t){ 43 boolean result = true; 44 // check if the row is not empty 45 if (t[0].length != 0){ 46 System.out.println("The two dimentional array is this"); 47 System.out.println(toString(t)); 48 int first = t[0].length;// the first arrays element. 49 int second = 0;// second longest or equal to the first element. 50 for (int i =1; i<t.length;i++){ 51 // t.length = 4 52 second = t[i].length;// 0 changed to 4 53 if(result){// if the result is still true 54 if(first >= second ){// if first is longer or same as second 55 first = second;// change first value to second value(second row length) 56 result = true; 57 }else{ 58 System.out.println("No row is longer than a preceding row"); 59 result = false; 60 } 61 }// second if 62 }// for 63 }else{ 64 result = false; 65 }// first if 66 return result; 67 }// method 68 /** 69 *returns true if no row is longer than a preceding row, otherwise false. 70 *@param t two dimentional array 71 *@return true if the integers are increasing from left to right. 72 */ 73 public static boolean rowValuesIncrease(int [] [] t){ 74 75 boolean result = true; 76 for (int i =0; i<t.length; i++){ 77 int first = t[i][0];// first elements for each array 78 int second =0; 79 for (int j = 1; j<t[i].length;j++){ 80 81 82 if (t[i][j] > first){ 83 second = t[i][j]; 84 System.out.println("second int is "+" " + second); 85 System.out.println("First int is "+" " + first); 86 System.out.println(""); 87 result = true; 88 first = second; 89 System.out.println("Current result " + "" + result); 90 }else{ 91 result = false; 92 return false; 93 }// if end 94 95 }// end for loop 96 }// end for loop 97 return result; 98 99 100 }// end method 101 102 /** 103 * Returns true if from top to bottom in any column, 104 * the integers are increasing otherwise false. 105 * @param t two dimentional array 106 * @return true if the integers are increasing 107 * 108 */ 109 public static boolean columnValuesIncrease(int [][] t){ 110 int firststep=0; 111 int temp = 0; 112 for (int i = 0; i<t[0].length; i++){ 113 for (int j = 0; j<t.length; j++){ 114 if (j == 0){ 115 116 firststep = t[j][i]; 117 continue; 118 119 } 120 try{ 121 temp = t[j][i]; 122 }catch(Exception e){ 123 continue; 124 } 125 if (firststep > temp ){ 126 return false; 127 } 128 firststep = temp; 129 } 130 131 } 132 return true; 133 134 } 135 /** 136 * Returns true if the set of integers used is {1,2...n} where 137 * n is the number of cells, otherwise false. 138 * @param t two dimentional array 139 * @return true 140 */ 141 public static boolean isSetOf1toN (int [] [] t){ 142 boolean result = true; 143 int count = 0;// 10 144 // get total number of cells 145 for (int i = 0; i<t.length; i++){ 146 count += t[i].length; 147 148 } 149 for (int i = 0; i<t.length; i++){ 150 for (int j = 0; j<t[i].length; j++){ 151 if (t[i][j] == count ){ 152 result = true; 153 return result; 154 155 }else{ 156 result = false; 157 158 } 159 160 } 161 162 } 163 164 return result; 165 166 167 } 168 /** 169 * Returns a string representation of an array based tableau. 170 * 171 * @param t a two-dimensional array which represents a tableau. 172 * 173 * @return a string representation of an array based tableau. 174 */ 175 public static String toString(int[][] t) { 176 StringBuilder result = new StringBuilder(); 177 for (int i = 0; i < t.length; i++) { 178 for (int j = 0; j < t[i].length; j++) { 179 result.append(String.format("%-4s", t[i][j])); 180 } 181 if (i < t.length-1) { 182 result.append("\n"); 183 } 184 } 185 return result.toString(); 186 } 187 188} 189
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/26 04:08
2017/03/26 04:50
2017/03/26 04:51