zoukankan      html  css  js  c++  java
  • 杭电1164 Eddy's research I(素数)

    Problem Description
    Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

     
    Input
    The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
     
    Output
    You have to print a line in the output for each entry with the answer to the previous question.
     
    Sample Input
    11 9412
     
    Sample Output
    11 2*2*13*181
     
    Author
    eddy
     
    Recommend
    JGShining
     
        问题描述:给你一个2~65535范围内的整数,然后让你用几个素数相乘的方式表示这个数
        问题解答:首先想到了一个问题是,是不是应该建立一个素数表。怎么建立?筛选法。第一次用这种方法求素数表,写的过程出现了几个错误,一一排除后,调试检查了一下素数表没什么问题啦。第二步,用primnum[]的数组存储了小于n的所有素数;第三步,判断下该整数是不是素数如果是,直接输出该整数就可以啦;不是的话,就用该整数去除小于它的每一个素数(作为除数的素数按从小到大的顺序,依次被除)!大致解题思路就是这样,另外不要随便改变作为除数的素数,因为该整数可能要除该素数好几次。
     1 #include <stdio.h>
     2 #include <math.h>
     3 #define MAX 65535
     4 
     5 int vis[MAX+1];
     6 int prime()
     7 {
     8     //筛选法求素数
     9     int i, j;
    10     int m = sqrt(MAX + 0.5);
    11     vis[0] = vis[1] = 0;
    12     for( i = 2; i < MAX+1; i++ )
    13         vis[i] = 1;
    14     for( i = 2; i <= m; i++ )
    15         if( vis[i] == 1 )
    16         {
    17             for( j = i * i; j <= MAX; j += i )
    18                 vis[j] = 0;
    19         }
    20     return 0;
    21 }
    22 
    23 int main()
    24 {
    25     int n, cnt, primnum[MAX], i;
    26     prime();
    27     while( scanf( "%d", &n ) != EOF )
    28     {
    29         cnt = 0;
    30         for( i = 2; i <= n; i++ )
    31             if( vis[i] == 1 )
    32                 primnum[cnt++] = i;
    33         if( vis[n] == 1 )
    34             printf( "%d\n",n );
    35         else{
    36             i = 0;
    37             while( n % primnum[i]  )
    38                 i++;
    39             n = n / primnum[i];
    40             printf( "%d",primnum[i] );
    41 
    42             while( n != 1 )
    43             {
    44                 while( n % primnum[i] )
    45                     i++;
    46                 n = n / primnum[i];
    47                 printf( "*%d", primnum[i] );
    48             }
    49             printf( "\n" );
    50         }
    51     }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    POJ1125 Stockbroker Grapevine(spfa枚举)
    20160402javaweb 开发模式
    20160331javaweb之JSP 标签技术
    20160331javaweb之JSP include 指令&&九大隐式对象
    20160330javaweb之session 小练习
    20160329javaweb之JSP -session入门
    20160329javaweb之JSP -cookie入门
    20160328 javaweb Cookie 小练习
    20160327javaweb 之JSP入门
    HttpServlet was not found on the Java
  • 原文地址:https://www.cnblogs.com/yizhanhaha/p/3131648.html
Copyright © 2011-2022 走看看