zoukankan      html  css  js  c++  java
  • 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;
                }
    		}
    	}
    
    }
    
    
  • 相关阅读:
    年薪百万必备能力
    二叉搜索树
    字符串和字符串模式匹配
    2006最后寄语
    “豆瓣”式推荐
    什么是LOMO?
    大国崛起
    马季之死
    时间的价值(The Value Of Time)
    我读雅虎的“花生酱宣言”
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948310.html
Copyright © 2011-2022 走看看