zoukankan      html  css  js  c++  java
  • 华为OJ平台——字符串分隔

    题目描述:

    连续输入字符串,请按长度为8拆分每个字符创 后输出到新的字符串数组;

    长度不是8整数倍的字符串请在后面补数字0,空字符串不处理

    输入
      连续输入字符串(输入两次,每个字符长长度小于100)
    输出
      输出到长度为8,的新字符串数组
    样例输入
      abc
      123456789
    样例输出
      abc00000
      12345678
      90000000

    思路:

    都是直接处理,没有具体的方法而言

    注意点:

    华为的OJ平台的输入输出有点坑,好多次的程序都在这里出问题,在Eclipse上运行的结果没问题,然后在OJ上就是编译出错或者格式错误什么的

    本题中输入是以换行符来表示一个字符串的输入结束,输出是以每8个一组为一行输出

     1 import java.util.Scanner;
     2 
     3 /**
     4  * 连续输入字符串,请按长度为8拆分每个字符创 后输出到新的字符串数组;
     5  * 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理
     6  * 输入
     7  *         连续输入字符串(输入两次,每个字符长长度小于100)
     8  * 输出
     9  *         输出到长度为8,的新字符串数组
    10  * 样例输入
    11  *         abc
    12  *         123456789
    13  * 样例输出
    14  *         abc00000
    15  *         12345678
    16  *         90000000
    17  *
    18  */
    19 public class StringSplit {
    20 
    21     public static void main(String[] args) {
    22         //读取输入字符串
    23         String [] strs = new String [2] ;
    24         Scanner cin = new Scanner(System.in) ;
    25         strs[0] = cin.nextLine() ;
    26         strs[1] = cin.nextLine() ;
    27         cin.close();
    28         
    29         String temp ;
    30         int sub = 0 ;
    31         
    32         //循环处理两个字符串
    33         for(int i = 0 ; i < 2 ; i++ ){
    34             //判断是否为空串(字符串首尾去除空格后判断长度是否为0),如果为空串则不处理
    35             if(strs[i].trim().isEmpty()){
    36                 continue ;
    37             }
    38             temp = strs[i] ;
    39             //每8个一组进行处理
    40             for(int j = 0 ; j < temp.length() ; j = j + 8){
    41                 //判断 j 之后是否还有8个字符 
    42                 if(j+8 < temp.length()){
    43                     // j 之后的长度大于8
    44                     System.out.println(temp.substring(j,j+8)) ;
    45                 }else if(j+8 == temp.length()){
    46                     // j 之后的长度等于8,即刚好是最后一个组,无需加0
    47                     System.out.println(temp.substring(j)) ;
    48                 }else{
    49                     // j 之后的长度小于8,后面需要加(j+8-temp.length())个 0 
    50                     System.out.print(temp.substring(j)) ;
    51                     sub = j+8 - temp.length() ;
    52                     while(sub != 0){
    53                         System.out.print( "0") ;
    54                         sub-- ;
    55                     }
    56                     System.out.println();
    57                 }                
    58             }    
    59         }        
    60 
    61     }
    62 
    63 }
    View Code
  • 相关阅读:
    django models设置联合主键
    sql语句清空表数据
    mysql交互模式下执行sql文件
    linux 下安装虚拟环境和创建虚拟环境
    解决Eclipse中无法直接使用sun.misc.BASE64Encoder及sun.misc.BASE64Decoder的问题
    mssql语法
    PowerDesigner的使用
    NoSuchMethodError错误
    在windows、widfly环境下,远程debug
    Blocking request failed HttpServerExchange{ GET /ssssssssss/kkk}: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  • 原文地址:https://www.cnblogs.com/mukekeheart/p/5596508.html
Copyright © 2011-2022 走看看