• 杭电 2136 Largest prime factor(最大素数因子的位置)


    Largest prime factor

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 13481    Accepted Submission(s): 4765


    Problem Description
    Everybody knows any number can be combined by the prime number.
    Now, your task is telling me what position of the largest prime factor.
    The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
    Specially, LPF(1) = 0.
     
    Input
    Each line will contain one integer n(0 < n < 1000000).
     
    Output
    Output the LPF(n).
     
    Sample Input
    1 2 3 4 5
     
    Sample Output
    0 1 2 1 3
     
    题意:众所周知,一个数可以由素数因子(和其它因子素数或者非素数 )相乘而形成,比如2的质数因子是2(也就是第1个质数),3是3(也就是第2个质数),4是2(也就是第1个质数)……然后题意就是问你是第几个素数。
    解题思路:表示刚刚看到题目是懵逼的,看了人家的题解也是懵逼的,可能和自己在家里好久没A题目了有关系。
                      题目思路是这样:1.先做一个全局变量(如果不是全局变量就会出现栈溢出),然后把那个全部置为0。a[i]=x  i表示是某个数 x表示是第几个质数
                                                   2.再开始打表,从第a[2]个开始看它是不是素数,是就把当前cnt做a[i]=cnt,然后把i的倍数 a[n*i]=cnt(循环),然后因为后面这些数都是2的倍数,它们就不是质数,但如果有质                                                    数比2 大且是它们的素数因子的话 就会再之后的判断中更新。
                                                   3.用scanf("%d",&n)!=EOF来读取数据,不然会超时,不加EOF也不行因为不加的话会不断的读取数据,还是会超时。
    附上代码:
    #include <iostream>
    #include<math.h>
    #include <iomanip>
    #include<cstdio>
    #include<string>
    #include<map>
    #include<vector>
    #include<list>
    #include<algorithm>
    #include<stdlib.h>
    #include<iterator>
    #include<sstream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    
    //题目的意思是找到最大素数因子的位置 就是第几个素数而不是
    //找到最大素数因子
    int a[1000001];//要定义全局变量 不然会栈溢出
    int main()
    {
       int cnt=1;//记录排第几个
    
       memset(a,0,sizeof(a));//把所有的数组中的数全部置为0
       // a[1]=0;
       for(int i=2;i<1000001;i++)//素数从2开始排 因为题目要求是1
       {
            if(a[i]==0)//表示是素数 因为没有它的因子让它置为其它数
            {
               a[i]=cnt;
    
               for(int k=2*i;k<1000001;k=k+i)//是素数就先把它的倍数给置数
               {// 然后遇到大的就不断刷新
                   a[k]=cnt;
               }
               cnt++;
            }
       }
       int n;
       while(scanf("%d",&n)!=EOF)
       {
           printf("%d
    ",a[n]);
       }
        return 0;
    }

    这题感觉花了一定的时间才弄懂。

  • 相关阅读:
    mysql 主从配置 读写分离
    interface接口
    http结构
    call_user_func函数
    pcntl_fork 进程
    数据库事务
    php 之 ob缓冲
    shell脚本
    php 守护进程
    ssdb zset
  • 原文地址:https://www.cnblogs.com/William-xh/p/7289223.html
Copyright © 2020-2023  润新知