백준

백준 2908 C++

슬뷔 2024. 7. 24. 19:47

stoi (string to int)

int stoi(const string& str, size_t * idx = 0, int base = 10)

 

const string& str

  • 변경할 문자열
  • const는 함수 내부에서 변경이 일어나지 않음을 방지하기 위해서 붙여넣은 키워드
  • 복사의 비용이 들어가지 않도록 참조를 이용해서 보냄

size_t * idx

  • 맨 처음부터 마지막까지 포인터를 검사하게 되는데, 숫자가 아닌 문자가 나오게 되면 그 부분에서 저장
  • 예를 들어서 14seulbee하면, 포인터는 2에서 멈추게 되며 그 전까지는 숫자로 인식

int base = 10

  •  진수 부분. 디폴트는 10 (10진수를 의미)
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	string A, B;
	cin >> A >> B;

	reverse(A.begin(), A.end());
	reverse(B.begin(), B.end());

	int reversedA = stoi(A);
	int reversedB = stoi(B);

	// 삼항 연산자를 이용하여 큰 수 찾기
	// condition ? expression1 : expression2
	// ? 앞이 조건으로 조건이 참일 때, expression1 실행
	// 조건이 거짓일 때, expression2 실행
	int max = reversedA > reversedB ? reversedA : reversedB;

	cout << max << "\n";

	return 0;
}
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	string A, B;
	cin >> A >> B;

	string result;

	// 세자리 숫자이므로 2 ~ 0
	for (int i = 2; i >= 0; --i)
	{
		if (A[i] > B[i])
		{
			result = A;
			break;
		}

		else if (A[i] < B[i])
		{
			result = B;
			break;
		}
	}

	// 출력
	for (int i = 2; i >= 0; --i)
	{
		cout << result[i];
	}

	return 0;
}

'백준' 카테고리의 다른 글

백준 1152 C++  (1) 2024.07.24
백준 2675 C++  (1) 2024.07.24
백준 10809 C++  (0) 2024.07.24
백준 11720 C++  (2) 2024.07.23
백준 11654 C++  (0) 2024.07.23