[UVa] 13148 - A Giveaway

題目

Link: UVa 13148 - A Giveaway

Hint

這題真的是 Giveaway...

Code

C++

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

constexpr const int MAX = 100000000;

int main()
{
  std::vector<int> power_of_2;
  std::vector<int> power_of_3;
  std::vector<int> special;
  std::vector<int>::const_iterator iter2, iter2_end;
  std::vector<int>::const_iterator iter3, iter3_end;

  int i, temp;

  for (i = 1; (temp = i * i) <= MAX; ++i) power_of_2.push_back(temp);
  for (i = 1; (temp = i * i * i) <= MAX; ++i) power_of_3.push_back(temp);

  iter2 = power_of_2.cbegin(), iter2_end = power_of_2.cend();
  iter3 = power_of_3.cbegin(), iter3_end = power_of_3.cend();

  while (iter2 != iter2_end && iter3 != iter3_end)
  {
    if (*iter3 == *iter2) special.push_back(*(iter3++));
    else if (*iter3 < *iter2) ++iter3;
    else ++iter2;
  }

  std::ios::sync_with_stdio(false);
  while (std::cin >> temp && temp)
  {
    if (std::binary_search(special.cbegin(), special.cend(), temp))
      std::cout << "Special\n";
    else std::cout << "Ordinary\n";
  }

  return 0;
}
Show Comments