0
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 317(A,Bだけ)

Last updated at Posted at 2023-08-29

はじめに

C 以降はお勉強をしてから取り組みます。

コンテスト出題内容

A

必要量 X - H を diff と置いて、 diff よりも大きい数値の中で一番小さな値を探す。

-> diff を P の値のリストに突っ込んでその位置を特定する。その index + 1 が答え。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.length() == 0) break;
            list.add(s);
        }
        scanner.close();
        List<String> numbers = Arrays.asList(list.get(0).split(" "));
        int H = Integer.valueOf(numbers.get(1));
        int X = Integer.valueOf(numbers.get(2));
        List<Integer> Ps = new ArrayList<>();
        for (String s : list.get(1).split(" ")) {
            Ps.add(Integer.valueOf(s));
        }

        int diff = X - H;
        Ps.add(diff);
        Collections.sort(Ps);
        System.out.println(Ps.indexOf(diff) + 1);
    }
}

B

ある連続する整数の配列の中で一つだけ欠けている数値を特定する。

-> 初項と末項が判明しているので、ステップ1の等差数列の和を算出し、そこから与えられた配列の総和を引いて出た値が答え。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.length() == 0) break;
            list.add(s);
        }
        scanner.close();
        double N = Double.valueOf(list.get(0));
        List<Integer> As = new ArrayList<>();
        for (String s : list.get(1).split(" ")) {
            As.add(Integer.valueOf(s));
        }
        Collections.sort(As);
        int total =(int) (((N+1)/2) * (As.get(0) + As.get(As.size()-1)));
        int pseudoTotal = As.stream().mapToInt(Integer::intValue).sum();
        System.out.println(total - pseudoTotal);
    }
}

0
0
1

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