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] + " ");             }         }     } }
点赞 1

相关推荐

10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
牛客网
牛客企业服务