这周通过自己学会,写出了一道codeforces上的一道题。
题目:A. Sum of Round Numbers
描述:
A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 11 to 99 (inclusive) are round.
For example, the following numbers are round: 40004000, 11, 99, 800800, 9090. The following numbers are not round: 110110, 707707, 222222, 10011001.
You are given a positive integer nn (1≤n≤1041≤n≤104). Represent the number nn as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number nn as a sum of the least number of terms, each of which is a round number.
Input
The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input. Then tt test cases follow.
Each test case is a line containing an integer nn (1≤n≤1041≤n≤104).
Output
Print tt answers to the test cases. Each answer must begin with an integer kk — the minimum number of summands. Next, kk terms must follow, each of which is a round number, and their sum is nn. The terms can be printed in any order. If there are several answers, print any of them.
input:
5
5009
7
9876
10000
10
output:
2
5000 9
1
7
4
800 70 6 9000
1
10000
1
10
import java.util.*;
public class HAHAHAHAH {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int arr[]=new int [n];
for(int i=0;i<=arr.length;i++) {
arr[i]=scanner.nextInt();
if(arr[i]<10) {
System.out.println(1);
System.out.println(arr[i]);
}
if(arr[i]>=10&&arr[i]<100) {
if(arr[i]%10==0) {
System.out.println(1);
System.out.println(arr[i]);
}else{
System.out.println(2);
System.out.print(arr[i]-arr[i]%10+" ");
System.out.println(arr[i]%10);
}
}
if(arr[i]>=100&&arr[i]<1000) {
if(arr[i]%100==0) {
System.out.println(1);
System.out.println(arr[i]);
}else if(arr[i]%10==0) {
System.out.println(2);
System.out.print(arr[i]-arr[i]%100+" ");
System.out.println(arr[i]%100);
}else if(arr[i]%10>=1&&arr[i]%10<=9&&arr[i]%10>10&&arr[i]%10<=99) {
System.out.println(2);
System.out.print(arr[i]-arr[i]%100+" ");
System.out.println(arr[i]%100);
}
else {
System.out.println(3);
System.out.print(arr[i]-arr[i]%100+" ");
System.out.print((arr[i]%100)-(arr[i]%100%10)+" ");
System.out.println(arr[i]%100%10+"\t");
}
}
if(arr[i]>=1000&&arr[i]<=10000) {
if(arr[i]%1000==0||arr[i]%10000==0) {
System.out.println(1);
System.out.println(arr[i]);
}else if(arr[i]%100==0) {
System.out.println(2);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.println(arr[i]%1000-arr[i]%100+" ");
}else if(arr[i]%1000>=1&&arr[i]%1000<=9) {
System.out.println(2);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.println(arr[i]%1000);
}else if((arr[i]%1000)%10==0&&arr[i]%1000!=0) {
System.out.println(2);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.println(arr[i]%1000);
}else if(arr[i]%1000!=0&&arr[i]%1000>=1&&arr[i]%1000<=99) {
System.out.println(3);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.print(arr[i]%1000-arr[i]%10+" ");
System.out.println(arr[i]%10);
}else if(arr[i]%10!=0&&arr[i]%10>=1&&arr[i]%10<=9&&arr[i]%1000>=10&&arr[i]%1000<=99) {
System.out.println(3);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.print(arr[i]%1000-arr[i]%10+" ");
System.out.println(arr[i]%10);
}
else if(arr[i]%10==0) {
System.out.println(3);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.print(arr[i]%1000-arr[i]%100+" ");
System.out.println(arr[i]%100-arr[i]%10+" ");
}else {
System.out.println(4);
System.out.print(arr[i]-arr[i]%1000+" ");
System.out.print(arr[i]%1000-arr[i]%100+" ");
System.out.print(arr[i]%100-arr[i]%10+" ");
System.out.println(arr[i]%10);
}
}
}
}
}
我这个算法可能有问题,不够简介,重复项也太多,不是一个好代码,我会继续努力。进行优化,早日拿出好成绩!