[JAVA] ABC --019 --A & B & C

AtCoder ABC 019 A&B&C AtCoder - 019

I realized that it would be difficult for me to find the problem name without the problem name, so let's fill in the problem name from now on.

** 2019/05/17: Fixed A and B code **

A --Takahashi-kun and age

--Code corrected because you pointed out in the comment that sort can be processed together with stream --Can you sort at the same time? .. .. There are too many things I can do and I can't keep up with my understanding.

--Since it is the median, sort it in the middle

	private void solveA() {
		int[] wk = IntStream.range(0, 3).map(i -> nextInt()).sorted().toArray();

		out.println(wk[1]);
	}

old

private void solveA() {

	int[] wk = IntStream.range(0, 3).map(i -> nextInt()).toArray();

	Arrays.sort(wk);

	out.println(wk[1]);
}

B --Takahashi-kun and character string compression

-** 2019/05/19 postscript **: There is a code of @swordone in the comment column. That is simpler.

--Append by counting how many consecutive same strings are --Cnt is 0 after exiting the loop = The last 1 character is different from the last -1 character, so count --Cnt is not 0 after exiting the loop = ++ because the last is the same character

	private void solveB() {
		char[] wk = next().toCharArray();
		StringBuilder builder = new StringBuilder();
		int cnt = 0;
		for (int i = 0; i < wk.length - 1; i++) {
			if (cnt == 0) {
				builder.append(wk[i]);
				cnt++;
			}
			if (wk[i] != wk[i + 1]) {
				builder.append(cnt);
				cnt = 0;
			} else {
				cnt++;
			}
		}
		if (cnt != 0) {
			builder.append(cnt);
		} else {
			builder.append(wk[wk.length - 1]);
			builder.append(1);
		}

		out.println(builder.toString());

	}

C --Takahashi-kun and the magic box: using set and list

-The same integer $ whether you put $ x or 2x -$ x, 2x, 4x, 8x ... $ values are counted as 1. --If you put the same value in both set and list, loop based on list and remove the value from set.

	private void solveC() {
		Set<Long> wk = new HashSet<Long>();
		List<Long> base = new ArrayList<Long>();
		int n = nextInt();
		for (int i = 0; i < n; i++) {
			long val = nextLong();
			wk.add(val);
			base.add(val);
		}

		Collections.sort(base);
		long max = base.get(base.size() - 1);
		long res = 0;
		for (Long longVal : base) {
			if (wk.contains(longVal)) {
				wk.remove(longVal);
				res += recursiveC(wk, max, longVal);
			}
			if (wk.size() == 0) {
				break;
			}
		}

		out.println(res);
	}

	private int recursiveC(Set<Long> wk, long max, long current) {
		if (current > max || wk.size() == 0) {
			return 1;
		}

		current <<= 1;
		wk.remove(current);
		return recursiveC(wk, max, current);
	}

C --Takahashi-kun and the Magic Box: Use only set

――After AC in a loop, I got "Is this just counting an odd number?", So I tried AC -The number that doubles $ 1 becomes 1 at the end if divided by 2. $ -$ If you double the odd number, be sure to double the even number $

	private void solveC2() {
		Set<Integer> wk = new HashSet<Integer>();
		int n = nextInt();
		for (int i = 0; i < n; i++) {
			int val = nextInt();
			while ((val & 1) != 1) {
				val /= 2;
			}
			wk.add(val);
		}

		out.println(wk.size());
	}

Recommended Posts

ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
ABC --015 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --018 --A & B & C
ABC --054 --A & B & C
ABC --029- A & B & C
ABC --022 --A & B & C
ABC --019 --A & B & C
ABC --020 --A & B & C
ABC --030- A & B & C
ABC --127 --A & B & C
ABC --007 --A & B & C
ABC --132- A & B & C
ABC --026 --A & B & C
ABC --014- A & B & C
ABC --016 --A & B & C
ABC --011-A & B & C
ABC --031 --A & B & C
ABC --021 --A & B & C
ABC --025 --A & B & C
ABC --024 --A & B & C
ABC --027 --A & B & C
ABC --080- A & B & C
ABC --133- A & B & C & D
ABC --122 --A & B & C & D
ABC --125- A & B & C & D
ABC --130- A & B & C & D
ABC --126 --A & B & C & D
ABC --134- A & B & C & D & E
ABC --131- A & B & C & D & E
diverta 2019 Programming Contest A & B & C & D
atcoder ABC113 C problem
ABC093 C --Same Integers
atcoder ABC115 C problem
AtCoder Beginner Contest 170 A, B, C up to ruby
A person writing C ++ tried writing Java
Make a SOAP call in C #
Call a C function from Swift
NLP4J [006-034] 100 language processing knocks with NLP4J # 34 "A B"