정신과 시간의 방
카테고리
작성일
2024. 5. 11. 23:09
작성자
risehyun
  • 문제

 

 

  • 풀이
#include <iostream>
#include <string>

using namespace std;
int main()
{
	string A, B, C;
	int iResult = 0, cResult = 0;

	cin >> A >> B >> C;
	
	iResult = (stoi(A) + stoi(B)) - stoi(C);
	cResult = (stoi(A + B) - stoi(C));

	cout << iResult << '\n';
	cout << cResult;

	return 0;
}

 

  • 메모
    문자열을 int 정수형으로 바꿔주는 stoi 함수를 사용해서 덧셈과 뺄셈을 해준다.
    두번째 출력의 조건이 입력받은 값 A와 B를 + 한 값에 - C 를 해주는 것이었기 때문에
    문자열 상태인 A, B를 stoi(A + B)로 합쳐서 int로 변환해주고 c만큼 값을 빼주는 방식을 사용해 결과를 도출했다.

  • 시행착오 (오답)
    - 문제 조건을 꼼꼼하게 체크하지 않아서 단순히 값이 모두 1자리 수인 경우만 따져 아래와 같이 작성했고 당연히 오답이었다. 앞으로는 문제 조건을 천천히 읽고 풀어야겠다.
#include <iostream>

using namespace std;
int main()
{
	int A = 0, B = 0, C = 0;
	int iResult = 0;
	int cResult = 0;
	char str[3] = "";

	cin >> A >> B >> C;
	
	iResult = (A + B) - C;

	str[0] = (char)A;
	str[1] = (char)B;
	str[2] = (char)C;

	cResult = (int)(str[0]) * 10;
	cResult += (int)(str[1]);
	cResult -= (int)(str[2]);

	cout << iResult << '\n';
	cout << cResult;

	return 0;
}

'코딩테스트' 카테고리의 다른 글

[백준/2884번/C++] 알람시계  (0) 2024.05.13
[백준/2562번/C++] 최댓값  (0) 2024.05.12
[백준/11720번/C++] 숫자의 합  (0) 2024.05.10
[백준/2439번/C++] 별 찍기 - 2  (0) 2024.05.09
[백준/27866번/C++] 문자와 문자열  (0) 2024.05.08