PAT:1059. Prime Factors (25) AC

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=100010;      //int型素数一定在这个范围内

int PrimeArr[MAX];        //素数表
int cnt=0;            //素数表中素数个数
int x,x2;            //输入的数字,x2是x的开平方数,x的因子只有从2到根号x+1

struct factor
{
  int num,amount;        //num代表这个结构代表的素数,amount代表这个素数的个数
}fac[10];            //【skill】2*3*5*7*11*13*17*19*23*29已超出int范围了,10个结构就足够了
int facI=0;            //fac的下标

bool isPrime(int a)        //判断是否为素数
{
  if(a==1)
    return 0;
  else
  {
    int sqr=sqrt(a*1.0);
    for(int i=2 ; i<sqr+1 ; ++i)
      if(a%i==0)
        return 0;
  }
  return 1;
}

void Prinmetable()          //生成素数表
{
  for(int i=2 ; i<MAX ; ++i)
    if(isPrime(i)==1)
      PrimeArr[cnt++]=i;    //是素数,存入素数表
}

int main()
{
  memset(fac,0,sizeof(fac));
  scanf("%d",&x);
  printf("%d=",x);        //后面x要改变,这里就先将x输出
  if(1==x)            //特判1,既不是素数也不是合数
  {
    printf("1");
    return 0;
  }
  x2=sqrt(1.0*x);          //【skill】x的开平方数,x的因子只有从2到根号x+1
  Prinmetable();          //生成素数表
  //情况1:x不是素数
  for(int i=0 ; i<cnt && x2>=PrimeArr[i]; ++i)  //统计素数;【caution】x>=PrimeArr[i]必须加上,PrimeArr[i]>x2即大于x的开平方一定没有约数了
  {
    int su=PrimeArr[i];
    if(x%su==0)              //说明是因子
    {
      fac[facI].num=PrimeArr[i];
      while(x%su==0)          //统计该因子个数
      {
        ++fac[facI].amount;
        x/=su;
      }
      ++facI;
    }
    if(1==x)
      break;
  }
  //情况2:x本身就是单独的素数
  if(1!=x)
  {
    ++fac[facI].amount;      //【warning】顺序不能颠倒,不然导致数量加到下一个上
    fac[facI++].num=x;
  }
  for(int i=0 ; i<facI ; ++i)    //输出
  {
    int tmp=fac[i].amount;
    if(tmp!=0)
    {
      if(tmp>=2)
        printf("%d^%d",fac[i].num,tmp);
      else
        printf("%d",fac[i]);
      if(i!=facI-1)      //控制“*”的输出量
        printf("*");
    }
  }
  system("pause");
  return 0;
}
时间: 2024-10-25 19:44:00

PAT:1059. Prime Factors (25) AC的相关文章

PAT 1059. Prime Factors (25)

1059. Prime Factors (25) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive inte

1059. Prime Factors (25)

时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2 *…*pm^km. Input Specification: Each input file contain

PAT:1071. Speech Patterns (25) AC

#include<ctype.h> #include<iostream> #include<map> #include<string> using namespace std; bool check(char a) //判断是否为有效字符 { if(isdigit(a) || isalpha(a)) return true; return false; } int main() { map<string,int> count; //单词与次数的映

PAT:1020. Tree Traversals (25) AC

#include<stdio.h> #include<stdlib.h> #include<queue> using namespace std; int POST[32]; //存放后序遍历 int IN[32]; //存放中序遍历 int n; //节点数 struct node { int data; node* l; node* r; }; node* creat(int postL,int postR,int inL,int inR) { if(postL&g

PAT:1063. Set Similarity (25) AC

#include<stdio.h> #include<set> using namespace std; set<int> str[51]; //最大51个集合 void compare(int s1,int s2) { int different=str[s1].size(),same=0; //初始化并集元素为s1元素个数,交集元素0个 for(set<int>::iterator it=str[s2].begin() ; it!=str[s2].end

PAT:1028. List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { char ID[10]; char name[10]; int gread; }STU[100010]; bool cmp1(Student a,Student b) { return strcmp(a.ID,b.ID)<0; } bool cmp2(Student a,Stu

PAT:1015. 德才论 (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { char mID[10]; int de,cai,sum; int tag; //标明是第几类:1德才都们组,2德胜才,3 }STU[100010]; bool cmp(Student a,Student b) //[warning]不能写STU { if(a.tag!=b.tag)

PAT:1083. List Grades (25) AC

#include<stdio.h> #include<algorithm> using namespace std; struct Student { char name[15]; char ID[15]; int gread; }STU[66666]; bool cmp(Student a,Student b) { return a.gread>b.gread; } int main() { int n; scanf("%d",&n); for(

pat1059. Prime Factors (25)

1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specificatio