1. 程式人生 > >Java常用類、集合框架類1

Java常用類、集合框架類1

 B  小學數學 (SDUT 2445)    spilt和Integer.parseInt()用法。

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)

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用法

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用法

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");
    	  }
      }
    }
}