0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JOIG入門講座

Last updated at Posted at 2024-01-21

00 - Hello World

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

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

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

box

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

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

Discount Fare

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

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

Calc

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

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

Garden

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

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

Payment

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

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

02 - 条件分岐

Rounding

#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;
  }
}

帰省

#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つの整数

#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に最も近い値

#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;
  }
}

鉛筆

#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

#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

#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

#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

#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;

双六

#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

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int a, b;
    cin >> a >> b;
    cout << a * b << endl;
}

2 - 寝坊だ!ピ太郎!

#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 - 動画検索

#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 - 長方形 α

#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 - 和の最大値 β

#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

#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

#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

#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文字列

#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ソート

#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;
}

シーザー暗号

#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;
}

文字列の反転

#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 - 配列

分割

#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;
}

未提出者は誰だ

#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

#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

#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;
}

共通要素

#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;
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?