백준 [단계별로 풀어보기 - 입출력과 사칙연산2]
10926 ??!
string을 사용해 간단하게 문자열 뒤에 문자를 추가로 입력하도록 작성한다.
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
cout << s << "??!";
return 0;
}
C++
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
string s;
s = Console.ReadLine();
Console.WriteLine(s + "??!");
}
}
C#
18108 1998년생인 내가 태국에서는 2541년생?!
불기연도는 현재년도 + 543년이다 따라서 그 해의 불기연도에서 서기 연도로 변경하려면 불기연도 - 543을 하면 된다.
#include <iostream>
using namespace std;
int main() {
int BangkokYear;
cin >> BangkokYear;
cout << BangkokYear - 543;
return 0;
}
C++
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
//18108 BackJoon
int BangkokYear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(BangkokYear - 543);
}
}
C#
10430번 나머지
단순 연산을 통한 결과값을 차례로 출력하는 것으로 별 다른 조건이 없기에 간단하게 풀 수 있다.
#include <iostream>
using namespace std;
int main() {
int A, B, C;
cin >> A >> B >> C;
cout << (A + B) % C << "\n";
cout << ((A % C) + (B % C)) % C << "\n";
cout << (A * B) % C << "\n";
cout << ((A % C) * (B % C) % C);
return 0;
}
C++
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
//10430 BackJoon
string inputData = Console.ReadLine();
string[] inputDatas = inputData.Split(' ');
int A = Convert.ToInt32(inputDatas[0]);
int B = Convert.ToInt32(inputDatas[1]);
int C = Convert.ToInt32(inputDatas[2]);
Console.WriteLine((A + B) % C);
Console.WriteLine(((A % C) + (B % C)) % C);
Console.WriteLine((A * B) % C);
Console.WriteLine(((A % C) * (B % C)) % C);
}
}
C#
2588번 곱셈
해당 문제는 서로 다른 방식으로 풀었다. C++은 수학적으로 접근했고 C#은 배열에 담겨있는 문자를 정수형으로 변환해주고 실제 손으로 연산하듯 자릿수값을 곱하여 풀었다.
#include <iostream>
using namespace std;
int main() {
int A = 0, B = 0;
cin >> A >> B;
cout << A * (B % 10) << "\n";
cout << A * ((B % 100) / 10) << "\n";
cout << A * (B / 100) << "\n";
cout << A * B;
return 0;
}
두번째 입력으로 들어오는 385 의 각 자릿수 값은 일의 자리 5의 경우 385 % 10 을 해주면 10으로 나눠준 값의 나머지를 구하니 5가 반환이 되며, 십의자리 8은 385 % 100 을 해주면 85 가 나오고 이걸 10으로 나눴을 때 8이 반환 된다.
백의자리 3은 385 / 100 을 하면 3이 나온다.
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
//2588 BackJoon
int A = Convert.ToInt32(Console.ReadLine());
string inputData = Console.ReadLine();
char[] inputChars = inputData.ToCharArray();
// 각 문자를 정수로 변환
int B = Convert.ToInt32(inputChars[0].ToString());
int C = Convert.ToInt32(inputChars[1].ToString());
int D = Convert.ToInt32(inputChars[2].ToString());
Console.WriteLine(A * D);
Console.WriteLine(A * C);
Console.WriteLine(A * B);
Console.WriteLine((A * D) + (A * C * 10) + (A * B * 100));
}
}
11382번 꼬마 정민
단순 입출력 문제이지만 조건이 있다. 10¹² x 3일 경우 int 자료형의 범위를 초과하기 때문에 int 보다 더 큰 범위인 long long으로 변수를 선언해야 한다.
#include <iostream>
using namespace std;
int main() {
long long A, B, C;
cin >> A >> B >> C;
cout << A + B + C;
return 0;
}
C++
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
//11382 BackJoon
string inputData = Console.ReadLine();
string[] inputDatas = inputData.Split(' ');
long A = Convert.ToInt64(inputDatas[0]);
long B = Convert.ToInt64(inputDatas[1]);
long C = Convert.ToInt64(inputDatas[2]);
Console.WriteLine(A + B + C);
}
}
C#
C#은 long long이 아닌 long을 사용한다.
long(64bit) : –9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
10171번 고양이
여러가지 방법으로 풀 수 있으나 C++ 11부터 사용 가능한 원시 리터럴 문자열(raw string literal)을 사용하여 출력하였다.
\이스케이프 시퀀스를 사용하지 않고 엔터만 누르면 줄 바꿈이 가능하다. 즉, 이스케이프시퀀스를 문자로 인식되기에 문자처럼 출력됩니다.
#include <iostream>
using namespace std;
int main() {
cout << R"(\ /\
) ( ')
( / )
\(__)|)";
return 0;
}
C#의 경우 @를 사용해 문자 그대로 출력하도록 동일한 방법으로 구현했다. 문자열에 @를 붙여줌으로써 이스케이프 문자를 그대로 출력한다.
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
//10171 BackJoon 고양이
Console.WriteLine(@"\ /\
) ( ')
( / )
\(__)|");
}
}
이 방법이 아니면 역슬래쉬를 출력하기 위해 \\로 작성하는 방법이 있다.
#include <iostream>
using namespace std;
int main() {
cout << "\\ /\\" << endl;
cout << " ) ( ')" << endl;
cout << "( / )" << endl;
cout << " \\(__)|";
return 0;
}
10172번 개
고양이와 동일한 방법으로 풀면 된다. 우선 원시 문자열을 사용하지 않는다면 역슬래시와 큰따옴표 앞에 각각 역슬래시를 붙여주어야 하고 마지막으로 줄 바꿈을 해주어야하니 각 줄의 마지막에 \n 으로 개행문자를 넣어준다.
#include <iostream>
using namespace std;
int main(int argc, char const* argv[]) {
cout << "|\\_/|\n";
cout << "|q p| /}\n";
cout << "( 0 )\"\"\"\\\n";
cout << "|\"^\"` |\n";
cout << "||_/=\\\\__|\n";
return 0;
}
C#은 @를 사용하여 간단하게 풀었다.
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(String[] args)
{
//10172 BackJoon 개 (이스케이프 시퀀스와 @ 기호)
Console.WriteLine(@"|\_/|
|q p| /}
( 0 )""""""\
|""^""` |
||_/=\\__|");
}
}
Reference
원시 문자 리터럴
https://learn.microsoft.com/ko-kr/cpp/cpp/string-and-character-literals-cpp?view=msvc-160
문자열 및 문자 리터럴(C++)
C++에서 문자열 및 문자 리터럴을 선언하고 정의하는 방법입니다.
learn.microsoft.com
https://todamfather.tistory.com/85
C++ 기초(Raw String Literal)
이번 포스팅에서는 조금은 생소하지만 스트링을 사용함에 있어서 편리함을 제공해 주는 로 스트링 리터럴에 대해서 정리했습니다. 인용구 사용 문자열 안에 인용구 같은 또 다른 문자열을 넣고
todamfather.tistory.com
c++11 원시 문자열 리터럴
c#에는 문자열에 @를 붙여주면 문자열 내의 이스케이프 문자를 그대로 인식한다 string dir = @"C:\hello\world"; Console.WriteLine(dir); //출력 : C:\hello\world c++에서는 이스케이프가 포함된 문자열을 표현하기
cpplove.tistory.com