PAT A1059

标签(空格分隔): PAT


解题思路 :先打印出素数表。利用结构体数组来存贮质因子的值和个数

strcut factor{
int x; //值
int cnt; //个数
}fac[20];
  • 如果i是x的质因子,就令fac[index].x = i(index是某个下标), fac[index].cnt = 0,然后 n = n / i。若又遇到i是x的质因子,则cnt++。
  • 进行完上述操作后,n != 1,则说明n还有一个大于sqrt(n)的质因子(可能就是sqrt(n)),我们需要加入这个值到fac[index]中,在将fac[index].cnt设为1;
#include <cstdio>
#include <math.h> const int maxn = 100010;
bool is_prime(int n) {
if(n == 1) return false;
int sqr = (int)sqrt(1.0 * n);
for(int i = 2; i <= sqr; i++) {
if(n % i == 0) return false;
}
return true;
}
int prime[maxn], pNum;
void Find_Prime() {
for(int i = 1; i < maxn; i++) {
if(is_prime(i) == true){
prime[pNum++] = i;
}
}
}
struct factor {
int x, cnt;
}fac[10]; int main() {
Find_Prime();
int n, num = 0;
scanf("%d", &n);
if (n == 1) printf("1=1");
else {
printf("%d=", n);
int sqr = (int)sqrt(1.0 * n);
for(int i = 0; i < pNum; i++) {
if(n % prime[i] == 0) {
fac[num].x = prime[i];
fac[num].cnt = 0;
while(n % prime[i] == 0) {
fac[num].cnt++;
n /= prime[i];
}
num++;
}
if(n == 1) break;
}
if(n != 1) {
fac[num].x = n;
fac[num].cnt = 1;
}
for(int i = 0; i < num; i++) {
if(i > 0) printf("*");
printf("%d", fac[i].x);
if(fac[i].cnt > 1) {
printf("^%d", fac[i].cnt);
}
}
}
return 0;
}

启示 :边写边测试,我写过用埃氏筛法得到素数表的时候,发现最后的结果是错的,

结果我验证发现我的素数表是错误的,但埃氏筛法肯定是没有写错了,最后为什么素数表是错的,原因我也不知道,但是这可以为我后来做题提供经验,写出一个模块就验证一下功能。

PAT A1059的更多相关文章

  1. PAT_A1059#Prime Factors

    Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...

  2. PAT甲级——A1059 Prime Factors

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

  3. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  4. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  5. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  6. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  7. PAT 1041. 考试座位号(15)

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

  8. PAT 1040. 有几个PAT(25)

    字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问 ...

  9. PAT 1032. 挖掘机技术哪家强(20)

    为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...

随机推荐

  1. C# 批量新增的两种方法。

    public class Test { private static readonly string strConnection = ""; public static void ...

  2. leecode第二百三十题(二叉搜索树中第K小的元素)

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  3. introduce explaining variable 引入解释变量

    一段复杂的计算的表达式(一般 逻辑判断  if(a!=1 && b!=Null && a>b  ) && .... 直接在代码中参与到 代码的逻辑 ...

  4. leetcode刷题——一些算法技巧总结1.0

    运算符优先级,简单记就是:! > 算术运算符 > 关系运算符 > && > || > 赋值运算符 把数字取反,可以作为一种标记 pythonlast = ...

  5. HBuild 连接安卓手机

    设备:一部电脑.一部安卓手机.一条数据线 1.  数据线连接电脑和安卓手机: 2.  安卓手机-->  设置 -- > 开发者选项 --> 点进去,找到USB调试并且打开,例:    ...

  6. Python的类的下划线命名的区别

    首先,单下划线开头,这个常被用于模块中,在一个模块中以单下划线开头的变量和函数被默认当做内部函数,如果使用from  module  import  *导入时,这部分变量和函数不会被导入.注意,如果使 ...

  7. C#数组--(Array类的属性和方法)

    Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义.Array 类提供了各种用于数组的属性和方法,可看作扩充了功能的数组(但不等同数组),可以使用Array类的属性来对数组 ...

  8. 当你的layui表格要做全选+删除功能【兼容ie8】

    <!-- 全选 --> <div class="choose"> <input type="checkbox" id=" ...

  9. 【转】IIS - 自动申请、部署Let's Encrypt的免费SSL证书

    IIS - 自动申请.部署Let's Encrypt的免费SSL证书(让网站实现HTTPS协议) 2017-12-19发布:hangge阅读:161   一.HTTPS 协议介绍 1,什么是 HTTP ...

  10. Python 计算π及进度条显示

    一,首先打开命令提示符找到Python路径,输入pip install tqdm下载第三方库tpdm. 二,写程序 法一 from math import * from tqdm import tqd ...