코딩 -/백준 알고리즘 해설
백준 알고리즘 단계별 문제풀이 3 . for문 , A+B - 7 (백준 11021)
심프슨정리
2021. 7. 6. 20:53
반응형
백준 알고리즘 문제의 단계별 문제의 3번. for문 파트입니다.
<출처 - 백준 알고리즘 문제 - 단계별 문제풀이 for 파트 7번 >
첫 번째 입력이 총 몇번 계산할지를 , 그리고 그 이후가 그 각각의 연산될 수에대해 입력이 들어옵니다.
C언어입니다.
#include<stdio.h>
int main(){
int a,b,c;
scanf("%d",&a);
for (int i=1;i<=a;i++){
scanf("%d %d",&b,&c);
printf("Case #%d: %d\n",i,b+c);
}
}
C++입니다.
#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a,b,n;
cin>>n;
for (int i=1;i<=n;i++){
cin>>a>>b;
cout<<"Case #"<<i<<": "<<a+b<<"\n";
}
}
python입니다
import sys
a=int(sys.stdin.readline())
for i in range(1,a+1):
b,c=input().split()
b= int(b)
c = int(c)
print("Case #{}: {}".format(i,b+c))
Java입니다.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int num = Integer.parseInt(br.readLine()); //총 입력받을 문자 수
StringTokenizer temp;
int a,b;
for(int i=1;i<=num;i++){
temp = new StringTokenizer(br.readLine());
a = Integer.parseInt(temp.nextToken());
b = Integer.parseInt(temp.nextToken());
bw.write("Case #"+i+": "+(a+b)+"\n");
}
bw.flush(); // 남아있는 데이터를 모두 출력
bw.close(); //닫음
}
}
이것으로 7번째 기본 반복문 문제인 A+B-7를 풀어보았습니다.
반응형