JOIG入門講座

# 00 - Hello World

```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
 cout << "Hello, world!" << endl;
}
```

# 01 - 初めてのプログラミング

## [box](https://atcoder.jp/contests/abc180/tasks/abc180_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, A, B;
    cin >> N >> A >> B;
    cout << N - A + B << endl;
}
```

## [Discount Fare](https://atcoder.jp/contests/abc113/tasks/abc113_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int X, Y;
    cin >> X >> Y;
    cout << X + Y / 2 << endl;
}
```

## [Calc](https://atcoder.jp/contests/abc172/tasks/abc172_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int a;
    cin >> a;
    cout << a + a*a + a*a*a << endl;
}
```

## [Garden](https://atcoder.jp/contests/abc106/tasks/abc106_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int A, B;
    cin >> A >> B;
    cout << A * B - (A + B - 1) << endl;
}
```

## [Payment](https://atcoder.jp/contests/abc173/tasks/abc173_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    cin >> N;
    cout << (1000 - N % 1000) % 1000 << endl;
}
```

# 02 - 条件分岐

## [Rounding](https://atcoder.jp/contests/abc130/tasks/abc130_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
  int X, A;
  cin >> X >> A;
  if(X<A){
      cout << 0 << endl;
  }
  else{
      cout << 10 << endl;
  }
}
```

## [帰省](https://atcoder.jp/contests/joi2021yo1b/tasks/joi2021_yo1b_a)
```C++
#include <bits/stdc++.h>
using namespace std;
int main(){
  int A, B, C;
  cin >> A >> B >> C;
  if(A<=C && B>C){
      cout << 1 << endl;
  }
  else{
      cout << 0 << endl;
  }
}
```

## [3つの整数](https://atcoder.jp/contests/joi2020yo1a/tasks/joi2020_yo1a_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
  int A, B, C;
  cin >> A >> B >> C;
  int one;
  if(A == 1){
      one += 1;
  }
  if(B == 1){
      one += 1;
  }
  if(C == 1){
      one += 1;
  }
  if(one >= 2){
      cout << 1 << endl;
  }
  else{
      cout << 2 << endl;
  }
}
```

## [Xに最も近い値](https://atcoder.jp/contests/joi2020yo1c/tasks/joi2020_yo1c_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
  int X, L, R;
  cin >> X >> L >> R;
  if(L<=X && X<=R){
      cout << X << endl;
  }
  else if(X<L){
      cout << L << endl;
  }
  else{
      cout << R << endl;
  }
}
```

## [鉛筆](https://atcoder.jp/contests/joi2018yo/tasks/joi2018_yo_a)
```C++
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, A, B, C, D;
  cin >> N >> A >> B >> C >> D;
  int X, Y;
  
  if(N % A == 0){
      X = N / A;
  }
  else{
      X = N / A + 1;
  }
  
  if(N % C == 0){
      Y = N / C;
  }
  else{
      Y = N / C + 1;
  }
 
 if(B * X <= D * Y){
     cout << B * X << endl;
 }
 else{
     cout << D * Y << endl;
 }
}

```

# 03 - 繰り返し

## [Roller Coaster](https://atcoder.jp/contests/abc142/tasks/abc142_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
  int N, K, h;
  cin >> N >> K;
  int sum = 0;
  for(int i=0; i<N; i++){
      cin >> h;
      if(h >= K){
          sum += 1;
      }
  }
  cout << sum << endl;
}
```

## [Archive the Goal](https://atcoder.jp/contests/abc151/tasks/abc151_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
  int N, K, M, a;
  cin >> N >> K >> M;
  int goal = N * M;
  int sum = 0;
  for(int i=0; i<N-1; i++){
      cin >> a;
      sum += a;
  }
  if(sum >= goal){
      cout << 0 << endl;
  }
  else if(sum + K < goal){
      cout << -1 << endl;
  }
  else{
      cout << goal - sum << endl;
  }
}
```

## [FizzBuzzSum](https://atcoder.jp/contests/abc162/tasks/abc162_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    long long N;
    cin >> N;
    long long answer=0;
    
    for(int i=1; i<N+1; i++){
        if(i%3!=0 && i%5!=0){ 
            answer+=i;
        }
    }
    cout << answer << endl;
}
```

## [Great Ocean View](https://atcoder.jp/contests/abc124/tasks/abc124_b)
```C++
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, H;
  cin >> N;
  int sum = 1;
  cin >> H;
  int max = H;
  for(int i=1; i<N; i++){
      cin >> H;
      if(max <= H){
          sum += 1;
          max = H;
      }
  }
  cout << sum << endl;
