分解质因数 妖狐艹你老母 2024-03-26 20:25 10阅读 0赞 ## 分解质因数 ## 给定 n个正整数 ai,将每个数分解质因数,并按照质因数从小到大的顺序输出每个质因数的底数和指数。 输入格式 第一行包含整数 n。 接下来 n行,每行包含一个正整数 ai。 输出格式 对于每个正整数 ai,按照从小到大的顺序输出其分解质因数后,每个质因数的底数和指数,每个底数和指数占一行。 每个正整数的质因数全部输出完毕后,输出一个空行。 数据范围 1≤n≤100, 2≤ai≤2×109 输入样例: 2 6 8 输出样例: 2 1 3 1 2 3 **提交代码** **C++** #include<iostream> using namespace std; int divide(int n) { for (int i = 2; i <= n / i; ++ i) { int cnt = 0; if (n % i == 0) { while(n % i == 0) { cnt ++; n /= i; } cout << i << " " << cnt << endl; } } if (n != 1) cout << n << " " << 1 << endl; cout << endl; } int main() { int n; cin >> n; while(n --) { int t; cin >> t; divide(t); } return 0; } **Java** 重点是这个divide函数如何编写。 static void divide(int x) { for (int i = 2; i <= x / i; ++ i) { int cnt = 0; if (x % i == 0) { while(x % i == 0) { cnt ++; x /= i; } System.out.println(i + " " + cnt); } } if (x != 1) System.out.println(x + " " + 1); System.out.println(); } import java.io.*; public class Main { static void divide(int x) { for (int i = 2; i <= x / i; ++ i) { int cnt = 0; if (x % i == 0) { while(x % i == 0) { cnt ++; x /= i; } System.out.println(i + " " + cnt); } } if (x != 1) System.out.println(x + " " + 1); System.out.println(); } public static void main(String [] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); while(n -- > 0) { int t = Integer.parseInt(reader.readLine()); divide(t); } } }
相关 分解质因数 原理: ![13dda8e0e0e5492ea0a2a4912d8f48d8.png][] 做法: 按照枚举约数的做法去枚举质因子,在枚举到质因子的倍数之前先枚举到质因子 àì夳堔傛蜴生んèń/ 2024年03月30日 09:12/ 0 赞/ 66 阅读
相关 质因数分解 一道清华的复试题,我先后看了两份代码,收获匪浅,分别摘自下面两个博客: [https://blog.csdn.net/Little\_Kid\_Kang/article/de 深藏阁楼爱情的钟/ 2023年03月14日 05:54/ 0 赞/ 168 阅读
相关 分解质因数 上一篇博客:[质数的筛法][Link 1] > 写在前面:大家好!我是`AC-fun`,我的昵称来自两个单词`Accepted`和`fun`。我是一个热爱ACM的蒟蒻。如果 Love The Way You Lie/ 2022年10月22日 04:12/ 0 赞/ 241 阅读
相关 分解质因数 public class DecomposePrimeFactor \{ public final static int NUM = 154; public static vo Dear 丶/ 2022年09月30日 06:22/ 0 赞/ 237 阅读
相关 分解质因数 问题描述 求出区间\[a,b\]中所有整数的质因数分解。 输入格式 输入两个整数a,b。 输出格式 每行输出一个数的分解,形如k=a1\a2\a3...( 超、凢脫俗/ 2022年08月05日 02:54/ 0 赞/ 280 阅读
相关 分解质因数 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数。比如,6可以被分解为2x3,而24可以被分解为2x2x2x3。 灰太狼/ 2022年07月03日 18:20/ 0 赞/ 275 阅读
相关 分解质因数 分解质因数 当无法分解是输出“no answer” <table style="font-size:12px; color:rgb(51,51,51); line-heig 淩亂°似流年/ 2022年06月06日 11:42/ 0 赞/ 301 阅读
相关 分解质因数 问题描述 求出区间\[a,b\]中所有整数的质因数分解。 输入格式 输入两个整数a,b。 输出格式 每行输出一个数的分解,形如k=a1\a2\a3...( 电玩女神/ 2022年06月01日 13:52/ 0 赞/ 307 阅读
相关 分解质因数 void solution(int num) { int i = 2; while (num != 1) { i ╰半夏微凉°/ 2022年05月09日 01:46/ 0 赞/ 339 阅读
还没有评论,来说两句吧...