功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )
数据范围:
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ long num = sc.nextLong(); long num2 = (long)Math.sqrt(num); for(int i = 2; i <= num2; i++){ while(num % i == 0){ num /= i; System.out.print(i + " "); } } System.out.println(num == 1 ? "" : num + " "); } sc.close(); } }
public static void main(String[] args){ Scanner sc = new Scanner(System.in); long s = sc.nextLong(); int temp = 2; StringJoiner str= new StringJoiner(" "); while(temp<=s){ if(s%temp==0){ if(temp==s){ str.add(temp+""); break; }else{ str.add(temp+""); s=s/temp; } } else { temp++; } } }这个 是啥问题
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ long num = scanner.nextLong(); if(num <= 3){ System.out.println(num); continue; } for (int i = 2; i <= num; i++) { while (num % i == 0) { System.out.println(i + " "); num /= i; } } } }
#include <stdio.h> #define ARR_LEN 200 #define TABLE_LEN 10000 static char ans[ARR_LEN]; static long numTable[TABLE_LEN] = {2,}; static int numTableLen = 1; char* getResult(long ulDataInput) { int lenAns = 0; memset(ans,0,sizeof(char) * ARR_LEN); while(1 != ulDataInput) { while(0 == ulDataInput%numTable[numTableLen - 1]) { int end = lenAns; long num = numTable[numTableLen - 1]; ans[end] = ' '; end++; while(num) { ans[end] = num%10 + '0'; num = num/10; end++; } for(int i = 0; i < (end - lenAns)/2; i++) { char tmp = ans[lenAns + i]; ans[lenAns + i] = ans[end - 1 - i]; ans[end - 1 - i] = tmp; } lenAns = end; ulDataInput /= numTable[numTableLen - 1]; } if(ulDataInput > numTable[numTableLen - 1]) { for(long i = numTable[numTableLen - 1] + 1; i <= ulDataInput; i++) { int flag = 1; for(int j = 0; j < numTableLen; j++) { if(0 == i%numTable[j]) { flag = 0; break; } } if(flag) { numTable[numTableLen] = i; numTableLen++; break; } } } } ans[lenAns] = '\0'; return ans; } int main() { long n = 0; scanf("%d",&n); printf("%s\n",getResult(n)); return 0; }
//第一次用递归解决了问题,纪念一哈
#include<stdio.h>
int z(long);//判断质数 int z(long a)
{
for(int i=2;i<a;i++)
{
if(a%i==0)
return 0;
}
return 1;
}
void y(long);//递归找因子
void y(long a)
{
int k=2;
while(a%k!=0&&k<a){
k++;
}
if(z(k)==1) printf("%d ",k);
if(k<a) y(a/k);
}
int main()
{
long x;
while(scanf("%d",&x)!=EOF){
y(x);
}
return 0;
}
#include <stdio.h> #include <string.h> #define N 100 int main() { long i; int j; long n; char over; long devisor[N]; scanf("%ld", &n); j = 0; while(1){ over = 1; for(i=2 ; i<=(n+1)/2 ; i++){ if(n%i == 0){ devisor[j++] = i; n /= i; over = 0; break; } } if(over) break; } for(i=0 ; i<j ; i++){ printf("%ld ", devisor[i]); } printf("%ld \n", n); }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); Integer num = Integer.valueOf(bufferedReader.readLine()); //System.out.println("num = " + num); for (int i = 2; i <= num; i++){ if (num % i == 0){ System.out.print(i + " "); num = num / i; //重新赋值 i = 1; //重新遍历 } } bufferedReader.close(); } }