当前位置:网站首页>867. Decomposition prime factor
867. Decomposition prime factor
2022-07-21 19:59:00 【Hunter_ Kevin】
subject :
Given n A positive integer ai, Decompose each number into prime factors , And output the base and index of each quality factor from small to large .
Input format
The first line contains integers n.
Next n That's ok , Each line contains a positive integer ai.
Output format
For each positive integer ai, Output the decomposition quality factor in the order from small to large , The base and exponent of each prime factor , One line for each base and index .
After all the prime factors of each positive integer are output , Output a blank line .
Data range
1≤n≤100,
2≤ai≤2×109
sample input :
2
6
8
sample output :
2 1
3 1
2 3
Code :
#include <iostream>
using namespace std;
void divide(int x)
{
for(int i = 2; i <= x / i; i++)// from 2 Start traversing prime and composite numbers
if(x % i == 0)// If you can divide Because from 2 Start traversing from small to large , therefore i It must be prime , Because if it is a composite number , It must have been decomposed into qualitative factors for statistics before
{
int cnt = 0;// Count the number of times the current prime factor is divided
while(x % i == 0)// Always remove quality factor
{
cnt++;
x/=i;
}
cout<<i<<' '<<cnt<<endl;
}
if(x != 1)// If the prime factor is removed , that x Finally, it must be 1, Otherwise, it means x Itself is a prime number , Only oneself and 1 Two factors
cout<<x<<' '<<1<<endl;
puts("");// Output blank lines
}
int main()
{
int n;
cin>>n;
while(n --)
{
int x;
cin>>x;
divide(x);
}
return 0;
}
边栏推荐
猜你喜欢
3D数学之三角公式
ECCV 2022 open source | target segmentation for 10000 frames of video
uniapp开发app解决uni.pageScrollTo不生效
FL Studio 20.9水果编曲软件中文汉化补丁包
全志A40i开发板硬件说明书——100%国产+工业级方案(中)
Trigonometric formula of 3D mathematics
让你告别996的项目维护方式
为什么要写单元测试?如何写单元测试?
(4) Transactions
Iwemeta: Shi Yuzhu's golden sauce wine: OEM factories have been shut down. Who is producing it?
随机推荐
3D数学之三角公式
Wechat applet payment
全志A40i开发板硬件说明书——100%国产+工业级方案(下)
STM32——ADC读取光敏传感器控制LED灯,看门狗中断
常用测试用例设计方法之边界值分析法
UML模型和类的六大关系
Why write unit tests? How to write unit tests?
ctfshow MengXIn misc1
STM32 - general timer control ultrasonic sensor hcsr04
字符函数和内存函数
Assertion failed: inputs.at(2).is_weights
QT notes - qmetaenum class
【transformer】ViT
[practice C] find out the single dog
2022.7.18DAY609
ECCV 2022 open source | target segmentation for 10000 frames of video
从20s优化到500ms,我是如何做到的
Bluetooth intelligent nutrition electronic scale solution
Common tools and test methods for interface testing
为什么要写单元测试?如何写单元测试?