Java常用类、集合框架类1

2023-03-09 17:11:09 浏览数 (1)

A   时间日期格式转换(SDUT 2246)(SimpleDateFormat用法)

转换的df2里面时间是US,上面的df1可以是CHINA或者US。

代码语言:javascript复制
package test;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		n = sc.nextInt();
		sc.nextLine();
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss",Locale.US);
		SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ssa",Locale.US);
		String s;
		while(n -- > 0) {
			s = sc.nextLine();
			try {
				System.out.println(df2.format(df1.parse(s)).toLowerCase());
			}catch(ParseException e) {
				e.printStackTrace();
			}
		}
		
	}
}

 B  小学数学 (SDUT 2445)    spilt和Integer.parseInt()用法。

代码语言:javascript复制
import java.io.*;
import java.util.*;
import java.nio.file.*;
import java.io.File;
import java.io.IOException;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int ans = 0,i;
		while (sc.hasNext()) {
			String s = sc.nextLine();
			String[] a = s.split("\ |\-|\=");
			if (!a[2].equals("?")) {
				int x1 = Integer.parseInt(a[0]);
				int x2 = Integer.parseInt(a[1]);
				int x3 = Integer.parseInt(a[2]);
				for(i = 0; i < s.length(); i   ) if(s.charAt(i) == ' ' || s.charAt(i) == '-')break;
					if (s.charAt(i) == ' ') {
						if (x1   x2 == x3) 
							ans  ;
					}
					else {
						if(x1 - x2 == x3) ans   ;
				}
			}
		}
		System.out.println(ans);
	}
}

加密术 (SDUT 2787)

代码语言:javascript复制
import java.util.*;
public class Main{
    public static void main(String[] args) {
      Scanner sc = new Scanner (System.in);
      String s,t;
      while(sc.hasNext()) {
    	  s = sc.next();
    	  t = sc.next();
    	  int num = 0;
    	  int i,j = 0;
    	  int f = 0;
    	  for(i = 0; i < s.length(); i   ) {
    		 for(; j < t.length(); j   ) {
    			 if(s.charAt(i) == t.charAt(j)) {
    				 num   ;
    				 break;
    			 }
    			 if(i != s.length() - 1) {
    				 if(j == t.length()) {
    					 f = 1;
    					 break;
    				 }
    			 }
    		 }
    		 if(f == 1) break;
    	  }
    	  if(num == s.length() && f == 0)
        	  System.out.println("Yes");
          else 
        	  System.out.println("No");
      }
     
    }
}

救基友记2(SDUT 2192) replace用法

代码语言:javascript复制
import java.util.*;
public class Main{
    public static void main(String[] args) {
      Scanner sc = new Scanner (System.in);
      String s;
      int t;
      t = sc.nextInt();
      while(t-- > 0) {
    	  s = sc.next(); 
    	  String s1 = "cRazY";
    	  String s2 = "CraZy"; 
    	  String news1 = "CrAZy"; 
    	  String news2 = "cRAzY";
    	  s = s.replace(s1, news1); 
    	  s = s.replace(s2, news2); 
    	  System.out.println(s);
      }
      sc.close();
    }
}

Eddy的难题(SDUT 2271)indexOf用法

代码语言:javascript复制
import java.util.*;
public class Main{
    public static void main(String[] args) {
      Scanner sc = new Scanner (System.in);
      String s,t;
      while(sc.hasNext()) {
    	  s = sc.next();
    	  t = sc.next();
    	  s = s   s;
    	  if(s.length() / 2 < t.length()) {
    		  System.out.println("no");
    	  }
    	  else {
    		  if(s.indexOf(t) != -1) {
    			  System.out.println("yes");
    		  }
    		  else System.out.println("no");
    	  }
      }
    }
}

0 人点赞