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.

ABC168を解いてみた(C問題まで)

Posted at

こんにちは!!

今回はAtCoderのコンテストに向けて過去問を解いたので、C問題までの各問題について方針と解答コードを投稿していきたいと思います。

#A問題-∴ (Therefore)
https://atcoder.jp/contests/abc168/tasks/abc168_a

###方針

  1. 与えられる入力値(Nとする。Nは999以下の正整数)を文字列方で受け取る。
  2. 一の位を比べれば良いので、1.で受け取った文字列から末尾の文字を取り出して数値型に変換
  3. 2.で変換した値を問題文に従って判定して分岐させる。

###コード
if文の分岐をswitch文に変えても良かったかもしれません…💦

標準入力と出力に関しては下記サイトから拝借しています。

Main.java
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);

		String n = fs.next();
		int firstPlace = Character.getNumericValue(n.charAt(n.length() - 1));

		if(firstPlace == 3) {
			out.println("bon");
		}else if(firstPlace == 0 || firstPlace == 1 || firstPlace == 6|| firstPlace == 8) {
			out.println("pon");
		}else {
			out.println("hon");
		}

		out.flush();
		fs.close();
	}

}


class FastScanner{
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}

    public void close() {
    	try {
			in.close();
		} catch (IOException e) {
			// TODO: handle exception
		}
    }
}

#B問題-... (Triple Dots)
https://atcoder.jp/contests/abc168/tasks/abc168_b

###方針

  1. 文字列(Sとする)と数値(K)を受け取ります。
  2. 文字列Sの長さ(.length()メソッドを使う)とKの大きさを比べる。
  3. 2.の結果Sの長さがK以下の場合はそのままSを出力して、Sの長さがKを上回っていた場合は、substring()メソッドを使って先頭K文字を取り出した後に文字列"..."を追加する。

###コード

Main.java
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		//Sの長さ
		int k = fs.nextInt();
		//文字列
		String s = fs.next();

		if(s.length() <= k) {
			out.println(s);
		}else {
			out.println(s.substring(0, k).toString() + "...");
		}

		out.flush();
		fs.close();
	}

}


class FastScanner{
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}

    public void close() {
    	try {
			in.close();
		} catch (IOException e) {
			// TODO: handle exception
		}
    }
}

#C問題-: (Colon)
https://atcoder.jp/contests/abc168/tasks/abc168_c

###方針

  1. 時針と分針が1分間に何度ずつ動くのかそれぞれ計算して求める。
  2. 1.で求めたそれぞれの値に対して、H時M分を分単位で表した値を掛けることで時針と分針が進んだ角度を求めることができる。
    1. で求めた分針の角度 - 時針の角度から時針と分針に挟まれた角度を求めることができる。
  3. 時針の長さAと分針の長さB、3.で求めた角度から余弦定理を用いて答えを導く

###コード

Main.java
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		//時針の長さ
		int a = fs.nextInt();
		//分針の長さ
		int b = fs.nextInt();

		//時間
		int h = fs.nextInt();
		//分
		int m = fs.nextInt();
		//1分ごとの時針の角度
		double hour_degree_per_m = 360 / 12;
		hour_degree_per_m /= 60;
		//1分ごとの分針の角度
		double minute_degree_per_m = 360 / 60;

		//合計で何分なのか
		int total_minute = 60 * h + m;

		//時針は分数のみで判断する
		double hour_degree = hour_degree_per_m * total_minute;
		double minute_degree = minute_degree_per_m * total_minute;

		double degree = minute_degree - hour_degree;

		if(degree > 180.0) {
			degree = 360 - degree;
		}

		BigDecimal ans = new BigDecimal(cosineTheorem(a, b, degree));
		out.println(ans.doubleValue());




		out.flush();
		fs.close();
	}

	/**
	 * 余弦定理のメソッド
	 * @param a 辺の長さ
	 * @param b 辺の長さ
	 * @param c 角度
	 * @return 対辺の長さ
	 */
	public static double cosineTheorem(double a,double b,double c) {
		double d = Math.pow(a, 2) + Math.pow(b, 2) - 2.0 * a * b * Math.cos(Math.toRadians(c));
		return Math.sqrt(d);
	}
}


class FastScanner{
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}

    public void close() {
    	try {
			in.close();
		} catch (IOException e) {
			// TODO: handle exception
		}
    }
}

#参考文献
https://qiita.com/p_shiki37/items/a0f6aac33bf60f5f65e4

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?