题意
大概是说,输入多行,每行一个数,然后分解成素数相乘的形式,并以7段数码管的显示方式打印。
示例
注意点
分解的时候,可能有超过9的素数因子,比如13,需要转换成1和3进行打印
代码
<code class="language-java hljs has-numbering"><span class="hljs-keyword">import</span> java.io.*;
<span class="hljs-keyword">import</span> java.util.*;
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> {</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">char</span>[][] numbers = {
{<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>},
{<span class="hljs-string">' '</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>},
{<span class="hljs-string">'*'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>},
{<span class="hljs-string">' '</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">'|'</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">'|'</span>},
{<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>, <span class="hljs-string">' '</span>,<span class="hljs-string">'-'</span>,<span class="hljs-string">' '</span>}};
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String args[]) {
Scanner cin = <span class="hljs-keyword">new</span> Scanner(System.in);
<span class="hljs-keyword">while</span>(cin.hasNextInt()){
<span class="hljs-keyword">int</span> k = cin.nextInt();
printNum(getYinshu(k));
}
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printNum</span>(List<Integer> nums){
<span class="hljs-keyword">int</span> k = nums.size() - <span class="hljs-number">1</span>;
<span class="hljs-keyword">if</span>(k==<span class="hljs-number">0</span>){
printPrime(nums.get(<span class="hljs-number">0</span>));
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">5</span>; i++) {
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> m = <span class="hljs-number">0</span>;m<nums.size()-<span class="hljs-number">1</span>;m++){
<span class="hljs-keyword">int</span> orin = nums.get(m);
<span class="hljs-keyword">int</span> n = Math.abs(orin);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>; j < <span class="hljs-number">3</span> * (n + <span class="hljs-number">1</span>) + <span class="hljs-number">1</span>; j++) {
System.out.print(numbers[i][j]);
}
<span class="hljs-keyword">if</span>(orin><span class="hljs-number">0</span>){<span class="hljs-comment">//超过9的质数</span>
<span class="hljs-keyword">if</span>(i==<span class="hljs-number">2</span>){
System.out.print(<span class="hljs-string">'*'</span>);
}
<span class="hljs-keyword">else</span>{
System.out.print(<span class="hljs-string">' '</span>);
}
}
}
<span class="hljs-keyword">int</span> n = nums.get(k);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>; j < <span class="hljs-number">3</span> * (n + <span class="hljs-number">1</span>) + <span class="hljs-number">1</span>; j++) {
System.out.print(numbers[i][j]);
}
System.out.println();
}
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printPrime</span>(List<Integer> nums){
<span class="hljs-keyword">int</span> k = nums.size() - <span class="hljs-number">1</span>;
<span class="hljs-keyword">if</span>(k==<span class="hljs-number">0</span>){
printPrime(nums.get(<span class="hljs-number">0</span>));
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">5</span>; i++) {
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> m = <span class="hljs-number">0</span>;m<nums.size();m++){
<span class="hljs-keyword">int</span> n = nums.get(m);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>; j < <span class="hljs-number">3</span> * (n + <span class="hljs-number">1</span>) + <span class="hljs-number">1</span>; j++) {
System.out.print(numbers[i][j]);
}
}
System.out.println();
}
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printPrime</span>(<span class="hljs-keyword">int</span> num){
String str = String.valueOf(num);
<span class="hljs-keyword">char</span> c;
<span class="hljs-keyword">int</span> n;
List<Integer> result = <span class="hljs-keyword">new</span> ArrayList<Integer>();
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<str.length();i++){
c = str.charAt(i);
n = Integer.valueOf(String.valueOf(c));
result.add(n);
}
printPrime(result);
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List<Integer> <span class="hljs-title">getYinshu</span>(<span class="hljs-keyword">int</span> num){
List<Integer> result = <span class="hljs-keyword">new</span> ArrayList<Integer>();
<span class="hljs-keyword">int</span> prime = <span class="hljs-number">2</span>;
<span class="hljs-keyword">while</span> (prime <= num) {
<span class="hljs-keyword">if</span> (prime == num) {
<span class="hljs-comment">//result.add(num);</span>
<span class="hljs-keyword">if</span>(num><span class="hljs-number">9</span>){
<span class="hljs-comment">//超过9,进行分割</span>
String str = String.valueOf(num);
<span class="hljs-keyword">char</span> c;
<span class="hljs-keyword">int</span> n;
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<str.length()-<span class="hljs-number">1</span>;i++){
c = str.charAt(i);
n = Integer.valueOf(String.valueOf(c));
result.add(-<span class="hljs-number">1</span>*n);
}
c = str.charAt(str.length()-<span class="hljs-number">1</span>);
n = Integer.valueOf(String.valueOf(c));
result.add(n);
}
<span class="hljs-keyword">else</span>{
result.add(num);
}
<span class="hljs-keyword">break</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (num % prime == <span class="hljs-number">0</span>) {
<span class="hljs-keyword">if</span>(prime><span class="hljs-number">9</span>){
<span class="hljs-comment">//超过9,进行分割</span>
String str = String.valueOf(prime);
<span class="hljs-keyword">char</span> c;
<span class="hljs-keyword">int</span> n;
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<str.length()-<span class="hljs-number">1</span>;i++){
c = str.charAt(i);
n = Integer.valueOf(String.valueOf(c));
result.add(-<span class="hljs-number">1</span>*n);
}
c = str.charAt(str.length()-<span class="hljs-number">1</span>);
n = Integer.valueOf(String.valueOf(c));
result.add(n);
}
<span class="hljs-keyword">else</span>{
result.add(prime);
}
num = num / prime;
} <span class="hljs-keyword">else</span> {
prime++;
}
}
<span class="hljs-keyword">return</span> result;
}
}</code>