TCHS 10 Online Round 1
medの方がeasyより簡単だと思った
kに近い4の倍数と7の倍数は
[k/4]*4
[k/4+1]*4
[k/7]*7
[k/7+1]*7
([x]はガウスの記号(整数型の割り算))
のうちいずれか
#include <cstdio> #include <vector> #include <algorithm> #include <cmath> using namespace std; class TheSequencesLevelOne{ public: vector<int> find(vector<int> sequence){ for(int i = 0; i < sequence.size(); i++){ int x = sequence[i]; if(x % 4 == 0 || x % 7 == 0) continue; int k[4]; int f = x / 4; k[0] = f * 4; k[1] = (f+1) * 4; f = x / 7; k[2] = f * 7; k[3] = (f+1) * 7; sort(k, k + 4); int a = abs(x-k[0]), y = k[0]; for(int j = 1; j < 4; j++){ if(abs(x-k[j]) < a){ y = k[j]; a = abs(x - k[j]); } } sequence[i] = y; } return sequence; } };
初期の並びは関係ないし、辞書順で最初のものを求めたいので、ソートしてi項目とi+1項目を入れ替える(i>=2)
#include <cstdio> #include <algorithm> #include <vector> using namespace std; class TheSequencesLevelTwo{ public: vector<int> find(vector<int> seq){ sort(seq.begin(), seq.end()); for(int i = 1; 2 * i < seq.size(); i++){ j = 2 * i; int t = seq[j]; seq[j] = seq[j-1]; seq[j-1] = t; } return seq; } };