当前位置:网站首页>刷题-洛谷-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;
}
边栏推荐
- 从输入网址到网页显示
- [ai4code final chapter] alphacode: competition level code generation with alphacode (deepmind)
- 并发编程 — 内存模型 JMM
- Shell common script: judge whether the file of the remote host exists
- Excel import and export source code analysis
- [figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)
- Discussion on principle and application technology of MLIR
- Excel record macro
- Sword finger offer special assault edition day 10
- Generate SQL script file by initializing the latest warehousing time of vehicle attributes
猜你喜欢

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

Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board

机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理

0710RHCSA

Shell common script: check whether a domain name and IP address are connected

Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions

In order to improve efficiency, there are various problems when using parallelstream

基于百问网IMX6ULL_PRO开发板移植LCD多点触摸驱动(GT911)

The interviewer asked me: how much do you know about MySQL's storage engine?
随机推荐
hcip第十天笔记
全网最简单解决方式1045-Access denied for user [email protected](using password:YES)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
ESP32-C3 基于Arduino框架下Blinker点灯控制10路开关或继电器组
Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
stable_ Baselines quick start
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
互斥锁、自旋锁、读写锁……理清它们的区别和应用
一味地做大元宇宙的规模,已经背离了元宇宙本该有的发展逻辑
运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
卷积神经网络模型之——AlexNet网络结构与代码实现
Hcip seventh day notes
Redis可视化工具RDM安装包分享
Convolutional neural network model -- lenet network structure and code implementation
并发编程之并发工具集
0717RHCSA
Convolutional neural network model -- vgg-16 network structure and code implementation
Hcip day 10 notes