隔四位插入字符、平方和判1、版本号判断更新

把一个包含若干字母的字符串每隔四位插入另一个字符串

题目:
把一个包含若干字母的字符串每隔四位插入另一个字符串,如果一个字符串用完了,则未完字符串就拼接到结果字符串的末尾即可。
输入:
123456
abc
输出:
1234a56bc
输入:
123456789
ad
输出:
1234a5678d9
思路:
创建一个字符串来接收拼接的值,通过遍历来实现拼接,在拼接的过程中需要注意两个字符串的长度。

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

         String x = in.nextLine();
         String y = in.nextLine();

        ArrayList<String> listX = new ArrayList<>();
        ArrayList<String> listY = new ArrayList<>();

        for(int i = 0; i < x.length(); i++) {
            char ch = x.charAt(i);
            if(ch != ' ') {
                listX.add(ch + "");
            }
        }
        for(int j = 0; j < y.length(); j++) {
            char ch = y.charAt(j);
            if(ch != ' ') {
                listY.add(ch + "");
            }
        }
        String res = "";
        int i = 0;
        int j = 0;
        while (listX.size() - i >= 4 && j < listY.size()) {
            res += (listX.get(i ++) + " ");
            res += (listX.get(i ++) + " ");
            res += (listX.get(i ++) + " ");
            res += (listX.get(i ++) + " ");

            res += listY.get(j ++) + " ";
        }
        while (i < listX.size()) {
            res += (listX.get(i ++) + " ");
        }
        while (j < listY.size()) {
            res += listY.get(j ++) + " ";
        }
        System.out.println(res);
    }
}

判断各位数的平方和是否为1

题目描述:
对输入的数的各个位数求平方和,若结果为1,则返回true,若结果不为1,则继续求这个新的数的各个位数的平方和。如果一个数永远不可能得到1,那么也返回false。
思路:
拆分原始数的各个位数,循环计算和判断。采用一个值来计数,超过一定循环次数则判断为false。
代码实现(Java):

import java.util.ArrayList;
import java.util.Scanner;

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

        int count = in.nextInt();
        for(int i = 0; i < count; i++) {
            int num = in.nextInt();
            boolean res = test(num);
            if(res == true) {
                System.out.println("true");
            }else {
                System.out.println("false");
            }
        }
    }

    public static boolean test(int num) {
        if(num == 1) {
            return true;
        }
        int x = num;
        int sum = 0;
        int count = 0;
        boolean res = false;
        while (true) {
            ArrayList<Integer> array = new ArrayList<>();
            while (x > 0) {
                array.add(x % 10);
                x = x / 10;
            }
            sum = 0;
            for(int i = 0; i < array.size(); i++) {
                sum += array.get(i) * array.get(i);
            }
            if(sum == 1) {
                res = true;
                break;
            }
            x = sum;
            count ++;
            if(count == 100) {
                break;
            }
        }
        return res;
    }
}

判断版本号是否需要更新

题目描述:给定两个版本号,版本号是否数字和“.”来表示的,“.”最多有三个。
输入:
1
3.3.1 3.3.2
输出:
true

输入:
2
1 1.0
2.2 2.1
输出:
false
false

思路: 我的思路是把点号分割的数作为不同的位数,分别赋予不同的权值,每个版本号可以计算得到一个数作为结果用来比较,通过比较这个数来判断是否需要更新。要完成这件事,首先就需要读取各个数字。
代码实现(Java)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

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

        int count = Integer.valueOf(in.nextLine());
        for(int i = 0; i < count; i++) {
            String str = in.nextLine();
            boolean res = test(str);
            if(res == true) {
                System.out.println("true");
            }else {
                System.out.println("false");
            }
        }
    }

    public static boolean test(String str) {
        ArrayList<Integer> listX = new ArrayList<>();
        ArrayList<Integer> listY = new ArrayList<>();
        boolean flag = false;
        String ss = "";
        for(int i = 0; i < str.length(); i ++) {
            char ch = str.charAt(i);
            if(ch == ' ') {
                listX.add(Integer.valueOf(ss));
                flag = true;
                ss = "";
                continue;
            }
            if(ch != '.') {
                ss += ch;
            }else {
                if(flag == false) {
                    listX.add(Integer.valueOf(ss));
                }else {
                    listY.add(Integer.valueOf(ss));
                }
                ss = "";
            }
        }
        listY.add(Integer.valueOf(ss));
        int num1 = 0;
        int num2 = 0;
        int p = 100000000;
        for(int i = 0 ; i < listX.size(); i ++) {
            num1 += listX.get(i) * p;
            if(p >= 100) {
                p = p / 100;
            }
        }
        p = 100000000;
        for(int i = 0; i < listY.size(); i ++) {
            num2 += listY.get(i) * p;
            if(p >= 100) {
                p = p / 100;
            }
        }
        if(num1 < num2) {
            return true;
        }else {
            return false;
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值