zoukankan      html  css  js  c++  java
  • 质因数分解

    描述

    已知正整数n是两个不同的质数的乘积试求出较大的那个质数。

    格式

    输入格式

    输入只有一行包含一个正整数n。

    输出格式

    输出只有一行包含一个正整数p, 即较大的那个质数。

    样例1

    样例输入1[复制]

    21

    样例输出1[复制]

    7

    限制

    1S

    提示

    【数据范围】 对于60%的数据6 ≤ n ≤ 1000。 对于100%的数据6 ≤ n ≤ 2*10的9次方

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<cmath>
     5 using namespace std;
     6 bool prim[10000005];
     7 bool judge(int a)
     8 {
     9     int i;
    10     for(i=2;i<=sqrt((double)a);i++)
    11     {
    12         if(a%i==0)
    13             return false;
    14     }
    15     return true;
    16 }
    17 int main()
    18 {
    19     int n;
    20     int i,j;
    21     memset(prim,false,sizeof(prim));
    22     prim[0]=true;
    23     prim[1]=true;
    24     for(i=2;i<=5000000;i++)
    25     {
    26         if(!prim[i])
    27         {
    28             for(j=i+i;j<=10000000;j+=i)
    29             {
    30                 prim[j]=true;
    31             }
    32         }
    33     }
    34 
    35     while(~scanf("%d",&n))
    36     {
    37         int dd=sqrt((double)n);
    38         if(dd*dd==n)
    39             dd=dd-1;
    40         for(i=2;i<=dd;i++)
    41         {
    42             if(!prim[i])
    43             {
    44                 if(n%i==0)
    45                 {
    46                     if(judge(n/i))
    47                     {
    48                         printf("%d\n",n/i);
    49                         break;
    50                     }
    51                 }
    52             }
    53         }
    54     }
    55     return 0;
    56 }

    又是一水题,贡献了好几次的RE,原因是题目的数据范围是20的9次方,我从2遍历到sqrt(n),找到符合条件的再用prim[n/i],来判断,结果有一个测试数据居然超过了我定义的数组,没办法,只能重新写一个judge()函数了,唉,还是不过仔细啊!!

  • 相关阅读:
    Educational Codeforces Round 86 (Rated for Div. 2) D. Multiple Testcases
    Educational Codeforces Round 86 (Rated for Div. 2) C. Yet Another Counting Problem
    HDU
    HDU
    HDU
    HDU
    Good Bye 2019 C. Make Good (异或的使用)
    Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
    codeforces 909C. Python Indentation
    codeforces1054 C. Candies Distribution
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3011853.html
Copyright © 2011-2022 走看看