patA1059 Prime Factors
这个问题叫做质因子分解,花了大概两个小时写对了。这道题细节挺多的,书上提到了几点,一个是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的更多相关文章
- [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. 这道题跟 ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 2014辽宁ACM省赛 Prime Factors
问题 L: Prime Factors 时间限制: 1 Sec 内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...
- PAT1059:Prime Factors
1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- A1059. Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- pat1059. Prime Factors (25)
1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
随机推荐
- HTTP 403 ,tomcat配置HTTPS,无法访问 返回状态码HTTP 403
为了将本机(windows系统)启动的应用以HTTPS的形式访问, 利用Keytool 生成证书之后.在tomcat的server.xml中将配置修改为如下: <Connector port=& ...
- RN页面获取组件位置和大小的方法
在RN的页面布局和操作中,有时需要获取元素的大小和位置信息,本文从网上抄袭了几个常用方法,以备不时之需. 首先是获取设备屏幕的宽高 import {Dimensions} from 'react-na ...
- [转] 微信小程序页面间通信的5种方式
微信小程序页面间通的5种方式 PageModel(页面模型)对小程序而言是很重要的一个概念,从app.json中也可以看到,小程序就是由一个个页面组成的. 如上图,这是一个常见结构的小程序:首页是一个 ...
- 使用Type.MakeGenericType,反射构造泛型类型
有时我们会有通过反射来动态构造泛型类型的需求,该如何实现呢?举个栗子,比如我们常常定义的泛型委托Func<in T, out TResult>,当T或TResult的类型需要根据程序上下文 ...
- Python3中的真值测试
1. 真值测试 所谓真值测试,是指当一种类型对象出现在if或者while条件语句中时,对象值表现为True或者False.弄清楚各种情况下的真值对我们编写程序有重要的意义. 对于一个对象a,其真值定义 ...
- aop切入mapper接口
***************************************分割线****************************************************** 参考: ...
- Imcash平台测评报告
ImCash是由全球知名量子基金(QuantumFund)与美国好事达保险公司 (ALL ) 联合投资美国区块链金融资本(BFC)打造全球首款量子基金数字资产服务平台 . ImCash作为全球首款量子 ...
- Cow Relays POJ - 3613 (floyd+快速幂)
For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race usin ...
- Wxpython入门
Wxpython入门 api文档以及中文教程: https://pan.baidu.com/s/1TDTgHg9Mwc74ODQy68YnlQ 提取码:354n 入门示例 frame=wx.Frame ...
- jsp填坑:找不到属性
javax.el.PropertyNotFoundException: Property [***] not found on type 接手的项目的页面是用jsp写的,虽然再有十几天就2019年了, ...