A.キャンディーと2人の姉妹
タイトル
数値を2つの等しくない数値に分割するケースはいくつありますか?
アイデア
奇数を2で割った場合、偶数は等しい場合は減算する必要があります。
コード
#include <bits / stdc ++。h> using namespace std; void solve(){ int n; cin >> n; if(n&1)cout << n / 2 << " \ n " ; 他の coutの<< N / 2 - 1 << " \ n個" ; } int main(){ int t; cin >> t; while(t-- )solve(); }
B.文字列を作成する
タイトル
$ n $の長い文字列を作成し、$ a $の連続する各部分文字列が$ b $異なる文字になるようにします。
アイデア
構築するには$ b $文字を使用するだけです。
コード
#include <bits / stdc ++。h> using namespace std; void solve(){ int n、a、b; cin >> n >> a >> b; for(int i = 0 ; i <n; i ++ ){ cout << char(' a ' + i%b); } cout << " \ n " ; } int main(){ int t; cin >> t; while(t-- )solve(); }
C.構成する2つのチーム
タイトル
配列内のいくつかの数値を選択し、それらを2つのグループに分割します。1つのグループはすべて異なり、1つのグループはすべて同じです。
アイデア
1つだけの数と1つの数の最大数を記録します。後者と前者の差が$ 2 $以上の場合、2番目の配列は最初の配列に数値を与えることができます。それ以外の場合は2つの小さい方価値、そして最後にサンプルの2つのケースを判断します。
コード
#include <bits / stdc ++。h> using namespace std; void solve(){ int n; cin >> n; map < int、int > m; int c [n]; for(int&i:c)cin >> i、++ m [i]; int a = 0、b = 0 ; for (auto i:m) if(i.second == 1)++ a; else { if(b)++ a; b = max(b、i.second); } if(n == 1 ) cout << 0 << " \ n " ; それ以外の 場合(b == 0 ) cout << 1 << " \ n " ; else if(b-a> = 2 ) cout << a + 1 << " \ n " ; else cout << min(a、b)<< " \ n " ; } int main(){ int t; cin >> t; while(t-- )solve(); }
D.アンチ数独
タイトル
$ 9 {\回} 9 $数独正方行列を逆数独正方行列に変更します。つまり、少なくとも3つの行、列、および$ 9 $正方配列の$ 3 {\回} 3 $は同じ数であり、ほとんどの変更$ 9 $番号。
アイデア
$ 9 $正方行列で繰り返さずに$ 9 $行と列を選択し、これらの$ 9 $行と列の交点の数を変更します。
実際、数独の性質を使用して、ある数を別の数に変更するだけで十分です。正しい数独正方行列では、各数は、繰り返されない行、列、および正方行列の9つのセットに対応するためです。
コード
#include <bits / stdc ++。h> using namespace std; void solve(){ char MP [ 10 ] [ 10 ] = {}; for(int i = 1 ; i <= 9 ; i ++ ){ for(int j = 1 ; j <= 9 ; j ++ ){ cin >> MP [i] [j]; } } BOOLの vis_row [ 10 ] = {}; BOOLの vis_col [ 10 ] = {}; のための(INT I = 0 ; I < 3 ; I ++){ // 単位の変化のために3×3のブロックに するための(INT J = 0 ; J < 3 ; j ++ ){ bool flag = false ; for(int x = 1 + 3 * i; x <= 3 *(i + 1); x ++ ){ for(int y = 1 + 3 * j; y <= 3 *(j + 1); y ++ ){ if(!vis_row [x] and!vis_col [y]){ if(MP [x] [y] == ' 9 ')MP [x] [y] = ' 1 ' ; それ以外の場合 MP [x] [y] ++ ; vis_row [x] = vis_col [y] = true ; フラグ = true ; 休憩; } } if(flag)break ; } } } for(int i = 1 ; i <= 9 ; i ++ ){ for(int j = 1 ; j <= 9 ; j ++ ){ cout << MP [i] [j]; } cout << " \ n " ; } } int main(){ int t; cin >> t; while(t-- )solve(); }
記入するEF。