上海耀乘健康科技公司笔试
两道算法
1.给平面三个点判断能组成多少个三角形。暴力n^3过了
2.给一个合数,求他的分解质因数,以每个因数的底数和指数输出,比如12 = 2 *2 * 3, 输出[[2,2],[3,1]] , 合数范围为2~2^31 - 1。
只过了20%, 太菜了,后面看题解学会了。代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n -- > 0){
int a = scanner.nextInt();
for (int i = 2 ; i <= a/i ; i++){
// i 为什么一定是质数? 因为如果是合数,应该被他的因子分解了已经。
if (a % i == 0){
int cnt = 0;
while (a % i == 0){
a /= i;
cnt++;
}
System.out.println(i + " " + cnt);
}
}
if(a > 1) System.out.println(a + " " + 1);
System.out.println();
}
}
}
1.给平面三个点判断能组成多少个三角形。暴力n^3过了
2.给一个合数,求他的分解质因数,以每个因数的底数和指数输出,比如12 = 2 *2 * 3, 输出[[2,2],[3,1]] , 合数范围为2~2^31 - 1。
只过了20%, 太菜了,后面看题解学会了。代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n -- > 0){
int a = scanner.nextInt();
for (int i = 2 ; i <= a/i ; i++){
// i 为什么一定是质数? 因为如果是合数,应该被他的因子分解了已经。
if (a % i == 0){
int cnt = 0;
while (a % i == 0){
a /= i;
cnt++;
}
System.out.println(i + " " + cnt);
}
}
if(a > 1) System.out.println(a + " " + 1);
System.out.println();
}
}
}
全部评论
请问有后续吗
是acm模式?
大佬的算法是在什么平台上学的
第二段代码有点问题把
相关推荐
点赞 评论 收藏
分享
10-31 13:04
南华大学 Java
嵌入式的小白:很多面试,面试前不会去打扰cto的,但一般cto不会在这些小事上刷人,只能说这个cto比较操心,啥重要不重要,紧急不紧急的,估计都会过问,平淡看待吧 点赞 评论 收藏
分享
