🧩PS/🥈Nomal
[C/C++] 백준 1373번 2진수 8진수
Cocoon_
2020. 4. 13. 02:37
반응형
<나의 CODE>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
int s;
int main(void)
{
int s;
char input[1000001];
scanf("%s", &input);
int len = strlen(input);
int part = int(len / 3);
if (input[0]-48 == 0) printf("0");
else
{
if (len % 3 == 1)
{
printf("1");
s = 1;
for (int i = 0; i < part; i++)
{
printf("%d", 4 * (input[s + 3 * i] - 48) + 2 * (input[s + 3 * i + 1] - 48) + (input[s + 3 * i + 2] - 48));
}
}
else if (len % 3 == 2)
{
printf("%d", 2 * (input[0] - 48) + input[1] - 48);
s = 2;
for (int i = 0; i < part; i++)
{
printf("%d", 4 * (input[s + 3 * i] - 48) + 2 * (input[s + 3 * i + 1] - 48) + (input[s + 3 * i + 2] - 48));
}
}
else
{
s = 0;
for (int i = 0; i < part; i++)
{
printf("%d", 4 * (input[s + 3 * i] - 48) + 2 * (input[s + 3 * i + 1] - 48) + (input[s + 3 * i + 2] - 48));
}
}
}
}
<좀 더 깔끔한 CODE>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s[1000001] ;
scanf("%s",&s);
int n = strlen(s);
if (n % 3 == 1)
printf("%d", s[0] - 48);
else if (n % 3 == 2)
printf("%d", (s[0] - 48) * 2 + (s[1] - 48));
for (int i = n % 3; i < n; i += 3)
printf("%d", (s[i] - 48) * 4 + (s[i + 1] - 48) * 2 + (s[i + 2] - 48));
printf("\n");
}
https://www.acmicpc.net/problem/1373
1373번: 2진수 8진수
첫째 줄에 2진수가 주어진다. 주어지는 수의 길이는 1,000,000을 넘지 않는다.
www.acmicpc.net
반응형