杭电OJ2025、2026、2027、2029(java版)

本文通过三个具体的Java编程实例,展示了如何处理字符串操作、字符检查及回文判断等问题,为初学者提供了实用的代码示例。

OJ2025

import java.util.Scanner;  
public class Main2025{  
  public static void main(String[] args) {  
      Scanner in = new Scanner(System.in);  
      while(in.hasNext()) {
        String s = in.next();
        int max =1;
        char[]c = s.toCharArray();
        if (c.length<100) 
        {
         for(int i = 0;i<c.length;i++) 
         {
           if (c[i]>max) 
           {
               max = c[i];
  } 
         }  
         for(int i = 0;i<c.length;i++) 
         {
           System.out.print(c[i]);
           if ((int)c[i]==max) 
           {
 System.out.print("(max)");
  }         
           if (i==c.length-1) 
           {
System.out.println();
  }        
         }    
}
}
}
}

OJ2026 

import java.util.Scanner;  
public class Main2026{  
  public static void main(String[] args) {  
      Scanner in = new Scanner(System.in);  
      while(in.hasNext()) {
        String s = in.nextLine();
        char [] ch = s.toCharArray();
        ch[0] = (char) (ch[0]-32);
        for(int i = 0;i<ch.length;i++)
        {
        if (ch[i]==' ')
        {
ch[i+1]=Character.toUpperCase(ch[i+1]);
}
        }
        for(int i = 0;i<ch.length;i++) 
        {
            System.out.print(ch[i]);
        }
             System.out.println();  
      }
      }
  }

新知识点:ch[i+1]=Character.toUpperCase(ch[i+1]);           //将ch[i+1]中的字母改成大写之后传给ch[i+1]。

OJ2027 

import java.util.Scanner;  
public class Main2027{  
  public static void main(String[] args) {  
      Scanner in = new Scanner(System.in);  
      label:while(in.hasNext()) {
        int n = in.nextInt();
        in.nextLine();
        int a = 0,e=0,i=0,o=0,u=0;
        String [] str = new String [n+1];
        for(int k = 1; k<=n;k++)
        {
           str[k] = in.nextLine();
        }
        for(int j= 1;j<=n;j++) 
        {
           char []c = str[j].toCharArray();
           for(int d = 0;d<c.length;d++) 
           {
         if (c[d]=='a') 
         {
a++;
 }
         if (c[d]=='e') 
         {
e++;
 }
         if (c[d]=='i') 
         {
i++;
 }
         if (c[d]=='o') 
         {
o++;
 }
         if (c[d]=='u') 
         {
u++;
 }
           }
           System.out.println("a:"+a);
           System.out.println("e:"+e);
           System.out.println("i:"+i);
           System.out.println("o:"+o);
           System.out.println("u:"+u);
           a = 0;e=0;i=0;o=0;u=0;
           if (j==n) break label;
           System.out.println();
        }
    }
  }
}

OJ2029

import java.util.Scanner;  
public class Main2029{  
  public static void main(String[] args) {  
      Scanner in = new Scanner(System.in);  
      while(in.hasNext()) {
        int n = in.nextInt();
        String str[] = new String[n+1];
        boolean x=true;
        for(int i = 1;i<=n;i++)
        {
            str[i]=in.next();
        }
        for (int i = 1; i <=n; i++) 
        {
char c[] = str[i].toCharArray();
for(int j = 0;j<=(c.length/2-1);j++) 
{
    if (c[j]!=c[c.length-j-1])
    {
   x = false;
   break;
}
}
   if (x==true) {
System.out.println("yes");
   }
            if (x==false) {
System.out.println("no");
   x = true;
            }
        }
    }
  }
}

注意:记得要初始化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值