zoukankan      html  css  js  c++  java
  • 蓝桥杯 算法提高 11-1实现strcmp函数 (JAVA方法)

    蓝桥杯 算法提高 11-1实现strcmp函数 (JAVA方法)

    首先这不是一个多难的题,但是网上的我没怎么找到有Java的代码,基本全都是c语言的,小编是个小白,如果有不对的地方请联系小编

    问题描述
      自己实现一个比较字符串大小的函数,也即实现strcmp函数。函数:int myStrcmp(char *s1,char *s2) 按照ASCII顺序比较字符串s1与s2。若s1与s2相等返回0,s1>s2返回1,s1<s2返回-1。具体来说,两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’’为止(注意’’值为0,小于任意ASCII字符)。如:
      “A”<“B”
      “a”>“A”
      “computer”>“compare”
      “hello”<“helloworld”
    样例输出
    在这里插入图片描述

    数据规模和约定
      字符串长度<100。

    思路:

    import java.util.Scanner;
    
    public class strcmp {
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		String s1=sc.next();
    		String s2=sc.next();
    		sc.close();
    		char a[]=s1.toCharArray();
    		char b[]=s2.toCharArray();
    		int a1=0,b1=0;
    		for (int i = 0; i < a.length; i++) {
    			a1+=a[i];
    		}
    		for (int i = 0; i < b.length; i++) {
    			b1+=b[i];
    		}
    		if(a1>b1) {
    			System.out.println(1);
    		}
    		else if(a1<b1) {
    			System.out.println(-1);
    		}
    		else {
    			System.out.println(0);
    		}
    		}
    
    }
    
  • 相关阅读:
    Spring和SpringMVC的关系
    The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory的解决方法
    java面试题
    单例模式
    java中的继承关系
    java重载
    JSP页面读取数据中的数据内容,出现乱码现象的解决方法
    java中时间与时间戳的相互转换
    java中重写
    eclipse中经常用到的修改菜单项
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13079502.html
Copyright © 2011-2022 走看看