滴滴笔试求解

import java.util.*;
public class Main9 {

    public static void sort(ArrayList<Integer> l,int low,int high){
        int n=high-low+1; //几个数字
        int temp;
        for(int i=0;i<n-1;i++){
            for(int j=low;j<=high-i-1;j++){
                if(l.get(j).intValue()>l.get(j+1).intValue()){ //?
                    temp=l.get(j);
                    l.set(j,l.get(j+1));
                    l.set(j+1,temp);
                }
            }
        }
        
    }
    

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		
		
		while(in.hasNext()) {
			int n=in.nextInt();
			if(n==1) {
				System.out.print(in.next());
				return;
			}
            
            ArrayList<Integer> shuzi=new ArrayList<>();
            ArrayList<String> fuhao=new ArrayList<>();
            ArrayList<Boolean> canSwap=new ArrayList<>(); //符号两边数是否可以交换

            int index=1;
            for(int i=0;i<n;i++){
                shuzi.add(Integer.parseInt(in.next()));
                if(i<n-1){
                    fuhao.add(in.next());
                }
            }
//            System.out.println(shuzi);
//            System.out.println(fuhao);
            for(int i=0;i<n-1;i++) {
            	canSwap.add(false);
            }
            
            //初始化canSwap
            for(int i=0;i<n-1;i++) {
            	String now=fuhao.get(i);
            	if(now.equals("+")) {
            		
            		if(i==0) {
            			if(i+1<=n-2) { //下一个符号存在
            				String next=fuhao.get(i+1);
            				if(now.equals("+")||now.equals("-")) {
            					canSwap.set(i, true);
            				}
            			}else {
            				canSwap.set(i, true);
            			}
            		}else {
            			String before=fuhao.get(i-1);
            			if(before.equals("+")) {  //前一个符号为加号
            				if(i+1<=n-2) { //下一个符号存在
                				String next=fuhao.get(i+1);
                				if(next.equals("+")||next.equals("-")) {
                					canSwap.set(i, true);
                				}
            				}else {
            					canSwap.set(i, true);
            				}
            			}
            		}
            		
            	}else if(now.equals("*")) {
            		if(i==0) {
            			canSwap.set(i, true); 
            		}else {
            			String before=fuhao.get(i-1);
            			if(!before.equals("/")) {
            				canSwap.set(i, true);
            			}
            		}
            	}
            }
//            System.out.println(canSwap);
            
            int flag=0;
            int low=0;
            int high=0;
            for(int i=0;i<n-1;i++) {
            	boolean can=canSwap.get(i).booleanValue();
            	if(can) {
            		if(flag==0) {
            			low=i;
            			flag=1;
            		}
            		if(i==n-2) {
            			high=n-1;
            			sort(shuzi,low,high);
//            			System.out.println(shuzi);
            		}
            		
            	}else {
            		if(flag==1) {
            			flag=0;
            			high=i;
//            			System.out.println(low+" "+high);
            			sort(shuzi,low,high);
//            			System.out.println(shuzi);
            		}
            	}
            }
            
            for(int i=0;i<n-1;i++){
                System.out.print(shuzi.get(i)+" "+fuhao.get(i)+" ");
            }
            System.out.print(shuzi.get(n-1));
            
		}
	}

}

 

#滴滴##笔试题目#
全部评论
package didi; import java.util.Arrays; import java.util.Scanner; public class Fuhao {     public static void main(String[] args) {         // TODO Auto-generated method stub         Scanner sca = new Scanner(System.in);         int n = sca.nextInt();         // 符号数组和数字数组         String[] fuhao = new String[n - 1];         int[] num = new int[n];         // 获取元素         for (int i = 0; i < 2 * n - 1; i++) {             if (i % 2 == 0) {                 num[i / 2] = sca.nextInt();             } else {                 fuhao[(i - 1) / 2] = sca.next();             }         }         /*          * temp 暂存数组中发现的第一个“+” or “*”符号 二维数组存放biaohao[i][o]存放第i次发现“+” or “*”的起始位置          * biaohao[i][1]存放第i次发现“+” or “*”的截止位置 变量a++等效于 i++; flag 标志位          */         String temp = "";         int[][] biaohao = new int[n - 1][2];         int a = 0;         boolean flag = true;         // 获取第一个“+” or “*”         for (int i = 0; i < fuhao.length; i++) {             // 判断是不是满足可以交换字符前后的数字的条件             if (fuhao[i].equals("+") || fuhao[i].equals("*")) {                 // 当前字符与暂存字符不相符,则记录截至位置,标志位true下次循环便可记录初始位置                 if (fuhao[i].equals(temp)) {                     biaohao[a][1] = i - 1;                     flag = true;                     a++;                 }                 // 满足,存放当前字符,记录起始位置,标志位为false不再重复记录                 if (flag) {                     temp = fuhao[i];                     biaohao[a][0] = i;                     flag = false;                 }             } else {// 不满***换条件                 biaohao[a][1] = i - 1;                 flag = true;                 a++;// 数组行自增             }         }         // 排序         for (int i = 0; i < biaohao.length / 2; i++) {             // 截止位置-起始位置+2 例 :1+2+3             int[] sor = new int[biaohao[i][1] - biaohao[i][0] + 2];             sor = Arrays.copyOfRange(num, biaohao[i][0], biaohao[i][1] + 2);             Arrays.sort(sor);             System.arraycopy(sor, 0, num, biaohao[i][0], sor.length);         }         // 打印数组         for (int i = 0; i < 2 * n - 1; i++) {             if (i % 2 == 0) {                 System.out.print(num[i / 2] + " ");             } else {                 System.out.print(fuhao[(i - 1) / 2] + " ");             }         }     } }
点赞 回复 分享
发布于 2019-08-28 12:42

相关推荐

赏个offer求你了:友塔HR还专门加我告诉我初筛不通过😂
点赞 评论 收藏
分享
11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务