1 개요[ | ]
- SWEA 1926 간단한 369게임
- SW Expert Academy 1926번 문제
- SWEA D2
- 자리수별로 3,6,9의 개수를 센다.
- 개수가 0이면 원래 숫자를 출력한다.
- 개수가 0이 아니면 그 개수만큼 -를 출력한다.
2 C++[ | ]
C++
Copy
.
#include <iostream>
using namespace std;
int N;
int count369(int n) {
int count = 0;
while(true) {
int digit = n % 10;
if( digit==3 || digit==6 || digit==9 ) count++;
n /= 10;
if( n == 0 )break;
}
return count;
}
int main() {
scanf("%d", &N);
for(int i=1; i<=N; i++) {
int count = count369(i);
if( count == 0 ) {
printf("%d ", i);
continue;
}
for(int j=0; j<count; j++) {
printf("-");
}
printf(" ");
}
}
3 Java[ | ]
Java
Copy
import java.util.Scanner;
import java.util.regex.*;
class Solution {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Pattern pattern = Pattern.compile("[369]");
String dashes;
Matcher matcher;
for(int i=1; i<=N; i++) {
matcher = pattern.matcher( String.valueOf(i) );
dashes = "";
while( matcher.find() ) dashes += "-";
if( dashes.equals("") ) System.out.format( "%d ", i );
else System.out.format( "%s ", dashes );
}
}
}
4 Python[ | ]
Python
Copy
#kcy
k = int(input())
cnt = 0
for i in range(1, k+1):
j = str(i)
if i < 10:
if j in ['3', '6', '9']:
cnt = cnt + 1
elif (i >= 10 and i < 100):
for y in range(0, 2):
if (j[y:y+1] in ['3', '6', '9']):
cnt = cnt + 1
else:
for y in range(0, 3):
if (j[y:y+1] in ['3', '6', '9']):
cnt = cnt + 1
if cnt > 0:
print('-'*cnt, end = ' ')
else:
print(i, end = ' ')
cnt = 0
Python
Copy
T = int(input())
for i in range(1, T+1):
s = sum(1 for x in str(i) if x in ['3','6','9'])
if s==0:
print( i, end=' ' )
else:
print( '-'*s, end=' ' )
편집자 Jmnote Kcy689 Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.