当前位置:网站首页>8.整数分解
8.整数分解
2022-04-22 05:34:00 【linsa_pursuer】
给你一个整数n,将它拆分成他所有最小因子(大于1)的连乘形式,同时遵守最小因子优先的原则,小的因子在前面,如n=6,则输出2*3=6。
package com;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
while (s.hasNextInt()){
System.out.println(mul(s.nextInt()));
}
}
public static String mul(int num){
StringBuilder sb = new StringBuilder();
String number = String.valueOf(num);
List<Integer> list = new LinkedList<Integer>();
for(int i=2;i<=num;i++){
while(num%i==0){
num/=i;
list.add(i);
}
}
for(int i=0;i<list.size();i++){
if(i==(list.size()-1)){
sb.append(list.get(i));
}else{
sb.append(list.get(i));
sb.append("*");
}
}
sb.append("=");
sb.append(number);
return sb.toString();
}
}
如:
输入:
5
6
24
输出:
5=5
2*3=6
2*2*2*3=24
版权声明
本文为[linsa_pursuer]所创,转载请带上原文链接,感谢
https://blog.csdn.net/linsa_pursuer/article/details/122051792
边栏推荐
- Arrays常用方法(超详解)
- Establishment, addition, deletion, modification and query of sequence table
- Unsafe row of spark
- MySQL表约束和表设计
- 随机字符串工具类RandomStringUtils详解
- GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.1 (1)
- Homebrew的基本使用与常见异常
- C language version: establishment of circular linked list
- Linux下安装Mysql8
- New tips for JS in 2022
猜你喜欢

Fundamentals of graphics - Ambient Occlusion

C language version: dynamic establishment of binary tree

判断链表是否有环

Auto. JS canvas setting anti aliasing paint setAntiAlias(true);

AssetBundle packaging based on unitygameframework framework

MySQL index

Using easyexcel to export excel forms

Mycat的读写分离

Codeforces Round #784 (Div. 4) All Problems

认识和安装MySQL
随机推荐
Fundamentals of graphics - depth buffer
认识和安装MySQL
C language version: establishment and basic operation of two-way linked list
Fundamentals of Unreal Engine Programming (II)
The nearest common ancestor problem of sequentially stored binary trees | PTA
Codeforces round 783 (Div. 2) d - optimal partition (DP / weight segment tree 2100)
Topology optimization of heat transfer based on finite volume method
List中set方法和add方法的区别
MySQL数据库基础
GBase 8s V8. 8 SQL Guide: tutorial-6.1
Keil-C51 与 Keil -ARM 共存的方法
Unity first order Bezier curve for throwing objects
New tips for JS in 2022
How to use U deep boot U disk to clear the system login password
MySQL表约束和表设计
C language practice (2) -- polynomial addition with linked list
使用easyexcel导出excel表格
GBase 8s V8.8 SQL 指南:教程-5.2(3)
idea2021. 1. When writing SQL in mapper: unable to resolve column / table
Five important properties of binary tree