微软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 ...
随机推荐
- Python实践练习:在 Wiki 标记中添加无序列表
题目描述 项目:在 Wiki 标记中添加无序列表 在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你 ...
- 记一则css3计算
.Head{ background-image: url("../../Img/PersonalCenter/banner.png"); background-repeat: no ...
- leetcode572
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- <转>杜绝 Defunct进程 僵尸进程
http://hanover.iteye.com/blog/881972 在测试基于 DirectFB+Gstreamer 的视频联播系统的一个 Demo 的时候,其中大量使用 system 调用的语 ...
- 通过ajax异步调用返回值
调用方法的时候传递一个callback方法来获取成功回调的值test(function (data) { }); function test(callback){ $.ajax({ type: &qu ...
- 清除html中的标记,只留下文字
/// <summary>/// 清除html中的标记,只留下文字./// </summary>/// <param name="HTML">& ...
- Array.prototype.slice用法详解
slice方法是定义在js数组原型中的方法,用于截取数组的部分元素,具体使用如下: arrayExample.slice(start, end); start为起始元素位置,end为截止元素位置,如: ...
- 3D数学基础 KeyNote 1
[计算几何复习要点] 1.向量加法的几何含意: a+b的释意为:a的尾连上b的头,新建一条从a的尾指向b的头的向量. 2.向量减法的几何含意: a-b的释意为:尾部相连,新建一个从b的头指向a的头的向 ...
- jmeter-plugins-dubbo & DevToolBox
jmeter-plugins-dubbo使用 A. 下载jmeter并安装,http://jmeter.apache.org/download_jmeter.cgi(文中使用的版本是3.3,理论上高版 ...
- 14-python登入教务网(python+bs4)
用request先得到到session对象,用其去放送请求,会自动保存cookie. 模拟有验证码的登入步骤: 1.发送请求登入页面: 2.分析验证码的地址,以及要将登入请求发往的地址(可以先输入错的 ...