Problem Solving

[Topcoder] SRM 144 ~ 150 연습

끄적끄적 2008. 8. 29. 14:25

Topcode연습할겸 예전 easy문제부터 풀어보기로 했다. 오늘은 144부터 150까지 풀어봄

SRM 144 DIV2
  : 문제는 간단하고, C++에서 int 를 string으로 할당하는 부분만 주의

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <cmath>
using namespace std;

class Time
{
public:

 string whatTime(int seconds)
 {
  int s = seconds % 60;
  int total_min = seconds / 60;
  int m = total_min % 60;
  int h = total_min / 60;

  char buf[10];
  sprintf( buf, "%d:%d:%d", s, m, h);
  string ret(buf);  
  return ret;
 }
};

* SRM 145 DIV2 easy

문제 해석하는게 좀 오래걸렸다; 예제 2)가 다음과 같은데, 샘플의 string 이 X로 끝나는걸 못보고, Z까지 있는 줄 알고 왜 156이 아니라 150일까 하고 한참 고민하다가 다시보니 X까지였다;

코딩중에는 algorithm의 count를 호출하려고 하는데, 멤버함수의 이름이 count여서 에러가 났다. std::를 붙이니 정상동작은 하는데 잘 싼 방법은 아닌거 같고.. for문을 한번 더 돌리면서 직접 비교하는 로직을 짜넣는게 좋을듯..

"ACEGIKMOQSUWY"
{"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"}
Returns: 150
A picture of vertical stripes, every other stripe is considered part of the dithered color.


#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <cmath>


using namespace std;

class ImageDithering
{
public:

 int count(string dithered, vector <string> screen)
 {
  int ret = 0;
  string sc;

  for(int j=0; j< screen.size(); j++)
  {
   sc = screen[j];

   for(int i=0; i< dithered.size(); i++)
   {    
    ret += std::count(sc.begin(), sc.end(), dithered[i]);
   }
  }

  return ret;
 }
};


SRM 146 DIV2
문제이해도 쉽고, 풀이도 간단했던 문제..

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <cmath>


using namespace std;

class YahtzeeScore
{
public:

 int maxPoints(vector <int> toss)
 {
  int ret = 0;
  vector<int> element(6, 0);

  for(int i=0; i < toss.size(); i++)
  {
   element[toss[i]-1] += toss[i];  
  }
 
  for(int i=0; i<6; i++)
  {
   ret = max(ret, element[i]);
  }

  return ret;
 }
};

*SRM147 DIV2
string에 대해 제대로 이해하고 있는지를 묻는 문제.

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <cmath>
using namespace std;

class CCipher
{
public:

 string decode(string text, int shift)
 {
  for(int i=0; i< text.size(); i++)
  {
   text[i] = (text[i] - 'A' - shift + 26) % 26 +'A';
  }
  return text;
 }
};

SRM148 DIV2

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <cmath>
using namespace std;

class DivisorDigits
{
public:

 int howMany(int number)
 {
  int v[10] = {0};
  int ret = 0 ;
  int n = number;

  for(int i=0; i<10; i++)
  {
   v[i] = n % 10;
   n /= 10;
   if(v[i] != 0 && (number % v[i] == 0)) ret++;
  } 
  return ret;
 }
};

SRM149 DIV2
int를 금액표기로 변경하는 문제. 내가 푼 방식은 숫자 크기가 기하급수로 더 커지면 곤란해지는 솔루션인듯..
다른사람들처럼 반복문 돌리면서 3번마다 , 를 찍어주는 방식으로 짜는게 좋은 솔루션인듯함.

class FormatAmt
{
public:
 string amount(int d, int c)
 {
  char buf_d[11];
  char buf_c[3];

  string cent;
  if(c >= 10) sprintf(buf_c, ".%d", c);
  else sprintf(buf_c, ".0%d", c);

  if(d >= 1000000000) sprintf(buf_d, "%d,%03d,%03d,%03d", d/1000000000, (d%1000000000)/1000000, (d%1000000)/1000, d%1000);
  else if(d >= 1000000) sprintf(buf_d, "%d,%03d,%03d", d/1000000, (d%1000000)/1000, d%1000);
  else if(d >= 1000) sprintf(buf_d, "%d,%03d",d/1000, d%1000);
  else sprintf(buf_d, "%d",d%1000);
  string ret("$");
  ret += string(buf_d)+ string(buf_c);
  return ret;

 }
};

SRM150 DIV2
비교적 쉬운 문제..
다른사람들 소스를 보니 마지막날 남은일 계산을 (remain + num -1) / num 으로 계산한 사람이 꽤 돼던데..
일부러 그렇게 짜는건지 숙달이 돼서 저게 더 편한지.. 암튼 대단함 ㅎㅎ;

class WidgetRepairs
{
public:
 int days(vector <int> arr, int num)
 {
  int remain = 0;
  int cnt = 0;

  for(int i=0; i< arr.size(); i++)
  {
   remain += arr[i];
   if( remain > 0)
   {
    remain -= num;
    if(remain < 0) remain = 0;
    cnt++;
   }
  }

  while(remain > 0)
  {
   remain -= num;
   cnt++;
  }
 
  return cnt;

 }
};

반응형