第一题(60分):
按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”
代码的算法效率可能实在是低,如果写的不好,请给一些好的建议,这是刚写出来的,还没有优化算法。明天再做了。
具体代码:
import java.util.Scanner;
public class StringIndictorOutput {
public static void main(String []args){
System.out.println("请输入你要输入的几个数字:");
Scanner scanner1 = new Scanner(System.in);
int M = scanner1.nextInt();
System.out.println("请输入你要数字字符串的位数:");
Scanner scanner2 = new Scanner(System.in);
int N = scanner2.nextInt();
for(int i=0;i
System.out.println("请输入字符串:");
Scanner scanner3 = new Scanner(System.in);
String string = scanner3.nextLine();
int length = string.length();
if(length
String str = string;
for(int j=0;j<8-length;j++){
str+=0;
}
System.out.println( str);
}
else if (length==N) {
System.out.println(string);
}
else {
int yushu = (int)(length/N);
int begin=0,end=N;
int h;
for(h=0;h
String string1 = null;
string1 = string.substring(begin, end);
System.out.println(string1+" ");
begin = begin+N;
end = end+N;
}
if(h==yushu){
String string2 = null;
string2 = string.substring(end-N);
System.out.println(string2);
}
}
}
}
}