기본 콘텐츠로 건너뛰기

9375 : 패션왕 신해빈 [C++]

이항계수 문제들을 풀다 보니 nCk에 익숙해져서 이 문제도 그렇게 풀려고 했다.

5C2 + 3C1 + 2C1 + ... 요런식으로 하다보니까 빠르게 틀리고 나서 다시 생각해봤는데

생각보다 이지했다.

clothes 3종류, pants 2종류, headgear 3종류라고 하면 

구하는 공식은 

 = {clothes(3) + 안입음(1)} * {pants(2) + 안입음(1)} * {headgear(3) + 안입음(1)}
 - 아무것도 안입음(1)

이렇게 되겠다. 채점중 50%에서 계속 틀렸다고 나와서 20분동안 헤매다가

cout << F.GetAnswer() <<endl 줄바꿈을 안해줬었다.

당연히 테스트케이스 문제에서는 써줬어야 하는데 실수다

다음부턴 헤매지 말자!

FashionKing::FashionKing(int n)
{
 this->Fashion.push_back("null");
 this->NumberofSort.push_back(0);

 for (int i = 0; i < n; i++)
 {
  string Name;
  string Wear;
  cin >> Name >> Wear;
  for (int j = 0; j < this->Fashion.size(); j++)
  {
   if (this->Fashion[j] == Wear)
   {
    this->NumberofSort[j]++;
    break;
   }
   if (j == this->Fashion.size() - 1)
   {
    this->Fashion.push_back(Wear);
    this->NumberofSort.push_back(1);
    break;
   }
  }
 }
}

long long FashionKing::GetAnswer()
{
 long long Sum = 1;

 for (int i = 1; i < this->NumberofSort.size(); i++)
  Sum *= (long long)(this->NumberofSort[i] + 1);

 return (Sum - 1);
}
<소스 코드>

*Source of the problem = https://www.acmicpc.net/problem/9375
*문제 출처 : BAEKJOON ONLINE JUDGE

댓글

이 블로그의 인기 게시물

11004 : K번째 수 [C++]

# include < iostream > # include < cstdio > # include < algorithm > int main ( ) { int * Number = new int [ 5000000 ] ; int N , K ; scanf ( " %d %d " , & N , & K ) ; for ( int i = 0 ; i < N ; i + + ) scanf ( " %d " , Number [ i ] ) ; std :: sort ( Number , Number + N ) ; printf ( " %d " , Number [ K - 1 ] ) ; return 0 ; }

1149 : RGB Street Coloring (Dynamic Programming) [C,C++]

The key to this problem lies in understanding the principles. Let me explain the algorithm to solve the problem by using DP. First, you need the same storage space like input data's size. When you draw any color of the nth house, the space will contain the minimum value. If you paint the red in the second house, this value is sum of blue or green of the first house.  You must use DP because you must use the previous value.  Of course, you can also use the recursive algorithm to solve it. But if it gets bigger, it will take a lot of time.  If you paint the red in the nth house in the same way, you should add the lower value of the blue and green of the n-1th house.  Therefore, the minimum value can be found in the value of the storage space (n-1) index. <pesudo code> *Source of the problem =  https://www.acmicpc.net/problem/1149 *문제 출처 : BAEKJOON ONLINE JUDGE

11478 : 서로 다른 부분 문자열의 개수 (미제) [C++]

# include < iostream > # include < vector > # include < string > using namespace std ; int main ( ) { string sInput ; getline ( cin , sInput , '\n' ) ; int Time = 1 ; int Count = 0 ; vector < string > Storage ; for ( int i = 0 ; i < sInput . size ( ) ; i + + ) { for ( int j = 0 ; ( j + Time - 1 ) < sInput . size ( ) ; j + + ) Storage . push_back ( sInput . substr ( j , Time ) ) ; Time + + ; } bool * Visited = new bool [ Storage . size ( ) * sizeof ( bool ) ] ; for ( int i = 0 ; i < Storage . size ( ) ; i + + ) { Visited [ i ] = true ; for ( int j = 0 ; j < Storage . size ( ) ; j + + ) { if ( i ! = j & & Storage [ i ] = = Storage [ j ] ) { Visited [ i ] = false ; Visited [ j ] = true ; break ; } } } for ( int i = 0 ; i < Storage . size ( ) ; i + + ) if ( Visited [ i ] ) Count ...