您的位置:首页 > 其它

hdoj简单题目(三)

2010-09-04 16:47 351 查看
hdoj2212 不得不承认这是一个比较菜的题目,本以为有什么好的方法来缩短时间的,可是结果是缩短搜索的次数。

import java.util.*;
public class Main2212 {
	public static void main(String[] args) {
		String s;
		int sum;
		for(int i=1; i<=362880; i++) {
			s = i+"";
			sum = 0;
			for(int j=0; j<s.length(); j++) {
				sum += factorial(s.charAt(j)-48);
			}
			if(sum == i) {
				System.out.println(sum);
			}
		}
	}
	static int factorial(int n) {
		if(n==0 || n==1) {
			return 1;
		} else {
			return n*factorial(n-1);
		}
	}
}




hdoj1402 用Java的BigInteger超时了。

import java.util.*;
import java.math.*;
public class Main1402 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		BigInteger a,b;
		String s1,s2;
		while(sc.hasNext()) {
			s1 = sc.next();
			s2 = sc.next();
			a = new BigInteger(s1);
			b = new BigInteger(s2);
			System.out.println(a.multiply(b));
		}
	}
}




hdoj3421 超时了,如何解决

import java.util.*;
public class Main3421 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t, n;
		int num1,num2;
		int sum;
		int count;
		int m;
		while(sc.hasNextInt()) {
			t = sc.nextInt();
			m = 0;
			while(m != t) {
				n = sc.nextInt();
				count = 0;
				m ++;
				num1 = sc.nextInt();
				num2 = num1;
				sum = num1<0 ? 0:num1;
				for(int i=1; i<n; i++) {
					num2 = sc.nextInt();
					if(num2 > 0) {
						sum += num2;
					}
					if(num2*num1 < 0) {
						count ++;
					}
					num1 = num2;
				}
				System.out.println("case"+" "+m+":");
				System.out.println(count+" "+sum);
				if(m != t) {
					System.out.println();
				}
			}
		}
	}
}




hdoj2816 I LOVE YOU TOO ,按照题意描述做就好了

import java.util.*;
public class Main2816 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] phone={"", "", "ABC", "DEF","GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"};
		String norm = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String comp = "QWERTYUIOPASDFGHJKLZXCVBNM";
		String secret = new String();
		String res1,res2,res3;
		while(sc.hasNext()) {
			secret = sc.next();
			res1 = new String();
			res2 = new String();
			res3 = new String();
			for(int i=0; i<secret.length()-1; i+=2) {
				int n = Integer.parseInt(secret.substring(i, i+2));
				res1 += phone[n/10].charAt(n%10-1)+"";
			}
			for(int i=0; i<res1.length(); i++) {
				res2 += norm.charAt(comp.indexOf(res1.charAt(i)))+"";
			}
			if(res2.length()%2 != 0) {
				res2 += " ";
			}
			for(int i=0,j=(res2.length())/2; i<(res2.length())/2; i++,j++) {
					res3 += res2.charAt(i)+""+res2.charAt(j)+"";
			}
			res3 = res3.trim();
			for(int i=res3.length()-1; i>=0; i--) {
				System.out.print(res3.charAt(i));
			}
			System.out.println();
		}
	}
}




hdoj1736没有通过,不知何故

import java.util.*;
public class Main1736 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str1;
		String str2 = ",.!?";
		String str3 = ",。!?";
		String res;
		boolean flag = true;
		while(sc.hasNext()) {
			str1 = sc.nextLine();
			res = "";
			for(int i=0; i<str1.length(); i++) {
				if(str2.indexOf(str1.charAt(i)) != -1) {
					res += str3.charAt(str2.indexOf(str1.charAt(i)));
				} else if(str1.charAt(i) == '"') {
					if(flag) {
						res += "”";
						flag = false;
					} else {
						res += "“";
						flag = true;
					}
				} else if(str1.charAt(i) == '<') {
					res += "《";
					i ++;
				} else if(str1.charAt(i) == '>') {
					res += "》";
					i ++;
				} else {
					res += str1.charAt(i);
				}
			}
			System.out.println(res);
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: