请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如
输入:EricZ
输出:1=EricZ
输入:David
输出:1=David 2=EricZ
输入:Peter
输出:1=Peter 2=David 3=EricZ
输入:Alan
输出:1=Alan 2=Peter 3=David 4=EricZ
输入:Jane
输出:1=Jane 2=Alan 3=Peter 4=David
请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如
输入:EricZ
输出:1=EricZ
输入:David
输出:1=David 2=EricZ
输入:Peter
输出:1=Peter 2=David 3=EricZ
输入:Alan
输出:1=Alan 2=Peter 3=David 4=EricZ
输入:Jane
输出:1=Jane 2=Alan 3=Peter 4=David
第一行为字符串个数m,接下来m行每行一个字符床,m不超过100,每个字符床长度不超过20。
输出m行,每行按照样例格式输出,注意用一个空格隔开。
5 EricZ David Peter Alan Jane
1=EricZ 1=David 2=EricZ 1=Peter 2=David 3=EricZ 1=Alan 2=Peter 3=David 4=EricZ 1=Jane 2=Alan 3=Peter 4=David
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Stack<String> stack = new Stack<>(); for (int i = 0; i < n; i++) { String s = scanner.next(); stack.remove(s); stack.push(s); for (int j = 1; j <= Math.min(stack.size(),4); j++) System.out.print(j+"="+stack.get(stack.size()-j)+" "); System.out.println(); } } }
import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); int m=sc.nextInt(); sc.nextLine(); String [] str=new String [100]; for(int i=0;i<m;i++){ str[i]=sc.nextLine(); } for(int j=0;j<m;j++){ if(j==0){ System.out.println("1="+str[0]); }else if(j==1){ System.out.println("1="+str[1]+" 2="+str[0]); }else if(j==2){ System.out.println("1="+str[2]+" 2="+str[1]+" 3="+str[0]); }else { System.out.println("1="+str[j]+" 2="+str[j-1]+" 3="+str[j-2]+" 4="+str[j-3]); } } } }
import java.util.*; public class Main{ private static int flag = 0; public static void main(String[] args){ int num = 0; Scanner in = new Scanner(System.in); num = in.nextInt(); String[] input = new String[4]; for(int i = 0; i < 4; i++){ input[i] = ""; } while(in.hasNext()){ insert(input, in.next()); } } public static void insert(String[] strs, String str){ strs[flag] = str; int tmp = flag; flag = (flag + 1)% 4; int count = 1; while(strs[tmp] != "" && count < 5){ System.out.print(count + "=" + strs[tmp]+ " "); tmp = (tmp - 1 + 4) % 4; count++; } System.out.println(); } }