U E D R , A S I H C RSS

Lotto/강소현

Difference between r1.5 and the current

@@ -2,7 +2,20 @@
||Problem||2245||User||talin0528||
||Memory||5060K||Time||266MS||
||Language||Java||Result||Accepted||
== Idea ==
[AnEasyProblem/강소현]과 비슷해서 복붙함.
 
(ex) k = 7일 때, 1 여섯개와 0 한개로 이루어진 이진수를 내림차순으로 정렬

1111110
1111101
1111011
1110111
1101111
1011111
0111111
 
k = 8이면 1 여섯개와 0 두개로 이루어진 이진수를 내림차순으로 정렬 ... k = 12까지 비슷하게 전개됨
== Source ==
{{{
import java.util.Scanner;


Status

Problem2245Usertalin0528
Memory5060KTime266MS
LanguageJavaResultAccepted

Idea

AnEasyProblem/강소현과 비슷해서 복붙함.

(ex) k = 7일 때, 1 여섯개와 0 한개로 이루어진 이진수를 내림차순으로 정렬

1111110
1111101
1111011
1110111
1101111
1011111
0111111

k = 8이면 1 여섯개와 0 두개로 이루어진 이진수를 내림차순으로 정렬 ... k = 12까지 비슷하게 전개됨

Source

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNextInt()){
			int k = sc.nextInt();
			if(k==0) System.exit(0);
			int[] S = new int [k];
			for(int i=0; i<k; i++)
				S[i] = sc.nextInt();
			printPossibleNum(S);
		}
	}
	private static void printPossibleNum(int[] S) {
		int num = (int) (Math.pow(2,S.length-6)-1);
		while(num < Math.pow(2, S.length)){
			int [] bin = decToBin(num);
			int count = 0;
			for(int i=0; i<S.length; i++){
				if(bin[S.length-i-1]==0){
					if(++count != 6)
						System.out.print(S[i]+" ");
					else
						System.out.println(S[i]);
				}
			}
			num = increase(bin);
		}
		System.out.println("");
	}
	private static int increase(int[] bin){
		int num = 0, count=0;
		while(num<bin.length-1){
			if(bin[num] == 1){
				if(bin[num+1] == 0){
					bin[num+1] = 1;
					bin[num] = 0;
					break;
				}else{
					bin[num] = 0;
					bin[count++] = 1;
				}
			}
			num++;
		}
		int result = 0;
		for(int k=0; k<bin.length;k++){
			result += bin[k]*Math.pow(2,k);
		}
		return result;
	}
	private static int[] decToBin(int i){
		int [] temp = new int[13];
		int num = 0;
		while(i>0){
			temp[num++] = i%2;
			i = i/2;
		}
		return temp;
	}
}

Trial and Error

  • Presentation Error - 다 출력하고 마지막에 엔터 하나 더 쳐야함.

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:40
Processing time 0.0312 sec