Kim Schrijvers
Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either 0 or 1.
This set of strings is interesting because it is ordered and contains all possible strings of length N that have L (1 <= L <= N) or fewer bits that are `1'.
Your task is to read a number I (1 <= I <= sizeof(S)) from the input and print the Ith element of the ordered set for N bits with no more than L bits that are `1'.
PROGRAM NAME: kimbits
INPUT FORMAT
A single line with three space separated integers: N, L, and I.SAMPLE INPUT (file kimbits.in)
5 3 19
OUTPUT FORMAT
A single line containing the integer that represents the Ith element from the order set, as described.SAMPLE OUTPUT (file kimbits.out)
10011
/*
ID: conicoc1
LANG: C
TASK: kimbits
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
FILE *fin,*fout;
int Length,One;
unsigned long long int Ith;
int A[32][32];
char str[31];
int main()
{
fin=fopen("kimbits.in","r");
fout=fopen("kimbits.out","w");
int i,j,sum,temp,OneCount;
fscanf(fin,"%d %d %lld",&Length,&One,&Ith);
for(i=0;i<=Length;i++){
A[i][0]=1;
A[0][i]=1;
}
for(i=1;i<=Length;i++){
for(j=1;j<=Length;j++){
if(i>=j)
A[i][j]=A[i-1][j]+A[i-1][j-1];
else
A[i][j]=A[i][i];
}
}
for(i=Length,j=One;i>=1;i--){
if(Ith<=A[i-1][j])
str[Length-i]='0';
else{
str[Length-i]='1';
Ith-=A[i-1][j];
j--;
}
}
fprintf(fout,"%s\n",str);
return 0;
}
一开始智商不行啊
把A[i][j]定义为,位数为i时,1的个数为j的情况,而不是小于j的情况
想了半天想不出来
判断1还是0的那个循环感觉好抽象,在心里想了好久。。。