```

## [双六](https://atcoder.jp/contests/joi2018yo/tasks/joi2018_yo_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
  int N, A;
  cin >> N;
  int sum = 0;
  int max = 0;
  for(int i=0; i<N; i++){
      cin >> A;
      if(A == 1){
          sum += 1;
      }
      else if(max < sum){
          max = sum;
          sum = 0;
      }
      else{
          sum = 0;
      }
  }
  if(max < sum){
      max = sum;
  }
  cout << max + 1 << endl;
}
```

# 振り返りコンテスト

## [1 - Multiplication 1](https://atcoder.jp/contests/abc169/tasks/abc169_a)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int a, b;
    cin >> a >> b;
    cout << a * b << endl;
}
```

## [2 - 寝坊だ！ピ太郎！](https://atcoder.jp/contests/otemae2019/tasks/otemae2019_a)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int a, b;
    cin >> a >> b;
    if(a < b){
        cout << 0 << endl;
    }
    else{
        cout << 1 << endl;
    }
}
```

## [3 - 動画検索](https://atcoder.jp/contests/dwacon2017-prelims/tasks/dwango2017qual_a)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int n, a, b;
    cin >> n >> a >> b;
    int ans = (a + b) - n;
    if(ans > 0){
        cout << ans << endl;
    }
    else{
        cout << 0 << endl;
    }
}
```

## [4 - 長方形 α](https://atcoder.jp/contests/chokudai_S002/tasks/chokudai_S002_a)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int n;
    cin >> n;
    for(int i; i<n; i++){
        long long a, b;
        cin >> a >> b;
        cout << a * b << endl;
    }
}
```

## [5 - 和の最大値 β](https://atcoder.jp/contests/chokudai_S002/tasks/chokudai_S002_d)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int n;
    cin >> n;
    long long sum = 0;
    
    for(int i; i<n; i++){
        long long a, b;
        cin >> a >> b;
        if(a<b){
            sum += b;
        }
        else{
            sum += a;
        }
    }
    cout << sum << endl;
}
```

## [6 - Number of Multiples](https://atcoder.jp/contests/aising2020/tasks/aising2020_a)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int L, R, d, ans;
    cin >> L >> R >> d;
    ans = 0;
    
    for(int i=L; i<R+1; i++){
        if(i % d == 0){
            ans += 1;
        }
    }
    cout << ans << endl;
}
```

## [7 - Polygon](https://atcoder.jp/contests/abc117/tasks/abc117_b)
```C++
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int N, sum=0, max=0;
    cin >> N;
    
    for(int i=0; i<N; i++){
        int l;
        cin >> l;
        sum += l;
        if(max<l){
            max = l;
        }
    }
    
    sum -= max;
    if(sum > max){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}
```

# 04 - 文字列

## [Plural Form](https://atcoder.jp/contests/abc179/tasks/abc179_a)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    string S;
    cin >> S;
    if(S[S.size()-1] == 's'){
        cout << S + "es" << endl;
    }
    else{
        cout << S + 's' << endl;
    }
}
```

## [IOI文字列](https://atcoder.jp/contests/joi2021yo1c/tasks/joi2021_yo1c_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    string S;
    cin >> N >> S;
    int cnt=0;
    for(int i=0; i<N; i++){
        if(i % 2 == 0 && S[i] != 'I'){
            cnt += 1;
        }
        if(i % 2 != 0 && S[i] != 'O'){
            cnt += 1;
        }
    }
    cout << cnt << endl;
}
```

## [JOIソート](https://atcoder.jp/contests/joi2021yo1a/tasks/joi2021_yo1a_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    string S;
    cin >> N >> S;
    int j=0, o=0, i=0;
    for(int a=0; a<N; a++){
        if(S[a] == 'J'){
            j += 1;
        }
        if(S[a] == 'O'){
            o += 1;
        }
        if(S[a] == 'I'){
            i += 1;
        }
    }
    for(int a=0; a<j; a++){
        cout << 'J';
    }
    for(int a=0; a<o; a++){
        cout << 'O';
    }
    for(int a=0; a<i; a++){
        cout << 'I';
    }
    cout << endl;
}
```

## [シーザー暗号](https://atcoder.jp/contests/joi2007yo/tasks/joi2007yo_c)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    string S;
    cin >> S;
    for(int i=0; i<S.size(); i++){
        if('A' <= S[i] && S[i] <= 'C'){
            cout << char(S[i] + 23);
        }
        else{
            cout << char(S[i] - 3);
        }
    }
    cout << endl;
}
```

