Java实现 蓝桥杯VIP 算法提高 字符串比较

算法提高 字符串比较
时间限制:1.0s 内存限制:512.0MB
独立实现标准字符串库的strcmp函数,即字符串比较函数,从键盘输入两个字符串,按字典序比较大小,前者大于后者输出1,前者小于后者输出-1,两者相等输出0。
样例输入:
apple one
样例输出:
-1
样例输入:
hello he
样例输出:
1

样例输入:
hello hello
样例输出:
0

import java.util.Scanner;


public class 字符串比较 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s1=sc.next();
		String s2=sc.next();
		char[] a1=s1.toCharArray();
		char[] a2=s2.toCharArray();
		if(s1.equals(s2)){
           System.out.println("0");
		}
		else
		for(int i=0,j=0;i<a1.length||j<a2.length;i++,j++)
		{
			if(a1[i]>=a2[j])
			{
				System.out.println(1);
                break;
            }
			if(a1[i]<a2[j])
			{
				System.out.println(-1);
                break;
            }
		}
	}

}

posted @ 2019-06-22 11:32  南墙1  阅读(58)  评论(0编辑  收藏  举报