1、空格替换问题

要求将空格替换为指定的字符串,在原数组的基础上面替换。

import java.util.Scanner;

public class Main {
    public static char[] replaceString(char[] str,int oldlen,char[] replaces){
        int j = str.length-1;
        for (int i = oldlen-1; i >= 0; i--) {
            if (str[i]!=' ') {
                str[j] = str[i];
                j--;
            }else {
                for (int k = replaces.length-1; k >= 0 ; k--) {
                    str[j] = replaces[k];
                    j--;
                }
            }   
        }
        return str;
    }


    public static void replace(String str, String replace){
        if (str==null) {
            return;
        }
        if(str.length()==0||str.length()>50){
            return;
        }

        if (replace==null) {
            return;
        }
        if(replace.length()==0||replace.length()>10){
            return ;
        }

        char[] replaces = replace.toCharArray();
        String[] strs = str.split(" +");
        //System.out.println(strs.length);

        //原始长度-空格数+替换长度*空格数
        char result[] = new char[str.length()-(strs.length-1)+(strs.length-1)*replaces.length];

        System.arraycopy(str.toCharArray(), 0, result, 0, str.length());

        replaceString(result,str.length(),replaces);

        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i]);
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);

        while(cin.hasNext())
        {
            String str, replace;
            str = cin.nextLine();
            replace = cin.nextLine();
            //System.out.println(str+" "+replace);
            replace(str,replace);

        }
        //System.out.println(replaceString("You are the best".toCharArray(),16,"123".toCharArray()));
        //System.out.println(replaceString("a b c d","abcd"));
    }
}

2、跳楼梯问题

每次只能跳一格或者两格,从第一个格跳起走,即当只有一个梯子时,为0。

import java.util.Scanner;

public class Main {
    public static int kip(int m){
        int sum=0;
        if (m==1) {
            return sum;
        }
        if (m==2) {
            return 1;
        }
        if (m==3) {
            return 2;
        }
        int first = 1;
        int secord = 2;
        for (int i = 4; i <= m; i++) {
            sum = first+secord;
            first = secord;
            secord = sum;
        }

        return sum;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int num = cin.nextInt();
        for (int i = 0; i < num; i++) {
            int m  = cin.nextInt();
            System.out.println(kip(m));
            if (num-1==i) {
                break;
            }
        }
    }
}