#include <iostream> #include <vector> using namespace std; class Cycle { private: vector<int> Relation; int Count = 0; public: Cycle(int n); void DFS(int target, int destination); int GetNumber(int index); void Print(); }; Cycle::Cycle(int n) { for (int i = 0; i < n; i++) { int Temp; cin >> Temp; this->Relation.push_back(Temp); } } void Cycle::DFS(int target, int destination) { if (target < 0) return; int t_temp = this->Relation[target] - 1; this->Relation[target] = -1; if (t_temp == destination) this->Count += 1; else DFS(t_temp, destination); } int Cycle::GetNumber(int index) { return this->Relation[index]; } void Cycle::Print() { cout << this->Count << endl; } int main() { int iTestCase; cin >> iTestCase; while (iTestCase--) { int N; cin >> N; Cycle C(N); for (int j = 0; j < N; j++) C.DFS(C.GetNumber(j) - 1, j); C.Print(); } }
# include < iostream > # include < vector > using namespace std ; int main ( ) { vector < int > Stick ; Stick . push_back ( 64 ) ; int Target ; cin > > Target ; int Last = 0 ; while ( true ) { if ( Target = = 64 ) break ; int Sum = 0 ; for ( int i = 0 ; i < Stick . size ( ) ; i + + ) Sum + = Stick [ i ] ; if ( Target = = Sum ) break ; else if ( Target < Sum ) { Stick [ Last ] / = 2 ; Sum = 0 ; for ( int i = 0 ; i < Stick . size ( ) ; i + + ) Sum + = Stick [ i ] ; if ( Target < = Sum ) continue ; else Stick . push_back ( Stick [ Last + + ] ) ; } } cout < < Stick . size ( ) ; ...
댓글
댓글 쓰기