개발/알고리즘
1373_이진수팔진수
송디
2020. 11. 21. 11:33
이 문제는 2진법에서 10진법으로 10진법에서 8진법으로 변환하는 문제가 아니라, 2진법에서 8진법으로 바로 바꾸는 방법이다. 왜냐하면 이진법의 길이가 1,000,000이기 될 수 있기 때문에 10진법으로 변환하면 큰일난다.
바로 바꾸는 방법은 진법 만큼 2^n 진법 만큼의 개수로 이동해주면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<iostream> #include<vector> #include<algorithm> #include<math.h> using namespace std; int main(void){ vector<char> nums; vector<int> res; string n; int ans, i; cin >> n; for(char c : n) nums.push_back(c); reverse(nums.begin(), nums.end()); ans = 0; i = 0; for(char o : nums){ ans += (o - '0') * pow(2, i); i++; if(i == 3) {res.push_back(ans); i = 0;ans = 0;} } if(ans != 0) res.push_back(ans); reverse(res.begin(), res.end()); for(int number : res) cout << number; } | cs |
728x90