728x90
๋ฐ์ํ
์์ํ ๋ฌธ์ ๋ค์ด ๋ง์์
๋ชฝ๋ ์ ๋ฆฌํด๋ณด์๋ค.
๐ 8393 ํฉ
์์์ ์ฌ์ฉํด์ ํ์๋ค.
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println( n*(n+1)/2 );
}
}
for๋ฌธ์ผ๋ก๋ ํ์ด๋ดค๋๋ฐ
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println(sum);
}
}
๐ 25314 ์์์ฆ
์ฒซ์งธ ์ค์๋ ์์์ฆ์ ์ ํ ์ด ๊ธ์ก ๊ฐ ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์๋ ์์์ฆ์ ์ ํ ๊ตฌ๋งคํ ๋ฌผ๊ฑด์ ์ข ๋ฅ์ ์ ์ด ์ฃผ์ด์ง๋ค. ์ดํ N ๊ฐ์ ์ค์๋ ๊ฐ ๋ฌผ๊ฑด์ ๊ฐ๊ฒฉ a์ ๊ฐ์ b๊ฐ ๊ณต๋ฐฑ์ ์ฌ์ด์ ๋๊ณ ์ฃผ์ด์ง๋ค. |
์ด 2+N๊ฐ์ ์ค์ ์ ๋ ฅ๋ฐ์์ผํจ.
์ค์บ๋๋ ๋ฒํผ๊ฐ ์ด์ง ๋ฌ๋ผ์ ๋๋ค ์ฌ์ฉํด๋ณด์๋ค.
์ค์บ๋
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int X = scanner.nextInt();
int N = scanner.nextInt();
int sum = 0;
for (int i = 0; i < N; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
sum += a * b;
}
if (sum == X){
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
๋ฒํผ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int X = Integer.parseInt(br.readLine());
int N = Integer.parseInt(br.readLine());
int sum = 0;
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
sum += a * b;
}
if (sum == X){
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
๐ 25314 ์ฝ๋ฉ์ ์ฒด์ก๊ณผ๋ชฉ์ ๋๋ค
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));
int N = Integer.parseInt(br.readLine());
int n = N/4;
String str = "long";
for (int i = 0; i < n-1; i++) {
str += " long";
}
System.out.println(str + " int");
}
}
๐ 2439 ๋ณ์ฐ๊ธฐ-2 (140ms -> 136ms)
์ค๋ฅธ์ชฝ์ผ๋ก ์ ๋ ฌํด์ค์ผํ๋ค.
๊ทธ๋์ ๋ฌธ์์ดํฌ๋งทํ ์ ์ฌ์ฉํด์ ๊ณต๋ฐฑ์ ์ถ๊ฐํด์ฃผ์๋ค.
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));
int N = Integer.parseInt(br.readLine());
String str ="";
for (int i = 0; i < N; i++) {
str += "*";
System.out.println(String.format("%"+N+"s",str));
}
}
}
์ฑ๋ฅ ๊ฐ์ ์ ์ํด ์ถ๊ฐ๋ก StringBuilder๋ ์ฌ์ฉํด๋ณด์๋ค.(136ms)
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));
int N = Integer.parseInt(br.readLine());
StringBuilder str = new StringBuilder();
for (int i = 0; i < N; i++) {
str.append("*");
System.out.println(String.format("%" + N + "s", str));
}
}
}
728x90
๋ฐ์ํ