• 洛谷P3048 [USACO12FEB]牛的IDCow IDs


    P3048 [USACO12FEB]牛的IDCow IDs

    题目描述

    Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitious, and only labels cows with binary numbers that have exactly K "1" bits (1 <= K <= 10). The leading bit of each label is always a "1" bit, of course. FJ assigns labels in increasing numeric order, starting from the smallest possible valid label -- a K-bit number consisting of all "1" bits. Unfortunately, he loses track of his labeling and needs your help: please determine the Nth label he should assign (1 <= N <= 10^7).

    FJ给他的奶牛用二进制进行编号,每个编号恰好包含K 个"1" (1 <= K <= 10),且必须是1开头。FJ按升序编号,第一个编号是由K个"1"组成。

    请问第N(1 <= N <= 10^7)个编号是什么。

    输入输出格式

    输入格式:
    • Line 1: Two space-separated integers, N and K.
    输出格式:

    输入输出样例

    输入样例#1:
    7 3 
    
    输出样例#1:
    10110 
    /*
        将串倒着存储
        枚举每个编号对应的二进制串
        如果还存在10子串,就swap
        如果不再存在,就加一位 
    */
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int n,m,l;
    bool bin[100000000];
    int main(){
        scanf("%d%d",&n,&m);
        l=m;
        for(int i=1;i<=m;i++)bin[i]=1;
        for(int i=2;i<=n;i++){//从头枚举 
            bool flag=0;
            for(int j=1;j<l;j++){
                if(bin[j]==1&&bin[j+1]==0){
                    flag=1;
                    bin[j]=0;bin[j+1]=1;
                    break;
                }
            }
            if(!flag){
                for(int j=1;j<m;j++)bin[j]=1;
                for(int j=m;j<=l;j++)bin[j]=0;
                l++;bin[l]=1;
            }
        }
        for(int i=l;i>=1;i--)printf("%d",bin[i]);
    }
    30分 暴力 不知道为什么WA了三个点
    #include<cstdio>
    using namespace std;
    int n,k,j;
    int a[13];
    int main(){
        scanf("%d%d",&n,&k);
        for(int i=1;i<=k;i++)a[i]=i;
        for(int i=2;i<=n;i++){
            j=1;
            while(1){
                a[j]++;
                if(a[j]!=a[j+1])break;
                a[j]=j;
                j++;
            }
        }
        j=k;
        for(int i=a[k];i>=1;i--){
            if(a[j]==i)printf("1"),j--;
            else printf("0");
        }
        return 0;
    }
    100分 模拟?
  • 相关阅读:
    H3C无线配置2三层注册典型配置举例(集中转发)
    PHP 中 exec() 执行系统外部命令
    salesforce 从零开始(一)开始使用
    求和平均统计
    VS2012 如何进行远程调试
    H5跳转小程序的方法
    C#提取HTML中IMG标签的URL
    数据库链接字符串中的细节(integrated security=true;MultipleActiveResultSets=true)
    运行cmd状态下MySQL导入导出.sql文件
    sql server 2008 rownumber 分页sql语句
  • 原文地址:https://www.cnblogs.com/thmyl/p/7576340.html
Copyright © 2020-2023  润新知