2011年9月2日金曜日

0221



なんか後輩が悩んでいた問題、実装だるかった、でもstd::list使ってすっきりかけたと思う、うん、すっきりかけた。追記:人の書いたコードの落ちるテストケース作るのは楽しいです(笑)

#include <stdio.h>
#include <list>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(void)
{
list<int> lst;
int n, m;
char say[10000][20];
list<int>::iterator it;
list<int>::iterator pit;

while (1){
scanf("%d%d", &n, &m);

if (n == 0) break;

lst.clear();

for (int i = 0; i < n; i++){
lst.push_back(i + 1);
}

for (int i = 0; i < m; i++){
scanf("%s", say[i]);
}

it = lst.begin();
for (int i = 1; i <= m; i++){
int cm;

if (lst.size() == 1){
break;
}

pit = it;
it++;

cm = 0;
if (i % 3 == 0){
cm--;
}
if (i % 5 == 0){
cm -= 2;
}

if (cm == 0){
if (i != atoi(say[i - 1])){
lst.erase(pit);
}
}
else if (cm == -1){
if (strcmp(say[i - 1], "Fizz") != 0){
lst.erase(pit);
}
}
else if (cm == -2){
if (strcmp(say[i - 1], "Buzz") != 0){
lst.erase(pit);
}
}
else if (cm == -3){
if (strcmp(say[i - 1], "FizzBuzz") != 0){
lst.erase(pit);
}
}
if (it == lst.end()){
it = lst.begin();
}
}


it = lst.begin();
printf("%d", *it++);
for (; it != lst.end(); it++){
printf(" %d", *it);
}
puts("");
}

return (0);
}

0 件のコメント:

コメントを投稿