# PTA

## 2.1 综合小测验

ArrayList<Integer> list = new ArrayList<>();
Integer num[] = null;
Scanner sc = new Scanner(System.in);
String order;
while (sc.hasNext()) {
order = sc.next();
switch (order) {
case "fib":
int a = sc.nextInt();
for (int i = 0; i < a; i++) {
if (i == 0 || i == 1) {
continue;
}
list.add(list.get(i - 1) + list.get(i - 2));
}
for (int i = 0; i < a; i++) {
if(i==a-1) {
System.out.print(list.get(i));
break;
}
System.out.print(list.get(i) + " ");
}
list.clear();
System.out.println();
break;
case "sort":
while (sc.hasNextInt()) {
}
num = list.toArray(new Integer[0]);
Arrays.sort(num);
System.out.println(Arrays.toString(num));
list.clear();
break;
case "search":
int search=Arrays.binarySearch(num, sc.nextInt());
if(search<0)
System.out.println(-1);
else
System.out.println(search);
break;
case "getBirthDate":
int n=sc.nextInt();
for(int i=0;i<n;i++) {
String str=sc.next();
System.out.println(str.substring(6, 10)+"-"+str.substring(10, 12)+"-"+str.substring(12, 14));
}
break;
default:
System.out.print("exit");
System.exit(0);
break;
}

}


## 2.2 身份证排序

Scanner sc = new Scanner(System.in);
Integer num = sc.nextInt();
ArrayList<String> id = new ArrayList<>();
String temp;
for (int i = 0; i < num; i++) {
int flag=0;
temp=sc.next();
Integer age1,age2;
age1=Integer.valueOf(temp.substring(6,10));
for(int m=0;m<i;m++) {
age2=Integer.valueOf(id.get(m).substring(6,10));
if(age1<age2) {
break;
}
else if (age1.equals(age2)){
age1=Integer.valueOf(temp.substring(10,12));
age2=Integer.valueOf(id.get(m).substring(10,12));
if(age1<age2) {
break;
}
else if(age1.equals(age2)){
age1=Integer.valueOf(temp.substring(12,14));
age2=Integer.valueOf(id.get(m).substring(12,14));
if(age1<age2) {
break;
}
}
}
flag=1;
}
if(flag==1) {
}
if(id.isEmpty())
}
while(sc.hasNext()) {
String order=sc.next();
switch (order){
case "sort1":
for(int i=0;i<num;i++) {
System.out.println(id.get(i).substring(6,10)+"-"+id.get(i).substring(10,12)+"-"+id.get(i).substring(12,14));
}
break;
case "sort2":
for(int i=0;i<num;i++) {
System.out.println(id.get(i));
}
break;
default:
System.out.println("exit");
System.exit(0);
}
}


## 2.3 StringBuilder

Scanner sc=new Scanner(System.in);
while(sc.hasNextInt())
{
int n=sc.nextInt(),begin=sc.nextInt(),end=sc.nextInt();
StringBuilder nums=new StringBuilder("");
for(int i=0;i<n;i++) {
nums.append(i);
}
System.out.println(nums.substring(begin, end));
}


### 2.4 动态数组

Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
Integer num=sc.nextInt();
String[][] str=new String[num][];
for(int i=0;i<num;i++)
{
str[i]=new String[i+1];
for(int j=0;j<=i;j++)
{
str[i][j]=Integer.toString(i+1)+"*"+Integer.toString(j+1)+"="+Integer.toString((i+1)*(j+1));
System.out.print(str[i][j]);
if(i==j)
break;
if((i+1)*(j+1)<10)
{
System.out.print("  ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
System.out.println(Arrays.deepToString(str));
}


### 收获

posted @ 2021-09-23 23:58  51456  阅读(34)  评论(0编辑  收藏  举报