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.

ABC - 101- A&B&C

Posted at

AtCoder ABC 101 A&B&C

AtCoder - 101

A問題

  • 文字列読み込んで1文字ずつ $\pm$ を判定する
	private void solveA() {
		Scanner scanner = null;
		String[] stringS = new String[4];

		try {
			scanner = new Scanner(System.in);
			stringS = scanner.next().split("");

			int res = 0;
			for (int i = 0; i < stringS.length; i++) {
				if (stringS[i].equals("+")) {
					res++;
				} else if (stringS[i].equals("-")) {
					res--;
				}
			}

			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

B問題

  • 数値を分解して桁を足して割る
	private void solveB() {
		Scanner scanner = null;
		int numN = 0;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();

			int wkN = 0;
			int tmp = numN;
			while (tmp != 0) {
				wkN += tmp % 10;
				tmp /= 10;
			}

			System.out.println(numN % wkN == 0 ? "Yes" : "No");

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

C問題:問題文通りに実装

  • 1回目は3種類の数値を一斉に変えられるけど、2回目以降は変えるための数値をKの中に含めないといけないので、K-1かまでしか変更できない
	private void solveC() {
		Scanner scanner = null;
		int numN = 0;
		int numK = 0;
		int[] wk;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			numK = scanner.nextInt();
			wk = new int[numN];

			for (int i = 0; i < wk.length; i++) {
				wk[i] = scanner.nextInt();
			}

			Arrays.sort(wk);

			int res = 0;
			int wkN = numN;
			while (wkN > 0) {
				if (res == 0) {
					wkN = wkN - numK;
					res++;
				} else {
					wkN = wkN - (numK - 1);
					res++;
				}
			}
			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

C問題:計算量削減

  • 数列NはKよりも大きい
    • 数列NがKで割り切れるのであれば、操作回数は $1+(N-K)/(K-1)$
    • 割り切れないのであれば、操作回数は $1+(N-K)/(K-1)+1$
  • 最初の1は、(N-K)をする回数
	private void solveC2() {
		Scanner scanner = null;
		int numN = 0;
		int numK = 0;
		int[] wk;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			numK = scanner.nextInt();
			wk = new int[numN];

			int wkN = numN - numK;
			int wkK = numK - 1;
			int res = 1 + (wkN % wkK == 0 ? (wkN / wkK) : (wkN / wkK) + 1);
			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}
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?