## [文字列の反転](https://atcoder.jp/contests/joi2020yo1b/tasks/joi2020_yo1b_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, A, B;
    string S;
    cin >> N >> A >> B >> S;
    string s;
    for(int i=A-1; i<B; i++){
        s += S[i];
    }
    for(int i=0; i<A-1; i++){
        cout << S[i];
    }
    for(int i=s.size()-1; i>=0; i--){
        cout << s[i];
    }
    for(int i=B; i<S.size(); i++){
        cout << S[i];
    }
    cout << endl;
}
```

# 05 - 配列

## [分割](https://atcoder.jp/contests/joi2021yo1b/tasks/joi2021_yo1b_c)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, A[100];
    cin >> N;
    
    for(int i=0; i<N; i++){
        cin >> A[i];
    }
    
    int max = 0;
    for(int i=0; i<N; i++){
        if(max < A[i]){
            max = A[i];
        }
    }
    
    int flg=0, a=0, b=0;
    for(int i=0; i<N; i++){
        if(A[i] == max){
            flg = 1;
        }
        else if(flg == 0){
            a += A[i];
        }
        else{
            b += A[i];
        }
    }
    
    cout << a << endl;
    cout << b << endl;
}
```

## [未提出者は誰だ](https://atcoder.jp/contests/joi2007yo/tasks/joi2007yo_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int A[28], ALL[30];
    for(int i=0; i<28; i++){
        cin >> A[i];
    }
    for(int i=0; i<30; i++){
        ALL[i] = i+1;
    }
    
    for(int i=0; i<28; i++){
        if(A[i] == ALL[A[i]-1]){
            ALL[A[i]-1] = 0;
        }
    }
    
    int a=0, b=0;
    for(int i=0; i<30; i++){
        if(ALL[i] != 0 && a == 0){
            a = ALL[i];
        }
        else if(ALL[i] != 0){
            b = ALL[i];
        }
    }
    
    cout << a << endl;
    cout << b << endl;
}
```

## [Counting Roads](https://atcoder.jp/contests/abc061/tasks/abc061_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, M, A[50], B[50], C[50];
    cin >> N >> M;
    for(int i=0; i<M; i++){
        cin >> A[i] >> B[i];
    }
    for(int i=0; i<N; i++){
        C[i] = 0;
    }
    
    for(int i=0; i<M; i++){
        C[A[i]-1] += 1;
        C[B[i]-1] += 1;
    }
    
    for(int i=0; i<N; i++){
        cout << C[i] << endl;
    }
}
```

## [Kagami Mochi](https://atcoder.jp/contests/abc085/tasks/abc085_b)
```C++
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, A[100], B[100];
    cin >> N;
    for(int i=0; i<N; i++){
        cin >> A[i];
    }
    for(int i=0; i<100; i++){
        B[i] = 0;
    }
    
    for(int i=0; i<N; i++){
        B[A[i]-1] = 1;
    }
    
    int ans = 0;
    for(int i=0; i<100; i++){
        ans += B[i];
    }
    
    cout << ans << endl;
}
```

## [共通要素](https://atcoder.jp/contests/joi2021yo1a/tasks/joi2021_yo1a_c)
```C++
#include <bits/stdc++.h>
using namespace std;
int main(){
    int N, M, A[100], B[100], C[100], D[100];
    cin >> N >> M;
    for(int i=0; i<N; i++){
        cin >> A[i];
    }
    for(int i=0; i<M; i++){
        cin >> B[i];
    }
    for(int i=0; i<100; i++){
        C[i] = 0;
    }
    for(int i=0; i<100; i++){
        D[i] = 0;
    }
    
    for(int i=0; i<100; i++){
        if(i<N){
            C[A[i]-1] += 1;
        }
        if(i<M){
            D[B[i]-1] += 1;
        }
    }
    
    for(int i=0; i<100; i++){
        if(C[i] >= 1 && D[i] >= 1){
            cout << i+1 << endl;
        }
    }
}
```