본문 바로가기
알고리즘/백준 BOJ

[BOJ C#, C++ ] 1000 A+B, 1001 A-B, 10998 AxB

by 왹져박사 2023. 7. 11.
728x90

백준을 프로젝트 기간동안 하지 못해서 어떻게 기본 폼을 만들었는지 조차 가물가물했다. 

연습하며 왕초보 문제들부터 최소 하루에 1~2개씩은 풀 예정이다. 

 

왕초보부터 풀어가며 c++로 같이 연습 할 예정이다. 

 

+) c++로 풀이한 답을 보며 공부하던 중에, 한 블로그에서 stdio.h와 iostream확장자의 성능을 각각 비교하는 글이 있어 궁금하여 찾아보았다. 

2023.07.11 - [분류 전체보기] - [ C++ ] stdio.h와 iostream의 차이

 

[ C++ ] stdio.h와 iostream의 차이

백준을 위해 C#에서 C++로 넘어가며 답을 비교하며 공부하던 중에, 한 블로그에 stdio.h와 iostream 헤더파일의 성능을 비교하는 글을 보았다. 처음 C++을 공부하였기 때문에 어떤 확장자를 써야하나

narmhye.tistory.com

 

1000) A+B

C#

더 간단하게 하는 방법도 있지만, 자주 사용했던 형식을 익히기 위해 연습하며 작성하였다. 

using System;
using System.IO;

namespace _1000
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            string[] arrStr = sr.ReadLine().Split(' ');

            int a = int.Parse(arrStr[0]);
            int b = int.Parse(arrStr[1]);

            int answer = a + b;

            sw.WriteLine(answer);

            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

 

C++

#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	int a;
	int b;

	cin >> a;
	cin >> b;
	//cin >> a >> b;
	cout << a + b;

	return 0;
}

c++에서의 cin은 입력, cout는 출력이라고 한다.

<<, >>는 입출력 연산자 

위의 주석처리한 부분과 같이 cin에 변수를 동시에 입력할 수 있다. (연산자 오버로딩)

이는 각 자료형에 연산자 오버로딩이 구현되어 있으면 가능하다. 

 

 

1000) A-B

C++

앞의 문제와 크게 다를 게 없어 글에 추가하였다.

이번에는 연산자 오버로딩을 사용하였다. 

#include <iostream>

using namespace std;

int main(int argc, char const* argv[])
{
	int a;
	int b;

	cin >> a >> b;
	cout << a - b;

	return 0;
}

 

10998) AxB

C++

#include <iostream>

using namespace std;

int main(int argc, char const* argv[])
{
	int a;
	int b;

	cin >> a >> b;
	cout << a * b;

	return 0;
}

728x90