微软2014年实习生在线机试第二题及解法
来源:互联网 发布:log4j输出mybatis sql 编辑:程序博客网 时间:2024/05/17 09:39
翻译我就不做了,看大家有什么好思路,
Time Limit: 10000ms
Case Time Limit: 1000msMemory 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
采用递归解法
#include<iostream>#include<string>using namespace std;//M代表0的个数int M=0;//N代表1的个数int N=0;//K代表第k个满足条件的数int K=0;/*a代表数组m代表0的个数n代表1的个数count代表满足要求的数的个数layer代表目前数的位数*/void findString(int layer, int &count,int m,int n,int a[]){if(m>M || n>N)//不满足条件,直接返回return;if(count>K)//只需要输出第K个,其它无需遍历了return;if( (layer+1)==(M+N) )//递归终止,位数到了{//这里(layer+1)是因为数组下标从0开始//最后一位放0,判断是否满足要求a[layer]=0;m=m+1;if(m==M && n==N)//判断是否满足条件:有m个0,n个1{count++;if(count==K){int i;for(i=0;i<=layer;i++)cout<<a[i];cout<<endl;}}--m;//最后一位放1,判断是否满足要求a[layer]=1;n=n+1;if(m==M && n==N)//判断是否满足条件:有m个0,n个1{count++;if(count==K){int i;for(i=0;i<=layer;i++)cout<<a[i];cout<<endl;}}return;}//当前位置先放0,使其满足字典排序a[layer]=0;findString(layer+1,count,m+1,n,a);//当前位置后放1a[layer]=1;findString(layer+1,count,m,n+1,a);}void main(){int a[35]={0};int count=0;K=4;M=2;N=2;//先判断K是否超出M和N组合的数的个数了findString(0, count,0,0,a);getchar();}
1 0
- 微软2014年实习生在线机试第二题及解法
- 微软2014实习生及校招秋令营技术类职位在线测试第二题
- 微软2014实习生在线测试题
- 微软2016年实习生在线笔试第二题(403 Forbidden)
- 2014微软实习生招聘第二题
- 微软2014实习生在线测试
- K-th string (微软2014实习生及秋令营技术类职位在线测试题)
- 微软2014实习生及秋令营技术类职位在线测试(第一题)
- 微软2014实习生及秋令营技术类职位在线测试第3题
- 微软2014实习生及秋令营技术类职位在线测试第一题:String reorder
- 2014/04/12微软实习生在线测试题
- 微软2014实习生在线测试题:K-th String
- 微软2014实习生在线测试_4.12
- 微软2014实习生及校招秋令营技术类职位,在线编程题目及解答。
- 微软2014实习生及秋令营技术类职位在线测试 String reorder java
- 微软2014实习生及校招秋令营技术类职位,在线编程题目
- 微软2014实习生及秋令营技术类职位在线测试+试题1+答案
- 微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder
- LayoutInflater作用及使用
- IOS声音调用
- Linux根目录下各个目录的作用
- Panel Paint事件
- jquery调用方法
- 微软2014年实习生在线机试第二题及解法
- fedora 18~20 中Qt 5.2.1 解决连接mysql数据库出现QMYSQL driver not loaded的问题
- 微软2014实习生及秋令营技术类职位在线测试: K-th string
- 我的OpenCV学习笔记(11):启动摄像头
- javascript通用事件处理程序
- c++-带默认形参值的函数
- ios中KeyChain用途
- Android颜色选择器
- Android自定义控件样式及高亮显示