LoginSignup
0
0

More than 3 years have passed since last update.

【Java】型変換・BufferedReader (AOJ③割り算・フレームの描画)

Last updated at Posted at 2020-11-14

AIZU ONLINE JUDGE の教材を使って勉強していきます

型変換(キャスト)

  • Javaは静的型付けであるが型変換が許される
  • 拡大変換
    • intからlong
    • 整数から浮動小数点への変換すると型落ちが発生することがあるので注意
  • 縮小変換(キャスト、明示的な変換)
    • intからbyte
    • 明示的に変換の意思を示す必要がある
    • int とshort の変換で符号が変わってしまうこともあるので注意
int i = 10;
// byte b = i ; //NG
byte b  = (byte)i ; //OK 

割り算

2つの整数 a と b を読み込んで、以下の値を計算するプログラムを作成して下さい:
* a ÷ b : d (整数)
* a ÷ b の余り : r (整数)
* a ÷ b : f (浮動小数点数)
Output
d, r, f を1つの空白で区切って1行に出力して下さい。fについては、0.00001以下の誤差があってもよいものとします。

  • intどうしの割り算では結果が切り捨てられてしまうが、少なくとも一方の変数をdoubleに型変換(キャスト)すればdoubleで計算できる
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String strArr[] = br.readLine().split(" ");
        int a=Integer.parseInt(strArr[0]);
        int b=Integer.parseInt(strArr[1]);
        int d=a/b;
        int r=a%b;
        double f=(double)a/b; //型の変換
        System.out.println(d+" "+r+" "+String.format("%.6f",f));
    }
}

BufferedReaderクラス

  • テキストファイルを読み込むためのクラス
  • readLineメソッド:1行のテキストを読み込み
    • 1行の終端は改行 (\n,\r)
    • 改行文字は読み込んだ文字に含まれない
    • 自動的に読み込む位置が次の行に移る

フレームの描画

以下のような、たてH cm よこ W cm の枠を描くプログラムを作成して下さい。

##########
#........#
#........#
#........#
#........#
##########

上図は、たて 6 cm よこ 10 cm の枠を表しています。
Input
入力は複数のデータセットから構成されています。各データセットの形式は以下のとおりです:H W
H, W がともに 0 のとき、入力の終わりとします。
Output
各データセットについて、たて H cm よこ W cm の枠を出力して下さい。
各データセットの後に、1つの空行を入れて下さい。
Constraints
* 3 ≤ H ≤ 300
* 3 ≤ W ≤ 300

//例1
//実行00.21 s
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while( ((line  = br.readLine()) != null) && !(line .equals("0 0"))){

            int H =Integer.parseInt(line.split(" ")[0]);
            int W =Integer.parseInt(line.split(" ")[1]);

            for(int i = 0; i < H; i++){
                if (i == 0 || i == (H-1) ){
                   for(int j = 0; j < W; j++){
                        System.out.print("#");
                    }
                }
                else{
                    for(int j = 0; j < W; j++){
                        if (j == 0 || j == (W-1) ){
                            System.out.print("#");
                        }else{  
                          System.out.print(".");
                        }
                    }
                }
              System.out.println();
            }
          System.out.println();
        }
        br.close();
    }
}

StringBuilderで高速化

  • 注意:毎回line = br.readLine();を更新しないと最後の結果のみ出力されてしまう。。
    • NG例
    • String line;
    • while( ((line = br.readLine()) != null) && !(line .equals("0 0"))){
//例2
// StringBuilder result = new StringBuilder();
//実行時間00.05 s

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();

    while( (line != null) && !(line .equals("0 0"))){

            String[] inputs = line.split(" ");
            int h = Integer.valueOf(inputs[0]);
            int w = Integer.valueOf(inputs[1]);
            StringBuilder result = new StringBuilder();

            for(int i = 0; i < h; i++){
                if(i == 0 || i == h-1){
                    for(int j = 0; j < w; j++) result.append("#");
                }else{
                    for(int j = 0; j < w; j++){
                        if(j == 0 || j == w-1) result.append("#");
                        else result.append(".");
                    }
                }
                result.append("\n");
            }

            System.out.println(result);

            line = br.readLine();
        }

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