LoginSignup
0
0

More than 1 year has passed since last update.

Programmars 問題2個 解説

Posted at

직사각형으로 별 찍기

import java.util.Scanner;

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();

   for(int i = 0; i < b ; i ++ ){ // 줄의 개수인 b만큼 반복되도록 작성해주고 반복될 때마다 하나씩 증가한다.(0,1,2에서 끝)
       for(int j = 0; j < a; j++){ //한 줄의 찍힌 별의 개수인 a만큼 반복하도록 작성해주고 반복될 때마다 하나씩 증가한다.

​(0,1,2,3,4)
System.out.print("*"); // for문이 반복되면서 이 찍히도록 System.out.print(""); 로 출력되도록 작성해 준다.
}
System.out.println(); // for문을 빠져나와 표시 위치를 다음 행의 동일 위치로 이동시키기 위해 System.out.println(); 을 작성해 준다.
}
}
}
우선 출력된 결과를 보면 별이 찍힌 건 한 줄에 5개씩 총 3줄이 찍혀있고 입력된 값을 보면 a=5, b=3이다.

여기서 보면 a는 한 줄에 찍힌 별의 개수, b는 줄의 개수를 나타낸다고 유추해 낼 수 있다.

그럼 여기서 중복 for문을 사용하면 출력된 값과 동일하게 찍어낼 수 있다.

=======================================================================================================================
ー日本語ー

for(inti=0;i<b;i++){ //行数であるbだけ繰り返されるように作成し、繰り返されるたびに一つずつ増加する。

for(int j = 0; j < a:j++){ //一行の写った星の数であるaだけ繰り返すように作成し、繰り返されるたびに一つずつ増加する。

​System.out.print(""); //for文が繰り返されながらが写るようにSystem.out.print("*");と出力されるように作成してくれる。

System.out.println(); //for文を抜けて表示位置を次の行の同一位置に移動させるためにSystem.out.println();を作成してくれる。

まず出力された結果を見れば、星が押されたのは一行に5個ずつ計3列が押されており、入力された値を見ればa=5、b=3だ。

ここで見れば、aは一列に撮られた星の個数、bは列の個数を表すと類推できる。

では、ここで重複for文を使えば、出力された値と同じように印刷することができる。

-----------------------------------------------------------------------------------------------------------------------문자열 다루기 기본

class Solution {
public boolean solution(String s) {
boolean answer = true;

  if(s.length() != 4 && s.length() != 6)    
          answer = false;   // 길이가 4나 6이 아닌 경우 => false
  for(int i=0; i<s.length(); i++)   // 문자열에 숫자가 아닐 경우 => false
             if(s.charAt(i) >= 'a')  // charAt은 특정 위치의 문자를 반환, 'a'와 같거나 큰 경우 false
          answer = false;
  return answer;

}
}

s.charAt(i) -> string을 문자를 char형으로 변경해준다..
'a' -> 97
'A' -> 65
'0' -> 48

-charAT함수에 대해서-
String a = "apple"
system.out.println(a.charAt(0));
결과는 a

ー日本語ー

class Solution {
public boolean solution(String s) {
boolean answer = true;

if(s.length() != 4 && s.length() != 6)
answer=false;//長さが4や6でない場合=>false
for(int i=0; ifalse
if(s.charAt(i)>=「a」)//charAtは特定位置の文字を返還、「a」と同じか大きい場合false
answer = false;
return answer;
}
}

s。charAt(i)->stringを文字をchar型に変更する。
'a' -> 97
'A' -> 65
'0' -> 48

-charAT関数について-
String a = "apple"
system.out.println(a.charAt(0));
結果はa

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