当前位置:网站首页>刷题-洛谷-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;
}
边栏推荐
- Hcip day 8 notes
- Excel录制宏
- Convolutional neural network model -- vgg-16 network structure and code implementation
- Shell common script: judge whether the file of the remote host exists
- 【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)
- How to refactor embedded code?
- pycharm不能输入中文解决方法
- 并发编程之并发工具集
- Mlx90640 infrared thermal imager temperature sensor module development notes (V)
- 【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
猜你喜欢

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

VIM tip: always show line numbers

为提高效率使用ParallelStream竟出现各种问题

电脑里一辈子都不想删的神仙软件

6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略

Convolutional neural network model -- alexnet network structure and code implementation

Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1

卷积神经网络模型之——LeNet网络结构与代码实现

Excel添加按键运行宏

ThreadLocal&Fork/Join
随机推荐
卷积神经网络模型之——AlexNet网络结构与代码实现
0719RHCSA
Django 2 ----- database and admin
机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
Django 2 ----- 数据库与Admin
Pycharm cannot input Chinese solution
运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
0719RHCSA
Friends let me see this code
[six articles talk about scalablegnn] around www 2022 best paper PASCA
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
Shell common script: judge whether the file of the remote host exists
Mutex lock, spin lock, read-write lock... Clarify their differences and applications
Blindly expanding the scale of the meta universe has deviated from the development logic of the meta universe
Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
并发编程之并发工具集
2022全球开发者中,你的收入排多少?
并发编程之阻塞队列