9
9

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.

会社の組織と給与

Posted at

 私は昨年からプログラミングを始めたのですが、振り返れば悪習慣の多さに気付かれます。プログラミングをする際に、だらだら解いている事が少なくありませんでした。気を改めて、習慣を見直す事で改善しようと思ってます!最近はあっちこっち行って、深さ優先探索や幅優先探索を練習していました!この問題は、再帰を使っているのですが、動的計画法の様に解く事が出来ませんでした。
ですが、少し形を変えれば出来るようです。

package salary2;

public class Main {
	String[] relate;
	
	int Salary(String relation) {
		int ret = 0;
		for(int r = 0; r < relation.length(); r++) {
			if(relation.charAt(r) == 'Y') {
				ret += Salary(relate[r]);
			}
		}
		if(ret == 0) {
			ret = 1;
		}
		return(ret);
	}
	
	long totalSalary(String[] relations) {
		relate = relations;
		long sum = 0;
		for(int r = 0; r < relate.length; r++) {
			sum += Salary(relations[r]);
			System.out.println(sum);
		}
		return(sum);
	}
	
	void doIt() {
		String[] relations = {
			"NYNNYN",
			"NNNNNN",
			"NNNNNN",
			"NNYNNN",
			"NNNNNN",
			"NNNYYN",
		};
		
		System.out.println(totalSalary(relations));
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main().doIt();
	}

}


今回もよろしければ、間違いや改善案等を教えてくださると嬉しいです!m(_ _)m

9
9
2

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
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?