C, C++/๐Ÿฅ‰ BOJ

(C++) โ˜…Basics I - 16 Solvedโ˜…

metamong 2024. 11. 13.

โ˜… 2557 Hello World โ˜…

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
}

โ˜… 1000 A + B โ˜… / โ˜… 1001 A - B โ˜… / โ˜… 10998 A x B โ˜…

#include <iostream>
using namespace std;
int main()
{
    int A, B;
    cin >> A;
    cin >> B;
    cout << A * B; // A - B, A + B
    return 0;
}

โ˜… 1008 A / B โ˜…

#include <iostream>
using namespace std;
int main()
{
    double A, B;
    cin >> A >> B;
    cout.precision(12);
    cout << fixed;
    cout << A / B;
    return 0;
}

 

๐Ÿค cout.precision(12)์œผ๋กœ ์‹ค์ˆ˜ ์ „์ฒด๋ฅผ 12์ž๋ฆฌ๋กœ ํ‘œํ˜„ํ•˜๊ฒ ๋‹ค๋Š” ๋œป.

๐Ÿค fixed ์จ์„œ 12์ž๋ฆฌ๋กœ ๊ณ ์ •ํ•œ ์ˆซ์ž๋กœ ์—ฐ์‚ฐ. fixed ์ดํ›„๋Š” 12์ž๋ฆฌ๋กœ ๊ณ ์ •ํ•œ ์ˆซ์ž๋กœ ์—ฐ์‚ฐ์„ ์ง„ํ–‰.


โ˜… 10869 ์‚ฌ์น™์—ฐ์‚ฐ โ˜…

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b << "\n"; 
    cout << a - b << "\n";
    cout << a * b << "\n";
    cout << a / b << "\n"; 
    cout << a % b << "\n"; 
    return 0;
}

 

๐Ÿค ๋ชซ์€ / ๊ธฐํ˜ธ, ๋‚˜๋จธ์ง€๋Š” % ๊ธฐํ˜ธ ์‚ฌ์šฉ. ๊ทธ๋ฆฌ๊ณ  endl ๋Œ€์‹  "\n"์„ ์‚ฌ์šฉํ•ด ๊ทธ ๋‹ค์Œ ์ค„ ์ถœ๋ ฅ ์ง„ํ–‰


โ˜… 10926 ??! โ˜…

//10926
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string id;
    cin >> id;
    id += "??!";
    cout << id;
    return 0;
}

 

๐Ÿค ๋ฌธ์ž์—ด์€ <string> library๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , string ๋ณ€์ˆ˜ id๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ž…๋ ฅ ํ›„, ??! ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ


โ˜… 18108 1998๋…„์ƒ์ธ ๋‚ด๊ฐ€ ํƒœ๊ตญ์—์„œ๋Š” 2541๋…„์ƒ?! โ˜…

//18108
#include <iostream>
#include <string>

using namespace std;
int main()
{
    int year;
    cin >> year;
    cout << year - 543 << '\n';
    return 0;
}

โ˜… 10430 ๋‚˜๋จธ์ง€ โ˜…

//10430
#include <iostream>
#include <string>

using namespace std;
int main()
{
    int A, B, C;
    cin >> A >> B >> C;
    cout << (A+B)%C << "\n" << ((A%C) + (B%C))%C << "\n" << (A*B)%C << "\n" << ((A%C)*(B%C))%C << "\n";
    return 0;
}

โ˜… 2588 ๊ณฑ์…ˆ โ˜…

//2588
#include <iostream>
#include <string>

using namespace std;
int main()
{
    int A;
    string B;
    cin >> A >> B ;
    
    cout << A * (B[2] - '0') << "\n";
    cout << A * (B[1] - '0') << "\n";
    cout << A * (B[0] - '0') << "\n";
    cout << A * stoi(B) << "\n";
    
    return 0;
}

 

๐Ÿค (ํ•œ ๊ฐœ์˜ ๋ฌธ์ž) B[2] - '0'์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฌธ์ž์— '0'์„ ๋นผ์„œ ์‹ค์ œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ / (๋ฌธ์ž์—ด) ์ง์ ‘ stoi() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉ


โ˜… 11382 ๊ผฌ๋งˆ ์ •๋ฏผ โ˜…

//11382
#include <iostream>
#include <string>

using namespace std;
int main()
{
    long long  A, B, C;
    cin >> A >> B >> C;
    
    cout << A + B + C << "\n";
    
    return 0;
}

 

๐Ÿค ๋งค์šฐ ํฐ ์ˆซ์ž๋Š” int๊ฐ€ ์•„๋‹ˆ๋ผ long long ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ์–ด์„œ ์ง„ํ–‰


โ˜… 10171 ๊ณ ์–‘์ด โ˜…

//10171
#include <iostream>
#include <string>

using namespace std;
int main(int argc, char const *argv[]) {
    cout << "\\    /\\\n";
    cout << " )  ( ')\n";
    cout << "(  /  )\n";
    cout << " \\(__)|\n";
    return 0;
}

 

๐Ÿค ์—ญ์Šฌ๋ž˜์‹œ(\)๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•ž์— ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ํ•œ ๋ฒˆ ๋” ๋ถ™์ด๋ฉด ๋œ๋‹ค.


โ˜… 10172 ๊ฐœ โ˜…

//10172

#include <iostream>
#include <string>

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;
}

 

๐Ÿค ์—ญ์Šฌ๋ž˜์‹œ + ํฐ๋”ฐ์˜ดํ‘œ + ์ž‘์€๋”ฐ์˜ดํ‘œ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•ž์— ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ํ•œ ๋ฒˆ ๋” ๋ถ™์ด๋ฉด ๋œ๋‹ค. 


โ˜… 11021 A + B - 7 โ˜…

//11021
#include <iostream>
#include <string>
using namespace std;

int main(){
    int T;
    cin >> T;

    for(int i = 0; i < T; i++) {
        int A, B;
        cin >> A >> B;
        cout << "Case #" << i+1 << ": " << A + B << '\n';
    }
}

โ˜… 11022 A + B - 8 โ˜…

//11022
#include <iostream>
#include <string>
using namespace std;

int main(){
    int T;
    cin >> T;

    for(int i = 0; i < T; i++) {
        int A, B;
        cin >> A >> B;
        cout << "Case #" << i+1 << ": " << A << " + " << B << " = " << A + B << '\n';
    }
}

โ˜… 15552 ๋น ๋ฅธ A+B โ˜…

#include <iostream>
#include <string>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N;
    cin >> N;
    
    for(int x = 0; x < N; x++){
        int A, B;
        cin >> A >> B;
        cout << A + B << '\n';
    }

}

 

๐Ÿค c++์—์„œ ๋น ๋ฅด๊ฒŒ ์ž…๋ ฅ๋ฐ›์œผ๋ ค๋ฉด cin.tie(NULL) / cout.tie(NULL) / ios_base::sync_with_stdio(false)๋ฅผ ๋‹ค ์ ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  buffer flush๋ฅผ ํ•œ ๋ฒˆ์— ์ง„ํ–‰ํ•ด์ฃผ๋Š” endl์„ ์“ฐ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๊ฐœํ–‰๋ฌธ์ž \n ์‚ฌ์šฉํ•ด์„œ ํšจ์œจ์ ์ด๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑ


 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€