백준 2331 c++

magicdrill·2024년 7월 15일

백준 문제풀이

목록 보기
393/673

백준 2331 c++

다시 DFS를 시작해야겠다.

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> visited(250000, 0);

void input_num(int* A, int* P)
{
	cin >> *A >> *P;

	return;
}

void DFS(int A, int P)
{
	int next = 0;

	visited[A]++;
	if (visited[A] == 3)
	{
		return;
	}
	while (A) {
		next += pow(A % 10, P);
		A /= 10;
	}

	DFS(next, P);
}

void find_answer(int A, int P)
{
	/*vector<int> D;
	int current, next, i = 0, j;
	
	D.push_back(A);
	while (1)
	{
		current = D[i];
		next = pow(current / 1000, P) 
			+ pow(current % 1000 / 100, P) 
			+ pow(current % 100 / 10, P)
			+ pow(current % 10, P);
		cout << i << " " << current << " " << next << "\n";
		D.push_back(next);
		i++;
		for (j = 0; j < D.size() - 1; j++)
		{
			if (D[j] == next)
			{
				cout << j << "\n";

				return;
			}
		}
	}*/
	int count = 0;
	int i;

	DFS(A, P);
	for (i = 1; i < visited.size(); i++) {
		if (visited[i] == 1)
			count++;
	}
	cout << count << "\n";

	return;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int A, P;

	input_num(&A, &P);
	find_answer(A, P);
	
	return 0;
}

0개의 댓글