2
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?

AtCoderのABCを解いてみよう!

2
Last updated at Posted at 2025-12-13

AtCoderのABCを解いてみよう!

学校でプログラム言語を学んでもその知識の使い道が無かったりします。そんなときにAtCoderは初級者でも解けるプログラム力を試す問題がたくさんあります!

AtCoderとは (簡単に)

  • 簡単な問題から難しい問題までがあり、プログラム力の練習ができる。
  • プログラムといっても色々あるが、今回紹介するABCコンテストはアルゴリズムの問題。
  • サイト https://atcoder.jp/

AtCoderを始めたての方はまずABCコンテストのA問題を楽に解けるようになることが最初のステップだと思います。

ABCの問題を解いてみる

簡単なA問題から解いていきます。今回は[AtCoder Beginner Contest 434(ABC434)]を解いていきます。
言語はC言語でやっていきます。

・A問題

整数W,Bが与えられるので、まず変数を用意してあげます。

int W; //[高橋君の体重]
int B; //[?]

入力を見てみると

W B

の形式で与えられることが分かります。C言語はscanfが手軽なため

scanf("%d %d", &W, &B);

次に、比較がしやすいように高橋君の体重を[g]に換算します。

W = W *1000;

問題文から、風船をn個物体に付けたとき、その物体の質量がnB[g]未満ならば飛び立つことが出来ると読み取ることが出来ます。頭に思い浮かべる計算式は以下のようになります。

W[g](物体の重さ) < nB[g]

よって、この不等式が成り立つ風船の最低の個数nを求めれば良いと分かります。
最初に風船の個数を入れるnを用意して

int n = 0;

としてあげて、そして後はwhile文の中でnを1つずつ増やしていきます。

while(w >= B *n) {
    n++;
}

ここで、whileですが、問題ではW[g] < nB[g]となる最低のnを求めるため、W >= nBを条件式としています。W[g] < nB[g]の逆の条件式を使えればよいので、

while(!(w < B *n)) {
    n++;
}

でもOKです。
最後にprintf("%d\n", n);で出力してあげて完了です!

・B問題

A問題が解けたら次はB問題です。
入力をみてみると、

N M
A1 B1
A2 B2


A_N B_N

となっているため、最初にN,Mを取得して、

int n;//[羽数]
int m;//[種類]
scanf("%d %d", &n, &m);

あとは適当にfor文とscanfを組み合わせて取得します。

int birdType[n];//[羽数]:種類の格納
int birdWeight[n];//[羽数]:大きさの格納
for (int i = 0; i < n; i++) {
    scanf("%d %d", &birdType[i], &birdWeight[i]);
}

そして、あとは各番号の鳥の種類を全探索し、各種類での大きさをそれぞれ加算していき、各種類ごとに出力してあげます。

for (int i = 0; i < m; i++) {
    int sum = 0;
    int count = 0;
    for (int j = 0; j < n; j++) {
        if (birdType[j] == i+1) {
            sum += birdWeight[j];
            count++;
        }
    }
    printf("%.10f\n", (float)sum/count);
}

あと、出力が平均値で真値との誤差も指定しているため、小数点以下6~桁くらい出力してあげてます。
これで完了です!

以下A問とB問の筆者のプログラムです。学習の参考になれば幸いです。
### A問題

#include <stdio.h>
int main(void) {
    int w, b;
    scanf("%d %d", &w, &b);

    w = w *1000;

    int num = 0;
    while(w >= b *num) {
        num++;
    }
    printf("%d\n", num);
}

### B問題

#include <stdio.h>
int main(void) {
    int n;//[羽数]
    int m;//[種類]
    scanf("%d %d", &n, &m);

    int birdType[n];//[番号]:種類
    int birdWeight[n];//[番号]:大きさ
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &birdType[i], &birdWeight[i]);
    }

    for (int i = 0; i < m; i++) {
        int sum = 0;
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (birdType[j] == i+1) {
                sum += birdWeight[j];
                count++;
            }
        }
        printf("%.10f\n", (float)sum/count);
    }
}
2
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
2
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?