zoukankan      html  css  js  c++  java
  • 微软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 s​uch 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>  ,可能超时

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 using namespace std;
     5 
     6 
     7 // 计算组合数
     8 long long com(int n,int r)
     9 {
    10    if(n-r < r) r= n-r;  //减少计算量
    11    int i,j,s=1;
    12    for(i=0,j=1;i<r;++i)
    13    {
    14        s*=(n-i);
    15        for(;j<=r && s%j==0; ++j) s/=j;  // 防止溢出
    16    }
    17    return s;
    18 }
    19 
    20 int main()
    21 {
    22     string str;
    23     int T,a,b,i,j;
    24     long long m,Count;
    25     cin>>T;
    26     while(T--)
    27     {
    28         Count=0;
    29         str.clear();
    30         cin>>a>>b>>m;
    31         for(i=0;i<a;i++)
    32            str+="0";
    33         for(j=0;j<b;j++)
    34            str+="1";
    35         if(m>com(a+b,b))
    36           cout<<"Impossible"<<endl;
    37         else
    38         {
    39             while (next_permutation(str.begin(), str.end()))
    40             {
    41                 ++Count;
    42                  if(Count+1==m)
    43                 {
    44                     cout<<str<<endl;
    45                     break;
    46                 }
    47             }
    48 
    49         }
    50     }
    51     return 0;
    52 }

    方法二: 回溯法

     1 #include <stdio.h>
     2 #define MAX_N 33
     3 
     4 int n,m=2;
     5 long long Count=0,times; // 共有n个数,其中互不相同的有m个 
     6 int rcd[MAX_N];   //记录每个位置填的数字
     7 int used[MAX_N]; //标记m个数可以使用的次数
     8 int num[MAX_N]; // 存放互不相同的m个数 0,1
     9 
    10 long long com(int n, int r)
    11 {
    12     if(n-r < r) r= n-r;   // 减少计算量
    13     int i,j,s=1;
    14     for(i=0,j=1;i<r;++i)
    15     {
    16        s*=(n-i);
    17        for(;j<=r && s%j==0; ++j) s/=j;  // 尽量避免越界
    18      }
    19     return s;
    20 }
    21 
    22 void unrepeat_permutation(int l)
    23 {
    24      int i;
    25      if(l==n)
    26      {
    27              Count++;
    28              if(Count==times)   //查找到所需的数字
    29              {
    30                  for(i=0;i<n;i++)
    31                  {
    32                                  printf("%d",rcd[i]);
    33                                  if(i<n-1) printf(" ");
    34                  } 
    35                  printf("
    ");    
    36                  
    37              }
    38              return;
    39      } 
    40      for(i=0;i<m;i++) //回溯求解
    41      {
    42        if(used[i]>0)
    43        {
    44                    used[i]--;
    45                    rcd[l] = num[i];
    46                    unrepeat_permutation(l+1);
    47                    used[i]++;
    48        }
    49      }
    50 } 
    51 
    52 
    53 
    54 int main()
    55 {
    56     int a,b,i,T;
    57     scanf("%d",&T);
    58     while(T--)
    59     {
    60           scanf("%d%d%lld",&a,&b,&times);
    61           n=a+b;
    62           if(times>com(n,a))  // 判断是否超出组合范围
    63           {
    64              printf("Impossible
    ");
    65              continue;
    66           }
    67           used[0]=a;  used[1]=b;  //记录0,1的个数
    68           num[0]=0;   num[1]=1;   //记录可能出现的数字
    69           Count=0; 
    70           unrepeat_permutation(0);
    71     }
    72     return 0;
    73 }

    第一题scanf,gets 浪费了太多时间!!!

  • 相关阅读:
    LIst判断是否为空
    SYBASE数据库基本操作
    Java内部类详解--成员内部类,局部内部类,匿名内部类,静态内部类
    String转jsonarry:字符串:[{"result":"20"},{"result":"21"},{"result":"20"},{"result":"22"}]
    使用HttpURLConnection发送POST请求
    Java 定义字符串数组
    JSP 解决illegal to have multiple occurrences of contentType with different values错误
    javascript 获取url参数值
    外键为','(逗号)拼接ID,连接查询外键表ID
    excel、csv、txt文件数据读取
  • 原文地址:https://www.cnblogs.com/double-win/p/3661364.html
Copyright © 2011-2022 走看看