zoukankan      html  css  js  c++  java
  • UVA, 583 Prime Factors

    题意:给你一个数,让你输出其质因数分解形式,注意负数和正数区别

    思路:素数筛法,质因数分解

    代码如下:

      1 #include <iostream>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <cstdio>
      5 #include <cstdlib>
      6 #include <cmath>
      7 
      8 using namespace std;
      9 
     10 const int N=1000000;
     11 bool flag=false;
     12 int cnt,tot;
     13 long num;
     14 long prime[N];
     15 bool isprime[N];
     16 int a[N];
     17 
     18 void getprime();
     19 bool datecin();
     20 void datecal(long);
     21 void showres();
     22 
     23 
     24 void getprime()
     25 {
     26     memset(isprime,1,sizeof(isprime));
     27     isprime[0]=isprime[1]=cnt=0;
     28     for(int i=2;i<N;i++)
     29     {
     30         if(isprime[i])
     31             prime[cnt++]=i;
     32         for(int j=0;j<cnt&&i*prime[j]<N;j++)
     33         {
     34             isprime[i*prime[j]]=0;
     35             if(!(i%prime[j]))break;
     36         }
     37     }
     38 }
     39 
     40 bool datecin()
     41 {
     42     flag=false;
     43     if(scanf("%ld",&num)!=EOF&&num)
     44         return true;
     45     return false;
     46 }
     47 
     48 
     49 void datecal(long num)
     50 {
     51     if(num<0)
     52     {
     53         flag=true;
     54         num=-num;
     55     }
     56     double temp=sqrt(num)+1;
     57     tot=0;
     58     for(int i=0;temp>prime[i];i++)
     59     {
     60         if(num%prime[i]==0)
     61         {
     62             while(num%prime[i]==0)
     63             {
     64                 a[++tot]=prime[i];
     65                 num/=prime[i];
     66             }
     67         }
     68     }
     69     if(num!=1)
     70     {
     71         a[++tot]=num;
     72     }
     73 }
     74 
     75 void showres()
     76 {
     77     if(flag)
     78     {
     79         printf("%ld = -1 x ",num);
     80     }
     81     else
     82     {
     83         printf("%ld = ",num);
     84     }
     85     for(int i=1;i<=tot;i++)
     86     {
     87         printf("%d",a[i]);
     88         if(i!=tot)
     89             printf(" x ");
     90     }
     91     printf("
    ");
     92 }
     93 
     94 int main()
     95 {
     96     getprime();
     97     //showprime();
     98     while(datecin())
     99     {
    100         datecal(num);
    101         showres();
    102     }
    103     return 0;
    104 }
  • 相关阅读:
    TCP/IP网络编程之优于select的epoll(一)
    TCP/IP网络编程之I/O流分离
    TCP/IP网络编程之套接字与标准I/O
    TCP/IP网络编程之多播与广播
    TCP/IP网络编程之多种I/O函数
    TCP/IP网络编程之I/O复用
    TCP/IP网络编程之进程间通信
    TCP/IP网络编程之多进程服务端(二)
    服务器被入侵了怎么办?
    sentos7为例添加python3和python2共存
  • 原文地址:https://www.cnblogs.com/byzsxloli/p/5438849.html
Copyright © 2011-2022 走看看