Java基础语法3

排序(SDUT 1582)

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		int a[] = new int[200];
		n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		Arrays.sort(a, 0, n);
		for (int i = 0; i < n; i++) {
			if (i == 0)
				System.out.print(a[i]);
			else
				System.out.print(" " + a[i]);
		}
		System.out.println("");
	}
}

期末考试之排名次(SDUT 2255)


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n, c, m, e;
		int a[] = new int[200];
		n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			c = sc.nextInt();
			m = sc.nextInt();
			e = sc.nextInt();
			a[i] = c + m + e;
		}
		Arrays.sort(a, 0, n);
		for (int i = n - 1; i >= 0; i--) {
			System.out.println(a[i]);
		}
	}
}

冒泡排序中数据交换的次数(SDUT 2554)

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n, t;
		int a[] = new int[200];
		t = sc.nextInt();
		for (int q = 0; q < t; q++) {
			n = sc.nextInt();
			for (int i = 0; i < n; i++) {
				a[i] = sc.nextInt();
			}
			int ans = 0;
			for (int i = 0; i < n - 1; i++) {
				for (int j = 0; j < n - 1 - i; j++) {
					if (a[j] > a[j + 1]) {
						int temp = a[j];
						a[j] = a[j + 1];
						a[j + 1] = temp;
						ans++;
					}
				}
			}
			System.out.println(ans);
		}
	}
}

小鑫の日常系列故事(五)——卡片游戏(SDUT 2736)

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans1 = 0,ans2 = 0;
		for(int i = 1; i <= n; i ++)
		{
			int x = sc.nextInt();
			if(i%2 == 1)ans1 += x;
			else ans2 += x;
		}
		if(ans1==ans2)System.out.println("Equal");
		else if(ans1>ans2)System.out.println("Greater than");
		else System.out.println("Less than");
	}
}

统计元音(SDUT 1250)


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str;
		int n;
		n = sc.nextInt();
		sc.nextLine();
		for (int q = 0; q < n; q++) {
			str = sc.nextLine();
			int len = str.length();
			int a, e, i, o, u;
			a = e = i = o = u = 0;
			for (int j = 0; j < len; j++) {
				char op = str.charAt(j);
				if (op == 'a')
					a++;
				else if (op == 'e')
					e++;
				else if (op == 'i')
					i++;
				else if (op == 'o')
					o++;
				else if (op == 'u')
					u++;
			}
			System.out.printf("a:%d
e:%d
i:%d
o:%d
u:%d

", a, e, i, o, u);
		}
	}
}

回文串判定(SDUT 1524)


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str;
		int flag = 0;
		str = sc.nextLine();
		int len = str.length();
		for(int i = 0; i < len/2; i++) {
			char t1 = str.charAt(i);
			char t2 = str.charAt(len - i - 1);
			if(t1 != t2) {
				flag = 1;
				break;
			}
		}
		if(flag == 0) System.out.println("yes");
		else System.out.println("no");
	
	}
}

U     字符统计2(SDUT 1525)

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[200];
		String str;
		while (sc.hasNext()) {
			str = sc.nextLine();
			Arrays.fill(a, 0);
			int len = str.length();
			for (int i = 0; i < len; i++) {
				char op = str.charAt(i);
				if (op != ' ')
					a[op]++;
			}
			int max = -1, ans = 0;
			for (int i = 0; i < 200; i++) {
				if (a[i] > max) {
					max = a[i];
					ans = i;
				}
			}
			System.out.println((char) ans + " " + max);
		}
	}
}

V     传说中的数据结构(SDUT 2556)

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[2000];
		int top = 0, n,x;
		String str;
		while (sc.hasNext()) {
			top = -1;
			n = sc.nextInt();
			sc.nextLine();
			for(int i = 0; i < n; i ++) {
//				sc.nextLine();     //读入一行
				str = sc.next();   // 只是读入字符串
//				System.out.println(str);
				if(str.equals("push")==true) {
					x = sc.nextInt();
					top ++;
					a[top] = x;
				}
				else if(str.equals("pop") == true) {
					if(top < 0)System.out.println("error");
					else top --;
				}
				else if(str.equals("top") == true) {
					if(top < 0)System.out.println("empty");
					else System.out.println(a[top]);
				}
			}
			System.out.println("");
		}	
	}
}

W    小鑫の日常系列故事(十)——排名次

原文地址:https://www.cnblogs.com/lcchy/p/10139515.html