zoukankan      html  css  js  c++  java
  • HDU 1058

    这是一道找规律的题目。(试图在2000000000个数中逐一找出不合适的去除,或者用蠢方法判断是否为大于7的素数或大于7素数的倍数再加入humble数组的方法都是不可行的!)

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27是正确的序列(素因子只有2,3,5,7),容易想到除1外,可以用辗转相除法求得其所有素因子。比如2000,先用2除,得1000,要使2000是humble数,那么1000也必须是humble数;若1000已是humble数,则必定已在数组中。得出结论,后面的humble数是前面某个humble数的2/3/5/7倍,利用p2、p3、p5、p7作为移动标签,每次选最小的加入后,移动该标签即可。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #define max 2000000000
     6 
     7 int num[5850],n,p2,p3,p5,p7;
     8 
     9 int min(int i,int j,int k,int l){
    10     int a,b;
    11     if(i<j) a=i;
    12     else a=j;
    13     if(k<l) b=k;
    14     else b=l;
    15     if(a<b) return a;
    16     else return b;
    17 }
    18 
    19 int main(){
    20     num[1]=1;
    21     n=1;
    22     p2=p3=p5=p7=1;
    23     while(num[n]<max){
    24         n++;
    25         num[n]=min(2*num[p2],3*num[p3],5*num[p5],7*num[p7]);
    26         if(num[n]==2*num[p2]) p2++;
    27         if(num[n]==3*num[p3]) p3++;
    28         if(num[n]==5*num[p5]) p5++;
    29         if(num[n]==7*num[p7]) p7++;
    30     }
    31     while(scanf("%d",&n)!=EOF&&n!=0){
    32         if(n%10==1&&n%100!=11)
    33             printf("The %dst humble number is %d.
    ",n,num[n]);
    34         else if(n%10==2&&n%100!=12)
    35             printf("The %dnd humble number is %d.
    ",n,num[n]);
    36         else if(n%10==3&&n%100!=13)
    37             printf("The %drd humble number is %d.
    ",n,num[n]);
    38         else
    39             printf("The %dth humble number is %d.
    ",n,num[n]);
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    Python_数据类型与变量
    啦啦啦
    Java开发环境搭建
    TCP/IP 学习 --- 4(linux网络基础api)
    TCP/IP 学习 --- 3 (流量控制和拥塞控制)
    TCP/IP 学习 --- 2
    TCP/IP 学习记录 -- 1
    多线程
    如何解析xml文件
    Redis
  • 原文地址:https://www.cnblogs.com/daic/p/4857326.html
Copyright © 2011-2022 走看看