FoxProgression srm 501 div2 easy

問題

http://www.topcoder.com/stat?c=problem_statement&pm=11283&rd=14430 (要ログイン)

  • 整数配列が与えられる。
  • それに一つ付け足して、等差数列 or 等比数列にしたい。
  • 付け加える事のできる数字は何種類か

方針

素直に調べる。

答案

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iterator>


using namespace std;


class FoxProgression{
public:
  bool checkDiff(vector<int>& v, int& d){
    d = v[1] - v[0];
    for(int i = 2; i < (int)v.size(); i++){
      if(v[i] - v[i - 1] != d){
	return false;
      }
    }
    return true;
  }

  bool checkRatio(vector<int>& v, int& r){
    if(v[1] % v[0] != 0) return false;
    r = v[1] / v[0];
    for(int i = 2; i < (int)v.size(); i++){
      if(v[i] % v[i - 1] != 0 || v[i] / v[i - 1] != r){
	return false;
      }
    }
    return true;
  }

  int theCount(vector <int> seq){
    if(seq.size() == 1){
      return -1;
    }
    int commonDiff, commonRatio;
    bool arithmetic, geometric;
    arithmetic = checkDiff(seq, commonDiff);
    geometric = checkRatio(seq, commonRatio);

    if(arithmetic && geometric){
      if(seq.back() + commonDiff == seq.back() * commonRatio){
	return 1;
      }
      else{
	return 2;
      }
    }
    else if(arithmetic || geometric){
      return 1;
    }
    else{
      return 0;
    }
  }



};