微软2014实习生招聘笔试第2题 the k-th string
Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB
Description
Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary order. If such a string doesn’t exist, or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s, we will have a set with 6 different strings, {0011, 0101, 0110, 1001, 1010, 1100}, and the 4th string is 1001.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case.
Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of string in the set that needs to be printed as output.
Output
For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
求全排列的问题.
使用STL #include<algorithm> ,可能超时
#include <iostream>
#include <algorithm>
#include <string>
using namespace std; // 计算组合数
long long com(int n,int r)
{
if(n-r < r) r= n-r; //减少计算量
int i,j,s=;
for(i=0,j=;i<r;++i)
{
s*=(n-i);
for(;j<=r && s%j==; ++j) s/=j; // 防止溢出
}
return s;
}
int main()
{
string str;
int T,a,b,i,j;
long long m,Count;
cin>>T;
while(T--)
{
Count=;
str.clear();
cin>>a>>b>>m;
for(i=;i<a;i++)
str+="";
for(j=;j<b;j++)
str+="";
if(m>com(a+b,b))
cout<<"Impossible"<<endl;
else
{
while (next_permutation(str.begin(), str.end()))
{
++Count;
if(Count+==m)
{
cout<<str<<endl;
break;
}
}
}
}
return ;
}
方法二: 回溯法
#include <stdio.h>
#define MAX_N 33 int n,m=;
long long Count=,times; // 共有n个数,其中互不相同的有m个
int rcd[MAX_N]; //记录每个位置填的数字
int used[MAX_N]; //标记m个数可以使用的次数
int num[MAX_N]; // 存放互不相同的m个数 0,1 long long com(int n, int r)
{
if(n-r < r) r= n-r; // 减少计算量
int i,j,s=;
for(i=0,j=;i<r;++i)
{
s*=(n-i);
for(;j<=r && s%j==; ++j) s/=j; // 尽量避免越界
}
return s;
}
void unrepeat_permutation(int l)
{
int i;
if(l==n)
{
Count++;
if(Count==times) //查找到所需的数字
{
for(i=;i<n;i++)
{
printf("%d",rcd[i]);
if(i<n-) printf(" ");
}
printf("\n");
}
return;
}
for(i=;i<m;i++) //回溯求解
{
if(used[i]>)
{
used[i]--;
rcd[l] = num[i];
unrepeat_permutation(l+);
used[i]++;
}
}
}
int main()
{
int a,b,i,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%lld",&a,&b,×);
n=a+b;
if(times>com(n,a)) // 判断是否超出组合范围
{
printf("Impossible\n");
continue;
}
used[]=a; used[]=b; //记录0,1的个数
num[]=; num[]=; //记录可能出现的数字
Count=;
unrepeat_permutation();
}
return ;
}
第一题scanf,gets 浪费了太多时间!!!
微软2014实习生招聘笔试第2题 the k-th string的更多相关文章
- 优酷土豆2014校园招聘笔试题目之Java开发类
先总体说下题型,共有20道选择题,4道简答题,3道编程题和1道扩展题,题目都比较简单,限时一小时完成. 一.选择题 选择题非常简单,都是基础题,什么死锁发生的条件.HashMap和HashSet查找插 ...
- 【微软2014实习生及秋令营技术类职位在线測试】题目2 : K-th string
时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Consider a string set that each of them consists of ...
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...
- 2019_京东JAVA实习生招聘机试第一题
题意抽象出来就是,求根节点的所有子节点中,以这些子节点为根的子树的最大节点数. 已有向图的方式来保存无向图,所以叶子结点i的eage[i].size()==1. import java.util.Ar ...
- 九度OJ 1525 子串逆序打印 -- 2012年Google校园招聘笔试题目
题目地址:http://ac.jobdu.com/problem.php?pid=1525 题目描述: 小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在印刷的过程中将字符串 ...
- S2 深入.NET和C#编程 笔试测试错题积累
---恢复内容开始--- <深入.NET平台和C#编程>内部测试题-笔试试卷错题积累 1: 1) 以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储 ...
- 乘风破浪:LeetCode真题_023_Merge k Sorted Lists
乘风破浪:LeetCode真题_023_Merge k Sorted Lists 一.前言 上次我们学过了合并两个链表,这次我们要合并N个链表要怎么做呢,最先想到的就是转换成2个链表合并的问题,然后解 ...
- 金山网络2014春季Android实习生招聘-成都站-笔试第二题
一个文件名为input.txt的文件当中,每一行都有一个单词,要求统计单词出现的频率,并且按照从小到大出现次数打印,次数相同的按照首字母顺序排序. package jinshanwangluo.exa ...
- 金山网络2014春季Android实习生招聘-成都站-笔试第一题
实现单例模式,并实现方法int getResult(float a),将a*8后返回. package jinshanwangluo.exam; /** * @author guoxm * @date ...
随机推荐
- 「小程序JAVA实战」小程序通用模板的使用(17)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-17/ 小程序也为了页面增加了通用模板的功能,如何去理解一个通用的模板呢?模板的定义就是为了让我们的 ...
- Django models拆分
大多数Django教程都是将models放在models.py文件(模块)中, 然而随着models类的增加, 将类放在一个文件中太混乱了, 于是将models做成一个package: 这样就可以将m ...
- Spring+Log4j的集成总结
导入依赖的jar包 <log4j.version>1.2.16</log4j.version> <!-- 自动引入slf4j-api.jar,log4j.jar,以及sl ...
- UNIX网络编程——socket的keep-alive(转)
第一部分 [需求] 不影响服务器处理的前提下,检测客户端程序是否被强制终了. [现状] 服务器端和客户端的Socket都设定了keepalive属性. 服务器端设定了探测次数等参数,客户端.服务器只是 ...
- select sum也会返回null值
SELECT SUM(detail.VAL) FROM AI_SDP_ORDER_MONTH_DETAIL_201706 detail 如果所有的VAL都是null的话,或者根本就不存在 ...
- ServletContextListener中@Autowired失效的解决方法
@WebListener public class ContextWebListener implements ServletContextListener { @Override public vo ...
- Elasticsearch-PHP 搜索操作
搜索操作 好吧,这不叫elasticsearch的不劳而获!让我们来谈谈PHP客户端中的搜索操作. 客户端允许你通过REST API访问所有的查询和公开的参数,尽可能的遵循命名规则.让我们来看一些例子 ...
- Linux环境下安装myeclipse+破解
1.下载myeclipse安装包,下载myeclipse破解文件. 2.修改myeclipse-pro-2014-GA-offline-installer-linux.run的权限 sudo chmo ...
- Bug of VS2015+WDK
1> Signability test failed.1> 1> Errors:1> 22.9.7: DriverVer set to incorrect date ( ...
- debian下使用dig/nslookup
debian默认没有安装dig/nslookup命令,使用下面命令安装: apt-get install dnsutils red-hat系列使用: yum install bind-utils ho ...