문제 : https://www.acmicpc.net/problem/6679


[알고리즘풀이]

1000 ~ 9999까지 10, 12, 16진수로 표현하면서 자릿수를 다 더해주고 같다면 출력, 아니라면 출력 X

#include<iostream>

using namespace std;

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int a, b, c, j ;
	for (int i = 1000; i <= 9999; i++) {		
		a = 0, b = 0, c = 0;
		j = i;
		while (j != 0) {
			a += j % 10;
			j /= 10;
		}
		j = i;
		while (j != 0) {
			b += j % 12;
			j /= 12;
		}
		j = i;
		while (j != 0) {
			c += j % 16;
			j /= 16;
		}
		if (a == b && b == c)
			cout << i << '\n';
	}
	return 0;
}

 

+ Recent posts