这个问题叫做质因子分解,花了大概两个小时写对了。这道题细节挺多的,书上提到了几点,一个是n=1的话需要特判。有一个很容易错的点就是n一开始要先用一个变量保存起来,不保存的话后面有点麻烦,所以建议还是先保存起来。因为过程中要不断的改变n,最后还要打印出n,如果n为1的话还要特殊处理。当然也可以一开始处理打印,不过我觉得和思维颠倒有些难受。一个很重要的问题就是打印的素数表需要多大,这个我也不清楚怎么办,所以先写出来看,随机应变,如果不能完全覆盖的话再往上加,最后完全覆盖了那个样例。最后解决的细节是当我输入2的时候一开始只探测到根号2导致问题没有解。其实根本没必要,因为有break来控制着for循环的结束条件。所以i<num的所有数字都正常输出就好。

最重要的思想就是把这个数分解成两种情况,一种是都小于sqrt(n)的很多个因子,另一种是一些小于sqrt(n)然后 只有一个 大于sqrt(n)的两部分。

一个很重要的思想就是设计结构体以及结构体数组,这个思想还是挺重要的。

#include<cstdio>
#include<math.h>
const int maxn = ;
using namespace std;
struct factor
{
int x;
int cnt = ;
}fac[];
int num = ;
bool p[] = { };
int prime[];
void findprime()//获取素数表
{
int i, j;
for (i = ; i *i< maxn; i++)
{
if (p[i] == false)
{
prime[num] = i;
num++;
for (j = i + i; j *j < maxn; j += i)
{
p[j] = true;
}
}
}
}
int main()
{
int n;
int temp;
scanf("%d", &n);
temp = n;
findprime();
int i;
int sum = ;
for (i = ; i<num; i++)
{
int x = n;
while (n % prime[i] == )
{
n /= prime[i];
fac[sum].x = prime[i];
fac[sum].cnt++;
}
if (x%prime[i] == )
{
sum++;
}
if (n == )
break;
}
if (n != )
{
fac[sum].x = n;
fac[sum].cnt = ;
}
printf("%d=", temp);
if (temp == )
{
printf("1\n");
}
else
for (i = ; i < sum; i++)
{
if (i != sum - && fac[i].cnt > )//不是最后一个
{
printf("%d^%d*", fac[i].x, fac[i].cnt);
}
else if (i != sum - && fac[i].cnt == )
{
printf("%d*", fac[i].x);
}
else if (i == sum - && fac[i].cnt == )//是最后一个且cnt只有一个
{
printf("%d\n", fac[i].x);
}
else//最后一个cnt有两个
printf("%d^%d\n", fac[i].x, fac[i].cnt);
}
}

patA1059 Prime Factors的更多相关文章

  1. [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

    7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...

  2. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  3. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  4. 2014辽宁ACM省赛 Prime Factors

    问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...

  5. PAT1059:Prime Factors

    1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  6. A1059. Prime Factors

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

  7. PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...

  8. PAT 1059 Prime Factors[难]

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

  9. pat1059. Prime Factors (25)

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

随机推荐

  1. Angular组件——投影

    运行时动态改变组件模版的内容.没路由那么复杂,只是一段html,没有业务逻辑. ngContent指令将父组件模版上的任意片段投影到子组件上. 一.简单例子 1.子组件中使用<ng-conten ...

  2. 【Android】自动测试工具 Monkey

    前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.其 ...

  3. 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)

    原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...

  4. 动态规划——Burst Ballons

    题意:给定n个气球.每次你可以打破一个,打破第i个,那么你会获得nums[left] * nums[i] * nums[right]个积分. (nums[-1] = nums[n] = 1)求你可以获 ...

  5. python class属性

    代码一: class A(object): pass a = A() a.name = "class_A" print(a.name) #class_A 代码二:class A(o ...

  6. Android项目中的config.xml文件 “config.xml”

    Android应用程序需要保存一些配置时,可以将这些配置项放置到values/config.xml文件中. 实例分析: <?xml version="1.0" encodin ...

  7. SDOI2018:荣誉称号

    题解: https://files.cnblogs.com/files/clrs97/title-solution.pdf Code: #include<cstdio> #include& ...

  8. TestNG 中DataProvider 的用法

    使用DataProvider提供数据有两种形式: 第一种:一种是在测试代码和测试数据放在同一个类中: 第二种:把所有的数据提供都单独写在一个类里面,当测试数据比较多时,这种方法利于维护. DataPr ...

  9. Java_泛型

    转自博客HappyCorn https://www.cnblogs.com/lwbqqyumidi/p/3837629.html 什么是泛型? 泛型,即“参数化类型”.一提到参数,最熟悉的就是定义方法 ...

  10. JavaFTP文件传输上传和下载文件

    首先在电脑上创建一个ftp服务器,具体步骤自行百度. 接下来开始写有用的java连接TFP站点和传输文件的代码. 1.首先jar用的是apache 的工具包 请自行下载 . 2.俩个文件代码 一个Ft ...