当前位置:网站首页>刷题-洛谷-P1075 质因数分解
刷题-洛谷-P1075 质因数分解
2022-07-25 13:32:00 【宋向上_UP】
P1075 质因数分解-C语言
1、题目

2、解题过程
(1)第一次 因为超时 写了太多的循环 特别是冒泡排序没必要
结果:
代码:
(2)第二次 去掉了冒泡排序 但还是超时
代码:
//洛谷 P1075 质因数分解
#include <stdio.h>
#define MAX 200
int main() {
int n;//正整数 1<=n<=2*10的9次方
int factor[MAX];//因数
int prime[MAX];//质数
int i, j, k;
int temp;//冒泡排序临时值
int counter = 0;//计数器 计因数个数
scanf("%d", &n);//输入正整数n
//求因数
for (i = 1; i <= n; i++) {
if (n % i == 0) {
factor[counter] = i;
counter++;
//printf("%d", i);//因数
}
}
k = 0;//质数下标
//求质数
for (i = 0; i < counter; i++) {
//要循环处理的因数个数
for (j = 2; j < factor[i]; j++) {
if (factor[i] % j == 0) {
//不是质数
break;
}
}
if (j >= factor[i]) {
//打印质数
prime[k] = factor[i];
k++;
//printf("\n %d ", factor[i]);
}
}
//printf("最大值:%d",prime[k] );
printf("%d", prime[k-1]);
return 0;
}
(3)第三次 这道题比较特别的就是题设中正整数N是两个不同质数的乘积。
结果:
代码:
//洛谷 P1075 质因数分解
#include <stdio.h>
int main() {
int n;//正整数 1<=n<=2*10的9次方
int i;
scanf("%d", &n);//输入正整数n
if (n == 1 || n==2 || n==3) {
printf("%d", n );
return 0;
}
//求质因数
for (i = 2; i <= n; i++) {
if (n % i == 0) {
printf("%d", n/i);//因数
break;
}
}
return 0;
}
边栏推荐
- ES6 array de duplication new set()
- stable_baselines快速入门
- hcip第九天笔记
- Mujoco+spinningup for intensive learning training quick start
- Programmer growth chapter 27: how to evaluate requirements priorities?
- Uncaught SyntaxError: Octal literals are not allowed in strict mode.
- 0719RHCSA
- Design and principle of thread pool
- QGIS loading online map: Gaode, Tiandi map, etc
- Excel record macro
猜你喜欢

Error: cannot find or load main class XXXX

VIM basic operation summary

Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem

uniapp处理后台传输图片

Generate SQL script file by initializing the latest warehousing time of vehicle attributes

好友让我看这段代码

【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)

Introduction to jupyter notebook

Excel add key run macro

电脑里一辈子都不想删的神仙软件
随机推荐
mujoco_ Py Chinese document
Shell common script: check whether a domain name and IP address are connected
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复
Leetcode 113. 路径总和 II
0716RHCSA
hcip第六天笔记
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
领域驱动模型设计与微服务架构落地-模型设计
并发编程之并发工具集
ThreadLocal&Fork/Join
【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
并发编程之阻塞队列
JS Array indexOf includes sort() 冒号排序 快速排序 去重和随机样本 random
Numpy简介和特点(一)
Redis可视化工具RDM安装包分享
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
stable_baselines快速入门
mujoco+spinningup进行强化学习训练快速入门
0710RHCSA