๋ฐฑ์ค 2309
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int n = 9, k = 7;
int a[9];
vector<int> result(7);
void combi(int start, vector<int> b) {
if (b.size() == k) {
if (accumulate(b.begin(), b.end(), 0) == 100) {
copy(b.begin(), b.end(), result.begin());
}
return;
}
for (int i = start + 1; i < n; i++) {
b.push_back(a[i]);
combi(i, b);
b.pop_back();
}
}
int main() {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b;
combi(-1, b);
sort(result.begin(), result.end());
for (int i : result) cout << i << "\n";
return 0;
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ์์ด์ ์ฌ์ฉํ์ฌ ๋ ์ฝ๊ฒ ๊ตฌํ ์๋ ์์๋ค. next_permutation ์ฌ์ฉ
2๏ธโฃ 9๊ฐ ์ค 7๊ฐ๋ฅผ ๋ฝ๋ ๊ฒ๋ณด๋จ, 9๊ฐ ์ค 2๊ฐ๋ฅผ ๋ฝ๋ ํธ์ด ๋ ๋ซ๋ค. ์ค์ฒฉ for๋ฌธ์ ํตํด ํด๊ฒฐํ ์๋ ์๋ค.
3๏ธโฃ ์์ด ์์ฑ ๋ฐฉ๋ฒ(next_permutation, ์ฌ๊ทํจ์), ์กฐํฉ(์ฌ๊ทํจ์) ์์ฑ ๋ฐฉ๋ฒ์ ๋จธ๋ฆฟ์์ ๋ฐ์๋์.
๋ฐฑ์ค 10808
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int a[26] = {0, };
cin >> s;
for(char c : s) {
a[(int)c-97] += 1;
}
for(int num : a) {
cout << num << " ";
}
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ C++์์ char๋ฅผ int๋ก ๋ณํ์ ์ํด (int)๋ฅผ ์จ๋ ๋๋ค.
2๏ธโฃ counting star๋ map ๋๋ ๋ฐฐ์ด์ ์ฌ์ฉํ์.
3๏ธโฃ String ๊ธฐ๋ฐ์ด๋ฉด map์ ์ฌ์ฉํ๊ณ , Int ๊ธฐ๋ฐ์ด๋ฉด Array๋ฅผ ์ฌ์ฉํ๋ ํธ์ด๋ค.
4๏ธโฃ A๋ 65, a๋ 97๋ง ์ธ์๋์.
5๏ธโฃ ์ซ์ ๋์ 'a'๋ฅผ ๋ฃ์ด๋ ๋๋ค.
๋ฐฑ์ค 2979
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
int max = 0;
int min = 101;
cin >> a >> b >> c;
map<int, int> mp;
int result = 0;
for(int i=0; i<3; i++) {
int start, end;
cin >> start >> end;
mp[start] = end;
if (max <= end) max = end;
if (min >= start) min = start;
}
for(int i=min; i<=max-1; i++) {
int cars = 0;
for(auto it : mp) {
if (it.first <= i && it.second - 1 >= i) {
cars++;
}
}
if (cars == 1) {
result += a;
cout << "car๊ฐ 1์ธ ๊ฒฝ์ฐ" << "\n";
cout << i << "\n";
} else if (cars == 2) {
result += b*2;
cout << "car๊ฐ 2์ธ ๊ฒฝ์ฐ" << "\n";
cout << i << "\n";
} else if (cars == 3) {
result += c*3;
cout << "car๊ฐ 3์ธ ๊ฒฝ์ฐ" << "\n";
cout << i << "\n";
}
}
cout << result << "\n";
return 0;
}
์ด๋ ๊ฒ ํธ๋ ํ๋ ธ๋ค๊ณ ๋จ๋๋ผ.. ์กฐ๊ธ๋ง ๋ฐ๊พธ๋ฉด ๋ ๊ฒ ๊ฐ๊ธดํ๋ฐ, ๊ทธ ๋ณด๋ค counting ๋ฐฐ์ด์ ์ฐ๋ ํธ์ด ํจ์ ํธํด๋ณด์ด๊ธดํ๋ค.
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ์ด๋ฌํ ๋ฌธ์ ๋ counting ๋ฐฐ์ด์ ์๊ฐํ์ด์ผ ํ๋ค.
2๏ธโฃ์๊ฐ์ ์ด์/๋ฏธ๋ง์ผ๋ก ์๊ฐํ๋ ๊ฒ์ด ์ข๋ค.
๐ฒ ์ฌ๋ฐ๋ฅธ ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
int start, end;
int cnt[101] = { 0, };
int ret = 0;
cin >> a >> b >> c;
for(int i=0; i<3; i++) {
cin >> start >> end;
for(int j=start; j<end; j++) cnt[j]++;
}
for(int j=1; j<100; j++) {
if(cnt[j]) {
if (cnt[j] == 1) ret += a;
else if (cnt[j] == 2) ret += 2*b;
else if (cnt[j] == 3) ret += 3*c;
}
}
cout << ret << "\n";
return 0;
}
๋ฐฑ์ค 10988
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
bool isP = 1;
cin >> s;
for(int i=0; i<s.length(); i++) {
if (s[i] != s[s.length() - i - 1]) {
isP = 0;
}
}
cout << isP << "\n";
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ reverse()๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํด์ ํ์ด๋ ๋๋ค. ๋ค์ง์ด์ ๊ฐ์ผ๋ฉด 1์ถ๋ ฅ, ๋ค๋ฅด๋ฉด 0์ถ๋ ฅ์ ํ๋ฉด๋๋ค.
๋ฐฑ์ค 1159
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int arr[26] = { 0, };
string result = "";
cin >> n;
for(int i=0; i<n; i++) {
string s;
cin >> s;
arr[s[0] - 'a'] ++;
}
for(int i=0; i<26; i++) {
if (arr[i] >= 5) {
char c = char(i+97);
result += c;
}
}
if (!result.size()) cout << "PREDAJA";
else cout << result;
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ char(i + 97)๋ ๋์ง๋ง, ๊ทธ๋ฅ i + 97ํ๊ณ ์์ ํ์ ๋ง char๋ string์ด๋ฉด ์๋ ์บ์คํ ๋๋ค.
๋ฐฑ์ค 11655
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, ret;
getline(cin, s);
for(char c : s) {
if (c >= 'A' && c <= 'Z') {
if (char(c + 13) > 'Z') {
ret += char(((int)c + 13) - 'Z' + 64);
} else {
ret += char(c+13);
}
} else if (c >= 'a' && c <= 'z') {
if (char(c+13) > 'z' || c+13 > 126) {
ret += char(((int)c + 13) - 'z' + 96);
} else {
ret += char(c+13);
}
} else {
ret += c;
}
}
cout << ret << "\n";
return 0;
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ์ ๋งํ๋ฉด ์ซ์๋ก ๋น๊ตํ์. ์์คํค ์ฝ๋ ์ง์์ 126๊น์ง ๋ฐ์ ์๋์ ๋ฌธ์๋ก ๋น๊ตํ ์, ์ค๋ฅ๊ฐ ๋ ์ ์๋ค. ์ฆ, ์ซ์๋ก ๋น๊ตํ๋ ๋ก์ง์ ์ง์.
2๏ธโฃ ๊ทธ๋ฅ c + 13 - 26์ ํด๋ ๋์๋ค.
๋ฐฑ์ค 9996
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string& input, string delimeter) {
vector<string> result;
auto start = 0;
auto end = input.find(delimeter);
while (end != string::npos) {
result.push_back(input.substr(start, end - start));
start = end + delimeter.size();
end = input.find(delimeter, start);
}
result.push_back(input.substr(start));
return result;
}
int main() {
int n;
cin >> n;
string hint;
cin >> hint;
vector<string> lists = split(hint, "*");
for (int i = 0; i < n; i++) {
string s;
string isP = "DA";
cin >> s;
if (lists[0].size() + lists[1].size() > s.size()) {
isP = "NE";
} else {
for (int j = 0; j < lists[0].size(); j++) {
if (s[j] != lists[0][j]) isP = "NE";
}
for (int j = 0; j < lists[1].size(); j++) {
if (s[s.size() - 1 - j] != lists[1][lists[1].size() - 1 - j])
isP = "NE";
}
}
cout << isP << "\n";
}
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ์ด ๋ฌธ์ ๋ ๋ฐ๋ก ์ฐพ๊ธฐ๊ฐ ์ด๋ ต๋ค. ๋ค์ด์ค๋ ๋ฌธ์์ด์ ์ฌ์ด์ฆ๋ฅผ ์ฒดํฌํด์ผํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ํญ์ ์๊ฐํ๋ฉฐ ํ์.
2๏ธโฃ split ํจ์ ๋์ ์ find์ substr๋ง ์ฌ์ฉํด๋ ์ข๋ค. ์ด์งํผ ํ ๋ถ๋ถ ๋ฐ์ splitํ์ง ์๊ธฐ ๋๋ฌธ์ด๋ค.
3๏ธโฃ for๋ฌธ์ผ๋ก ๋ฌธ์ ํ๋ํ๋ ๋น๊ต x -> substr์ ํตํด์ ํ๋ฒ์ ๋น๊ต๊ฐ ๊ฐ๋ฅํ๋ค.
๋ฐฑ์ค 2259
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int maxInfo = -101;
vector<int> arr;
int n, k;
void getMax() {
auto start=arr.begin();
auto end = start + k;
while(end != arr.end() + 1) {
int temp = accumulate(start, end, 0);
if (temp >= maxInfo) {
maxInfo = temp;
}
start++;
end++;
}
}
int main() {
cin >> n >> k;
for(int i=0; i<n; i++) {
int value;
cin >> value;
arr.push_back(value);
}
getMax();
cout << maxInfo << "\n";
}
์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค.
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ๊ตฌ๊ฐ ํฉ ๋ฌธ์ ๋ผ๋ ๊ฒ์ ์์์ฐจ๋ ค์ผ ํ๋ค.
2๏ธโฃ ์ด ๋ฌธ์ ์ ์ต์ ๊ฐ์ -1,000,0000์ด๋ผ๋ ๊ฒ์ ์์์ผ ํ๋ค. ์ค์ ๋ก๋ ์ด ์๋ณด๋ค ์ด์ง ์์(4์ ๋) ๊ฐ์ ๋ ๋นผ์ฃผ๋ ๊ฒ์ด ์ข๋ค
3๏ธโฃ ์ฑ๋ฅ ํฅ์์ ์ํด ์๋ cin, cout ์ ์๋ ๊ตฌ๋ฌธ์ ์จ์ฃผ์.
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
c++๋ง์ ๋ ๋ฆฝ์ ์ด ๋ฒํผ๋ฅผ ์์ฑํ๊ฒ ํ์ฌ ์คํ ์๋๊ฐ ๋นจ๋ผ์ง๋ค.
4๏ธโฃ ๊ตฌ๊ฐ ํฉ ๊ณต์์ ์๋์ ๊ฐ๋ค.
for(int i=1; i<=n; i++) {
cin >> temp; psum[i] = psum[i-1] + temp;
}
for(int i=k; i<=n; i++) {
ret = max(ret, psum[i] - psum[i-k]);
}
psum[i] - psum[i-k]๋ฅผ ํด๋ณด๋ฉด, k๊ฐ 2์ผ๋, psum[2] - psum[0], psum[3] - psum[1] ... ๊ณผ ๊ฐ์ด ๊ตฌ๊ฐ ๋ง๋ค์ ํฉ์ ๊ตฌํ ์ ์๋ค.
๐ฒ ์ฌ๋ฐ๋ฅธ ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
int n, k, temp, psum[100001], ret = -10000004;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for(int i=1; i<=n; i++) {
cin >> temp; psum[i] = psum[i-1] + temp;
}
for(int i=k; i<=n; i++) {
ret = max(ret, psum[i] - psum[i-k]);
}
cout << ret << "\n";
return 0;
}
๋ฐฑ์ค 1620
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, m;
map<string, int> lists;
cin >> n >> m;
for(int i=0; i<n; i++) {
string temp;
cin >> temp;
lists[temp] = i+1;
}
for(int i=0; i<m; i++) {
string input;
cin >> input;
if (atoi(input.c_str()) != 0) {
int number = atoi(input.c_str());
for(auto value : lists) {
if (value.second == number) {
cout << value.first << "\n";
}
}
} else {
cout << lists.at(input) << "\n";
}
}
}
์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค.
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ map์ ์ฌ์ฉํ ๋, key๋ฅผ ํตํด value๋ฅผ ์ฐพ๋๋ฐ๋ O(logN)๋งํผ ๊ฑธ๋ฆฌ๋, value๋ฅผ ํตํด key๋ฅผ ์ฐพ์ ๋๋ ํจ์ฌ ๋ง์ด ๊ฑธ๋ฆฐ๋ค.
2๏ธโฃ ์ด๋ฅผ ๋ณด์ํ๊ธฐ ์ํด์, map ์๋ฃ๊ตฌ์กฐ๋ฅผ 2๊ฐ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค. string-int, int-string ์กฐํฉ์ผ๋ก ๋๊ฐ๋ฅผ ๋ง๋ค๋ฉด, ์งง์ ์๊ฐ ๋ณต์ก๋ ์ด๋ด์ key์ value์ ํ์์ด ๊ฐ๋ฅํ๋ค.
3๏ธโฃ atoi(string.c_str())์ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด์ด ์ซ์๋ผ๋ฉด ํด๋น ์ซ์๋ฅผ ๋ด๋๊ณ , ๋ฌธ์๋ผ๋ฉด 0์ ๋ด๋๋๋ค.
๋ฐฑ์ค 9375
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
for(int i=0; i<n; i++) {
int m;
cin >> m;
map<string, int> mp;
for(int j=0; j<m; j++) {
string s, t;
cin >> s >> t;
mp[t] += 1;
}
int result = 1;
for(auto j : mp) {
result *= (j.second + 1);
}
result -= 1;
cout << result << "\n";
}
return 0;
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ๊ฒฝ์ฐ์ ์ ๋ฌธ์ ๋ ์ผ์ผ์ด ์ธ๋ ๋ฐฉ์๋ ์์ง๋ง, ์ ์ฒด ๊ฒฝ์ฐ์ ์์์ ํน์ ๊ฒฝ์ฐ์ ์๋ฅผ ๋นผ๋ ๋ฐฉ์๋ ์๊ฐํ ์ ์๋ค.
2๏ธโฃ map์์ int์ ์ด๊ธฐํ ๊ฐ์ 0์ด๋ค.
๋ฐฑ์ค 1213
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
map<char, int> mp;
for (char c : s) {
mp[c]++;
}
int even_num = 0;
for (auto item : mp) {
if (s.length() % 2 == 0 && item.second % 2 == 1) {
cout << "I'm Sorry Hansoo"
<< "\n";
exit(0);
}
if (s.length() % 2 == 1) {
if (item.second % 2 == 1) {
even_num++;
}
}
}
if (even_num != 1 && s.length() % 2 == 1) {
cout << "I'm Sorry Hansoo"
<< "\n";
exit(0);
}
string temp = "";
string result = "";
if (s.length() % 2 == 0) {
for (auto item : mp) {
for (int i = 0; i < item.second / 2; i++) {
temp += item.first;
}
}
result += temp;
reverse(temp.begin(), temp.end());
result += temp;
} else {
string middle;
for (auto item : mp) {
if (item.second % 2 == 1) {
item.second--;
middle = item.first;
}
for (int i = 0; i < item.second / 2; i++) {
temp += item.first;
}
}
result += temp;
result += middle;
reverse(temp.begin(), temp.end());
result += temp;
}
cout << result << "\n";
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ string์ reverseํ๊ธฐ ์ํด์, reverse(s.begin(), s.end())๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
2๏ธโฃ ํ์ ์ฒดํฌ๋ cnt[i] & 1๊ณผ ๊ฐ์ ์์ผ๋ก๋ ๊ฐ๋ฅํ๋ค.
3๏ธโฃreturn string์ ๋ง๋ค๊ณ , for๋ฌธ์ผ๋ก ์ ์์ ์ถ๊ฐํ๋ ๋ฐฉ์์ผ๋ก ์ง๋๋๋ค. +=2๋ฅผ ํ๋ฉด ์ฝ๋ ์๋ ์ค์ผ ์ ์๋ค.
4๏ธโฃํ์์ผ ๋, mid๊ฐ์ insertํ๋ ์ฒ๋ฆฌ๋ก ๊ฐ๋ ๊ฒ์ด ์กฐ๊ธ ๋ ํธํ๋ค.
๐ฒ ๊ฐ์ ๋ ์ฝ๋
#include<bits/stdc++.h>
using namespace std;
string s, ret;
int cnt[200], flag;
char mid;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
for(char a : s)cnt[a]++;
for(int i = 'Z'; i >= 'A'; i--){
if(cnt[i]){
if(cnt[i] & 1){
mid = char(i);flag++;
cnt[i]--;
}
if(flag == 2)break;
for(int j = 0; j < cnt[i]; j += 2){
ret = char(i) + ret;
ret += char(i);
}
}
}
if(mid)ret.insert(ret.begin() + ret.size() / 2, mid);
if(flag == 2)cout << "I'm Sorry Hansoo\n";
else cout << ret << "\n";
}
๋ฐฑ์ค 1940
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int n, m;
int result = 0;
int arr[15001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cin >> m;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
arr[i] = a;
}
for(int i=0; i<n; i++) {
for(int j=0; j<i; j++) {
if (arr[i] + arr[j] == m) {
result++;
}
}
}
cout << result << "\n";
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ์ฒ์์๋ ์กฐํฉ์ ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํ์๋ค. ๊ทธ๋ฌ์ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฌ๊ณ , ์ด๋ ์คํ์ ํจ์๊ฐ ์์ฌ ๋ฐ์ํจ์ ์๊ฒ๋์๋ค.
2๏ธโฃ ๊ทธ๋์ ์ค์ฒฉ for๋ฌธ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ฅผ ๋ง์๋ค.
3๏ธโฃ ๋ง์ฝ ์๊ฐ ์ด๊ณผ๊ฐ ๋นก์ธ๋ค๋ฉด, ์๋๋ ๊ฒฝ์ฐ๋ if๋ฌธ์ผ๋ก ์ฒดํฌํด์ผ ํ๋ค.
if(m > 200000) cout << "0" << "\n";
๋ฐฑ์ค 3986
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
string s;
int ret=0;
cin >> n;
for(int i=0; i<n; i++) {
cin >> s;
stack<char> stk;
for(char a : s) {
if (stk.size() && stk.top() == a) stk.pop();
else stk.push(a);
}
if (stk.size() == 0) ret++;
}
cout << ret << "\n";
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ๋ฌธ์ ๋ฅผ ๋ณผ ๋ ์ดํด๊ฐ ์๋๋ค๋ฉด, ๋์ํํ๊ฑฐ๋ ๊ทธ๋ฆผ์ ๊ทธ๋ฆฐ๋ค. 90๋ ํ์ , ๋ค์ง์ด ๋ณด๊ธฐ, ๋ถ์ฌ๋ณด๊ธฐ
2๏ธโฃ ์คํ์ ์ฌ์ฉํ๋ค. ํญ๋ฐ์ ์ผ์ผํค์..!
3๏ธโฃ ๋ฌธ์ ์์ ์ง์ง๊ธฐ, ํญ๋ฐ์ด๋ผ๋ ๋จ์ด๊ฐ ์๋ค๋ฉด ์คํ์ ์๊ฐํด๋ณด์.
๋ฐฑ์ค 1629
๐ฒ ๊ฐ์ ๋ ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a, b, c;
ll go(ll a, ll b) {
if (b == 1) {
return a % c;
}
ll ret = go(a, b / 2);
ret = (ret * ret) % c;
if (b % 2) ret = (ret * a) % c;
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> a >> b >> c;
cout << go(a, b) << "\n";
return 0;
}
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ๊นก์ผ๋ก ๊ณฑํ๋ ๊ฒ๋ณด๋จ, ์ฌ๊ท์ ์ผ๋ก ๋ค์ด๊ฐ๋ฉด ๊ณฑํ๋ ๊ฒ์ด ์๊ฐ ๋ณต์ก๋๊ฐ ๋ ์ค์ด๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. Log ์๊ฐ์ผ๋ก ์ค์ด๋ค๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฅผ๋ค์ด, ๋จ์ํ a๋ฅผ ์ฌ๋ฌ๋ฒ ๊ณฑํ๋ ๊ฒ์ด ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ, ๋ก๊ทธ์๊ฐ์ผ๋ก ์ค์ด๋ ๋ค.
2๏ธโฃ (a + b) % c = a % c + b % c, (a * b) % c = a % c * b % c, ๊ฐ์ด overflow๊ฐ ๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํจ. ์ ์๋ก ์ ์ผ์ข
3๏ธโฃ ์ ์๋ก ์ ๋ชจ๋ ์ฐ์ฐ์ ํตํด overflow ๋ฐฉ์ง, ์ฌ๊ทํจ์๋ฅผ ํตํ ๊ณฑํ๊ธฐ์ ์๊ฐ ๋ณต์ก๋ ์ค์ด๊ธฐ
๋ฐฑ์ค 4375
๐ฒ ๋ด ํ์ด
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
ll n;
while(cin >> n) {
ll num = 1;
bool is_not_done = true;
int result = 1;
while(is_not_done) {
if (num % n == 0) {
is_not_done = false;
cout << result << "\n";
} else {
string new_num = to_string(num);
new_num += "1";
result++;
num = stoll(new_num);
}
}
}
return 0;
}
๋ฐํ์ ์๋ฌ๊ฐ ๋ฌ๋ค.
๊ฒฐ๊ตญ long long๋ ๋ฒ์ด๋๋ ์ซ์๊ฐ ๋ง๋ค์ด์ง๋ฏ๋ก, ๋ชจ๋ ์ฐ์ฐ์ ํตํด overflow๋ฅผ ๋ฐฉ์งํ์ฌ์ผ ํ๋ค.
๐ฒ ๋ณด์ํ ์ & ์๊ฒ๋ ์
1๏ธโฃ ์ ์๋ก ์ ๋ชจ๋์ฐ์ฐ์ ํตํด์ % ์ฐ์ฐ์ ๋ถ์ฐํ ์์ผ์ฃผ์ด์ผ ํ๋ค.
๐ฒ ๊ฐ์ ๋ ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
ll n;
while(cin >> n) {
ll num = 1;
bool is_not_done = true;
int result = 1;
while(is_not_done) {
if (num % n == 0) {
is_not_done = false;
cout << result << "\n";
} else {
num = (num * 10) + 1;
num %= n;
result++;
}
}
}
return 0;
}
10์ฃผ์์ฑ C++ ์ฝ๋ฉํ ์คํธ | ์๊ณ ๋ฆฌ์ฆ ์ฝ๋ฉํ ์คํธ ๊ฐ์ - ์ธํ๋ฐ
๋ค์ด๋ฒ, ์นด์นด์ค, ์ผ์ฑ์ ์ฝ๋ฉํ ์คํธ๋ฅผ 10์ฃผ๋ง์ ํฉ๊ฒฉ์ํจ ์ต๊ณ ์ ์ฝ๋ฉํ ์คํธ ๊ฐ์!, ์ฝ๋ฉํ ์คํธ, ์ด์ ๊ฒ์ฆ๋ 10์ฃผ ์์ฑ ์ปค๋ฆฌํ๋ผ์ผ๋ก ์ ๋ณตํ์!๐ [์ฌ์ง] ์ฝ๋ฉํ ์คํธ ๊ฐ์์ด๋ค ๊ฒ์ ๊ณจ๋ผ์ผ ํ ๊น
www.inflearn.com