0. C++ ์
์ถ๋ ฅ
2023. 1. 27. 00:12ใ๐ Algorithm/๐ C++ ๋ฌธ์ ํ์ด
๋ฐ์ํ
์ ๋ ฅ
1. std::cin ์ด์ฉํ๊ธฐ
๊ฐํ ๋ฌธ์ (๋์ด์ฐ๊ธฐ ์ํฐ) ๊น์ง๋ง ์ ๋ ฅ ๋ฐ๋๋ค
int main() {
std::string s;
std::cin >> s; // ์๋
ํ์ธ์ ํ์ด
std::cout << s << '\n'; // ์๋
ํ์ธ์
}
2. scanf ์ฌ์ฉํ๊ธฐ
์ํ๋ ํ์๋๋ก ์ ๋ ฅ์ ๋ฐ์ ์ ์๋ค.
#include <bits/stdc++.h>
using namespace std;
int a;
double b;
char c;
int main(){
scanf("%d %lf %c", &a, &b, &c);
printf("%d\n", a);
printf("%lf\n", b);
printf("%c\n", c);
return 0;
}
/*
์
๋ ฅ
23330
233.23123
a
์ถ๋ ฅ
23330
233.231230
a
*/
3. std::getline ์ฌ์ฉํ๊ธฐ
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
getline(cin, s);
cout << s << '\n';
return 0;
}
/*
์๋
ํ์ธ์ ํ์ด
์๋
ํ์ธ์ ํ์ด
*/
๋ง์ฝ ์ฌ๋ฌ์ค์ input์ ๋ฐ์์ผ ํ๋ค๋ฉด ?!
using namespace std;
int main() {
int T;
string s;
cin >> T;
string bufferflush;
getline(cin, bufferflush);
for (int i = 0; i < T; i++) {
getline(cin, s);
cout << s << '\n';
}
return 0;
}
์ถ๋ ฅ
1. std::cout ์ฌ์ฉํ๊ธฐ
์ํ๋ ์๋ฆฟ์๋งํผ์ ์ค์๋ฅผ ์ถ๋ ฅํ๊ณ ์ถ๋ค๋ฉด? cout.precision(์๋ฆฟ์)๋ฅผ ์ด์ฉํ์!
using namespace std;
double a = 1.23456789;
int main() {
cout << a << '\n'; // 1.23457
cout.precision(7);
cout << a << '\n'; // 1.234568
}
2. printf ์ฌ์ฉํ๊ธฐ
printf ์ถ๋ ฅ์ string์ ์ถ๋ ฅํ ๋ ์ฃผ์ํด์ผํ ์
printf('%s\n', str.c_str());
printf๋ก ์ถ๋ ฅํ๋ ๊ฒฝ์ฐ์๋ string์ ๋ฌธ์์ด ํฌ์ธํฐ(char *) ํ์ ์ผ๋ก ๋ฐ๊ฟ์ค์ผ ํ๋ค!
๋ฐ์ํ
'๐ Algorithm > ๐ C++ ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
C++ ๋์ ํฉ (0) | 2023.02.10 |
---|---|
C++ String ์๊ณ ๋ฆฌ์ฆ์์ ์์ฃผ ์ฐ์ด๋ ๋ฉ์๋ (0) | 2023.02.06 |
C++ ์์ด(permutation)๊ณผ ์กฐํฉ(combination) (0) | 2023.02.01 |