質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1922閲覧

ヤング図形のプログラム

TomofumiKimura

総合スコア65

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/03/26 03:47

編集2017/03/26 04:42

このプログラムで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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

整数のセット、つまり整数の集合で議論しなくてはいけません。
問題の配列は1~12の整数が1つずつ表れており、その最大数は12、
セルの数も12で、条件に合致します。

集合で論じる、つまり追加分は1~12の整数がすべて使われていない(11が抜けてる)のでfalseと出さなければならないところを、最大数12だけで見て判定しているのでそうなります。

投稿2017/03/26 04:02

編集2017/03/26 04:47
swordone

総合スコア20649

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TomofumiKimura

2017/03/26 04:08

おーーすごいですね、頭いいですねーー。すみません。自分は頭悪いので。大学から渡された英語の奴には isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n} where n is the number of cells, otherwise false.ってあったので、一番最後の整数がそうだと思ってしまいました。ありがとうございます!!
Zuishin

2017/03/26 04:50

質問もソースも変わっていますが、当初の問題が解決したならベストアンサーでしょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問