0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第6回オフラインリアルタイムどう書くの問題 Javaで回答

Last updated at Posted at 2014-03-16

挑戦!


package how;

public class JudgeZokugara {

	/** 続柄:本人 */
	private static final String HONNIN =  "me";
	/** 続柄:母 */
	private static final String HAHA =  "mo";
	/** 続柄:娘 */
	private static final String MUSUME =  "da";
	/** 続柄:姉妹 */
	private static final String SIMAI =  "si";
	/** 続柄:おば */
	private static final String OBA =  "au";
	/** 続柄:姪 */
	private static final String MEI =  "ni";
	/** 続柄:いとこ */
	private static final String ITOKO =  "co";
	/** 続柄:その他 */
	private static final String OTHER =  "-";

	/**
	 * 判定メソッド
	 * @param relation 関係
	 * @return 続柄
	 */
	private static String judge(String relation) {

		// relation(a->b)を"a"と"b"に分解
		int from = Integer.valueOf(relation.split("->")[0]);
		int to = Integer.valueOf(relation.split("->")[1]);
		// fromとtoが同値であれば本人
		if (from == to) {
			return HONNIN;
		}

		// 世代判定
		// fromの世代判定
		int fromGene = isGeneration(from);
		// toの世代判定
		int toGene = isGeneration(to);
		if (fromGene == toGene) {
			// 世代が同じ
			if(getParentNo(from) == getParentNo(to)) {
				// 親が同じ => 姉妹
				return SIMAI;
			} else {
				// 親が違う
				if (getParentNo(getParentNo(from)) == getParentNo(getParentNo((to)))) {
					// 祖母が同じ => 従姉妹
					return ITOKO;
				} else {
					return OTHER;
				}
			}
		} else if (fromGene > toGene) {
			// toがfromより世代が1つ上 => 母 or おば
			// それ以外 => その他の続柄
			if (fromGene-1 == toGene) {
				if (to == getParentNo(from)) {
					// fromの親番号がtoと同じ => 母
					return HAHA;
				} else {
					if (getParentNo(to) == getParentNo(getParentNo(from))) {
						// fromの祖母がtoの母と同じ => おば
						return OBA;
					}
				}
				// fromの祖母がtoの母と違う => その他
				return OTHER;
			}
			return OTHER;
		} else {
			// fromがtoより世代が1つ上 => 娘 or 姪
			// それ以外 => その他の続柄
			if (fromGene+1 == toGene) {
				if (from == getParentNo(to)) {
					// toの親番号がfromと同じ => 娘
					return MUSUME;
				} else {
					if (getParentNo(from) == getParentNo(getParentNo(to))) {
						// toの祖母がfromの母と同じ => 姪
						return MEI;
					}
				}
				// toの祖母がfromの母と違う => その他
				return OTHER;
			}
			return OTHER;
		}
	}

	/**
	 * 世代判定
	 * @param num
	 * @return 世代番号(第1世代:0,第2世代:1)
	 */
	private static int isGeneration(int num) {
		int gene = 0;
		int start = 1;
		int end = 1;
		if (1 < num) {
			gene = 1;
			while (gene<=3) {
				start = start + (int)Math.pow(3,gene-1);
				end = end + (int)Math.pow(3,gene);
				if (start <= num
						&& end >= num) {
					break;
				}
				gene++;
			}
		}
		return gene;
	}

	/**
	 * 親の番号を返却する。
	 * @param no
	 * @return
	 */
	private static int getParentNo(int no) {
		int parentNo = 1;

		if (0==no%3) {
			parentNo = no/3;
		} else if ((no-1)%3==0) {
			parentNo = (no-1)/3;
		} else if ((no+1)%3==0) {
			parentNo = (no+1)/3;
		}


		return parentNo;
	}

	/**
	 * テスト実行メソッド
	 * @param relation 関係
	 * @param exp 結果の期待値
	 */
	private static void test(String relation, String exp) {
		// 判定
		if (exp.equals(judge(relation))) {
			System.out.println(relation + " : " + exp + " => OK" );
		} else {
			System.out.println(relation + " : " + exp + " => NG" );
		}
	}

	/**
	 * 実行メソッド
	 * @param args
	 */
	public static void main(String[] args) {
		/*#0*/ test( "5->2", "mo" );
		/*#1*/ test( "28->10", "au" );
		/*#2*/ test( "1->1", "me" );
		/*#3*/ test( "40->40", "me" );
		/*#4*/ test( "27->27", "me" );
		/*#5*/ test( "7->2", "mo" );
		/*#6*/ test( "40->13", "mo" );
		/*#7*/ test( "9->3", "mo" );
		/*#8*/ test( "4->1", "mo" );
		/*#9*/ test( "1->3", "da" );
		/*#10*/ test( "12->35", "da" );
		/*#11*/ test( "3->8", "da" );
		/*#12*/ test( "6->19", "da" );
		/*#13*/ test( "38->40", "si" );
		/*#14*/ test( "9->8", "si" );
		/*#15*/ test( "4->2", "si" );
		/*#16*/ test( "15->16", "si" );
		/*#17*/ test( "40->12", "au" );
		/*#18*/ test( "10->4", "au" );
		/*#19*/ test( "21->5", "au" );
		/*#20*/ test( "8->2", "au" );
		/*#21*/ test( "3->5", "ni" );
		/*#22*/ test( "11->39", "ni" );
		/*#23*/ test( "2->13", "ni" );
		/*#24*/ test( "13->32", "ni" );
		/*#25*/ test( "14->22", "co" );
		/*#26*/ test( "40->34", "co" );
		/*#27*/ test( "5->8", "co" );
		/*#28*/ test( "12->10", "co" );
		/*#29*/ test( "1->27", "-" );
		/*#30*/ test( "8->1", "-" );
		/*#31*/ test( "12->22", "-" );
		/*#32*/ test( "2->40", "-" );
		/*#33*/ test( "32->31", "-" );
		/*#34*/ test( "13->14", "-" );
	}
}

多分、無駄がいっぱいある・・・

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?