您的位置:首页 > 其它

UVA 729 The Hamming Distance Problem

2016-07-26 21:24 363 查看

UVA-729

题意:balabala了一堆,其实就是求n位01串中有 h 个1的所有序列。

解题思路:STL中有一个求字符串字典序下一个字符的函数。next_permutation(c,c+n)。第一个是起始位置,后一个是结束位置。类似sort。然后只要把第一个(n-h个)0+ (h个)1的串弄出来。就好了。。

/*************************************************************************
> File Name: UVA-729.cpp
> Author: Narsh
>
> Created Time: 2016年07月26日 星期二 16时33分32秒
************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int t,n,h;
char c[20];
int main() {
scanf("%d",&t);
while (t--) {
scanf("%d%d",&n,&h);
memset(c,0,sizeof(c));
for (int i = 0; i < n-h; i++) c[i]='0';
for (int i = n-h; i < n; i++) c[i]='1';
cout<<c<<endl;
while (next_permutation(c,c+n))
cout<<c<<endl;
if (t) printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: