微软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实战」小程序我的个人信息-注销功能(42)
转自:https://idig8.com/2018/09/06/xiaochengxujavashizhanxiaochengxuwodegerenxinxi-zhuxiaogongneng40/ 注 ...
- Linux下network提示Determining if ip address
转自:https://blog.csdn.net/ranran0224/article/details/73323925 Centos系统重启网络服务network 会提示Determining if ...
- 【LUA table 移除操作非常慢】
LUA的表有插入和删除两种操作.插入操作非常快,100000次操作都在0.01S左右,而删除操作在表元素大于10000时却急速变慢,测试如下: t = {} local t1= os.clock() ...
- Struts2结果页面配置(Result)
1.全局页面配置 全局结果页面:针对一个包下的所有Action的页面跳转都可生效 <global-results> <result name="xxx">/ ...
- Android 重写EditText回车事件
之前遇到的问题没来得及记录下来,趁今晚有空就重新回忆并写下了. 我们在用到EditText这个空间时经常需要重写软键盘中的回车事件以配合我们接下来的响应,比如点击回车变成搜索.发送.完成等. Edit ...
- SQL 数据库 学习 003 什么是数据库? 为什么需要数据库?是不是所有的软件都是用Sql Server?
什么是数据库? 为什么需要数据库? 是不是所有的软件都是用Sql Server? 我的电脑系统: Windows 10 64位 使用的SQL Server软件: SQL Server 2014 Exp ...
- loadrunner12-添加集合点
1.首先添加集合点之前要插入事务,集合点和事务通常是一起使用的,单独使用集合点,基本上可以说是没有意义的. 2.在开始事务之前 ,插入一个“集合点”,那么在多用户执行时,就可以将用户请求停下来,直到用 ...
- [C++] Pen questions & linux cmd
1.宏替换,完全展开替换,注意带来副作用 #include <stdio.h>#define 打印语句 printf(“hello”); Void main(void) { If (1) ...
- code1085 数字游戏
划分dp 把环变链(读入4 3 -1 2变成4 3 -1 2 4 3 -1 2) 设dp[i][j][k]为把i~j分成k份,各部分内的数字相加,相加所得的k个结果对10取模后再相乘,最终得到的一个数 ...
- 一个新手后端需要了解的前端核心知识点之margin(二)
最近以开发自己博客网站为出发点开始决心打牢几个非常重要的前端知识点: margin,这个在我刚刚接触编程的时候留下的困扰的东西,一开始只想着怎么快速开发自己的网站,别人的终归是别人的,想要挖墙脚,必须 ...