zoukankan      html  css  js  c++  java
  • 河南省第十届省赛 Binary to Prime

    题目描述:

    To facilitate the analysis of  a DNA sequence,  a DNA sequence is represented by a binary  number. The group of  DNA-1 has discovered a great new way .  There is a certain correlation between binary number and prime number. Instead of using the ordinary decadic numbers, they use prime base numbers. Numbers in this base are expressed as sequences of zeros and ones similarly to the binary numbers, but the weights of bits  in the representation are not powers of two, but the elements of the primes  ( 2, 3, 5, 7,... ).

    For example  01101 , ie. 2+5+7=14

    Herefore, it is necessary to convert the binary number to the sum of prime numbers

    输入:

    The input consists of several instances, each of them consisting of a single line. Each line of the input contains a 01 string, length of not more than 150.  The end of input is not marked in any special way.

    输出:

    For each test case generate a single line containing a single integer , The sum of the primes.

    样例输入:

    000010
    0011
    11001
    

    样例输出:

    3
    5
    20
    

    分析:

    给出一个二进制的序列,逆着看这个序列,输出每个‘1’位上对应的素数.

    例如: 01101

    逆着看的话为1的分别为第一位,加上第一个素数2

                                         第三位,加上第三个素数5
    
                                         第四位,加上第四个素数7
    

    最终结果为2+5+7=14

    代码:

    #include <cstdio>
    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    char st[160];
    bool vis[2000];
    int s[2000];
    using namespace std;
    void Shusu()///首先把素数表给打印出来
    {
        int i,j;
        for(i=2; i<=3000; i++)
        {
            for(j=i+i; j<=3000; j+=i)
            {
                if(!vis[j])
                {
                    vis[j] = true;
                }
            }
        }
        int k = 0;
        for(i=1; i<=3000; i++)
        {
            if(vis[i]==false)
            {
                s[k++] = i;
            }
        }
     
    }
    int main()
    {
        memset(vis,false,sizeof(vis));
        Shusu();
        while(scanf(" %s",st)!=EOF)
        {
            int i;
            long long sum = 0;
            int len = strlen(st);
            for(i=1; i<=len; i++)
            {
                if(st[len-i]=='1')///然后直接加上每一位对应的素数就行
                {
                    sum += s[i];
                }
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    Struts2 xml表单验证
    struts2表单验证-整合国际化
    Strut2-Ajax总结
    java中的线性安全和不安全
    智游推送试用
    推送的重连策略
    Android本地通知的实现方式
    MyBatis框架学习二
    Java基础学习 2 (选择结构,循环结构)
    Java基础学习 1 (变量,数据类型,运算符)
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6887901.html
Copyright © 2011-2022 走看看