백준 10950

https://www.acmicpc.net/problem/10950
백준 코드 for
백준 코드 while
깃허브 코드 for
깃허브 코드 while

A+B - 3

브론즈 5


시간 제한 메모리 제한
1초 256MB

분류


  • 수학
  • 구현
  • 사칙연산

    문제


    두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

    입력


    첫째 줄에 테스트 케이스의 개수 T가 주어진다.

    각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)

    출력


    각 테스트 케이스마다 A+B를 출력한다.

    예제

    예제 입력 1


    5
    1 1
    2 3
    3 4
    9 8
    5 2
    

    예제 출력 1


    2
    5
    7
    17
    7
    

문제분석


어떤 작업이 원하는 횟수 만큼 반복적으로 실행되게 하는 문제
반복문 for나 while이용하고 횟수를 정하는 변수 입력받고 그 변수는 반복문을 그만두는 조건이 된다.

손 코딩


입출력 함수 해당하는 헤더파일 포함

메인함수
T 변수 선언 - 테스트 케이스 개수
A, B 변수 선언

T 입력
반복 T
 A, B 입력
 A+B 출력하고 줄을 바꿈

슈도코드


main
	T 변수 선언
	A, B 변수 선언
	
	scanf T
	for(T만큼 반복)
	{
		scanf A, B
		printf (A + B) + newline
	}

​ while

main
	T 변수 선언
	A, B 변수 선언
	
	scanf T
	while(T--)
	{
		scanf A, B
		printf (A + B) + newline
	}

코드


#include <cstdio>

int main()
{
	int nTestCases = 0;
	int nInput1 = 0, nInput2 = 0;
	int nResult = 0;
	
	scanf("%d", &nTestCases);
	for(int i = 0; i < nTestCases; i++)
	{
		scanf("%d %d", &nInput1, &nInput2);
		nResult = nInput1 + nInput2;
		printf("%d\n", nResult);
	}
	
	return 0;
}

while

#include <cstdio>

int main()
{
	int nTestCases = 0;
	int nInput1 = 0, nInput2 = 0;
	int nResult = 0;
	
	scanf("%d", &nTestCases);
	while(nTestCases--) // nTestCases가 0이 될 때까지 반복
	{
		scanf("%d %d", &nInput1, &nInput2);
		nResult = nInput1 + nInput2;
		printf("%d\n", nResult);
	}
	
	return 0;
}
PS식 코드
#include <cstdio>

int main()
{
	int t;
	int a, b;
	
	scanf("%d", &t);
	for(int i = 0; i < t; i++)
	{
		scanf("%d %d", &a, &b);
		printf("%d\n", a+b);
	}
	
	return 0;
}

while

#include <cstdio>

int main()
{
    int t;
    int a, b;

    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &a, &b);
        printf("%d\n", a+b);
    }
    return 0;
}

댓글남기기