zoukankan      html  css  js  c++  java
  • HDU 4548.美素数 解题心得

    原题:

    Description

      小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识。 
      问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数。 
      给定一个区间,你能计算出这个区间内有多少个美素数吗?
     

    Input

    第一行输入一个正整数T,表示总共有T组数据(T <= 10000)。 
    接下来共T行,每行输入两个整数L,R(1<= L <= R <= 1000000),表示区间的左值和右值。
     

    Output

    对于每组数据,先输出Case数,然后输出区间内美素数的个数(包括端点值L,R)。 
    每组数据占一行,具体输出格式参见样例。
     

    Sample Input

    3 1 100 2 2 3 19
     

    Sample Output

    Case #1: 14 Case #2: 1 Case #3: 4
     
     
    分析:测试数据组数较多,时间只有1000ms ,所以只能打表解决
     
    代码:
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    const int N = 1000000 + 10;
    int ans[N] = { 0 };
    int vis[N];
    
    bool check(int x)
    {
        int total=0;
        while (1)
        {
            total += (x % 10);
            //cout << total << endl;
            x = x / 10;
            if (x == 0)
                break;
    
        }
        if (vis[total] == 0)
            return 1;
        else
            return 0;
    }
    int main()
    {
        int t, kase = 1;
        cin >> t;
    
        vis[1] = 1;
        for (int i = 2; i <= N; i++)
        {
            if (vis[i] == 0)
            for (int j = 2; j*i <= N; j++)
            {
                vis[j*i] = 1;
            }
        }
    
        for (int i = 1; i <= N; i++)
        {
            if (vis[i] == 0&&check(i))
                ans[i] = ans[i - 1] + 1;
            else
                ans[i] = ans[i - 1];
        }
        while (t--)
        {
            int cnt = 0;
            int left,right;
            cin >> left >> right;
            printf("Case #%d: %d
    ", kase++, ans[right]-ans[left-1]);
        
        }
        return 0;
    }
  • 相关阅读:
    MAC OSX 进程间通信
    UVa 10397 Connect the Campus
    ios 类似的效果淘宝商品详细页面
    Programming from the ground up(0)
    解决因特网和xshell考虑到问题
    除去在阵列中重复元件
    Cocos2d-x 手机游戏《疯狂的蝌蚪》资源 “开源” win32+安德鲁斯+iOS三合一
    (四)左右ng-app自己主动bootstrap相框
    Codeforces 338D GCD Table 中国剩余定理
    十月金在线编程竞赛的第二个冠军:解密
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4748886.html
Copyright © 2011-2022 走看看