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
  • 相关阅读:
    优化后的组合算法
    Android之——AsyncTask和Handler对照
    LeetCode(30) Substring with Concatenation of All Words
    HDU--1054--Strategic Game【最小点覆盖】
    最小生成树模板(poj3625)
    QT中使用高速排序
    走进APICloud的世界 (1)
    layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案
    解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:
    移动端H5的一些基本知识点总结
  • 原文地址:https://www.cnblogs.com/mukekeheart/p/5596508.html
Copyright © 2011-2022 走看看