前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java基础语法3

Java基础语法3

作者头像
Lokinli
发布2023-03-09 16:44:24
2790
发布2023-03-09 16:44:24
举报
文章被收录于专栏:以终为始以终为始

排序(SDUT 1582)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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\ne:%d\ni:%d\no:%d\nu:%d\n\n", a, e, i, o, u);
		}
	}
}

回文串判定(SDUT 1524)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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    小鑫の日常系列故事(十)——排名次

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 排序(SDUT 1582)
  • 期末考试之排名次(SDUT 2255)
  • 冒泡排序中数据交换的次数(SDUT 2554)
  • 小鑫の日常系列故事(五)——卡片游戏(SDUT 2736)
  • 统计元音(SDUT 1250)
  • 回文串判定(SDUT 1524)
  • U     字符统计2(SDUT 1525)
  • V     传说中的数据结构(SDUT 2556)
  • W    小鑫の日常系列故事(十)——排名次
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档