【java常用类】SDUT 实验四(2)

述:

1.这个实验涉及到:Date类,String类(str.toCharArray()字符串转数组;a=a.replace(in1, out1);字符串替换),Integer类(Integer.parseInt(t[0])字符转数字,BigInteger类(=new BigInteger(str1);大数加减)。

2.红色代表这题不会

A 2246 时间日期格式转换

对于日期的常用格式,在中国常采用格式的是“年年年年/月月/日日”或写为英语缩略表示的”yyyy/mm/dd”,此次编程竞赛的启动日期“2010/11/20”就是符合这种格式的一个日期,而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制的表示方法,24小时制用0-24来表示一天中的24小时,而12小时制只采用1-12表示小时,再加上am/pm来表示上午或下午,比如”17:30:00”是采用24小时制来表示时间,而对应的12小时制的表示方法是”05:30:00pm”。注意12:00:00pm表示中午12点,而12:00:00am 表示凌晨12点。

对于给定的采用”yyyy/mm/dd”加24小时制(用短横线”-”连接)来表示日期和时间的字符串,请编程实现将其转换成”mm/dd/yyyy”加12小时制格式的字符串。

import java.text.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws ParseException {
		Scanner s=new Scanner(System.in);
		Date d=null;
		int n=s.nextInt();
		//s.next();
		while(n-->0)
		{
			String a=s.next();
			SimpleDateFormat in=new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
			SimpleDateFormat out=new SimpleDateFormat("MM/dd/yyyy-hh:mm:ssa",new Locale("US"));
			d=in.parse(a);
			a=out.format(d);
			System.out.println(a.toLowerCase());
		}
	}
}

B 2445 小学数学

其中每道算术题的格式为a+b=c、a-b=c、a+b=?、a-b=? 中的一种,最后的问号表示这个小朋友不会计算这道题。在检查作业的过程中,大宝发现他经常算错题目而且会数错个数。所以他想请你帮忙写个程序来统计小朋友做对题目的个数。


import java.lang.reflect.Array;
import java.text.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws ParseException {
		Scanner s = new Scanner(System.in);
		int cnt = 0;
		while (s.hasNext()) {
			String str = s.nextLine();
			char[] a = str.toCharArray();
			int f = 0;
			if (a[str.length() - 1] != '?')
			{
				for (int i = 0; i <= str.length(); i++) {
					if (a[i] == '-') {
						f = 1;
						break;
					}
					if (a[i] == '+') {
						f = 2;
						break;
					}
				}
				String t[] = str.split("\\+|-|=");
				int x = Integer.parseInt(t[0]);
				int y = Integer.parseInt(t[1]);
				int z = Integer.parseInt(t[2]);
				if (f == 1 && x - y == z)
					cnt++;
				else if (f == 2 && x + y == z)
					cnt++;
			}
		}
		System.out.println(cnt);
	}
}

C  2787 加密术

给定加密前和加密后的字符串,判断从加密后的字符串中删除若干个字符后剩下的字符串是否可以拼接成加密前的字符串。

import java.lang.reflect.Array;
import java.text.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws ParseException {
		Scanner s = new Scanner(System.in);
		while(s.hasNext())
		{
			String str1=s.next();
			String str2=s.next();
			char a[]=str1.toCharArray();
			char b[]=str2.toCharArray();
			int i=0,j=0;
			while(i<str1.length()&&j<str2.length())
			{
				if(a[i]==b[j])
				{
					i++;
					j++;
				}
				else j++;
			}
			if(i==str1.length())
				System.out.println("Yes");
			else System.out.println("No");
		}
	}
}

D 2192 救基友记
 给你一个字符串s,长度小于1000,让你找出该字符串所包含的所有子串"cRazY" 或者"CraZy",并将找出的子串的大写字母变成小写字母,小写字母变成大写字母,然后输出该字符串。

import java.lang.reflect.Array;
import java.text.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws ParseException {
		Scanner s = new Scanner(System.in);
		int t=s.nextInt();
		while(t-->0)
		{
			String a=s.next();
			String in1="cRazY";
			String in2="CraZy";
			String out1="CrAZy";
			String out2="cRAzY";
			a=a.replace(in1, out1);
			a=a.replace(in2, out2);
			System.out.println(a);
		}
	}
}

E 2271 Eddy的难题

给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。

import java.lang.reflect.Array;
import java.text.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws ParseException {
		Scanner s = new Scanner(System.in);
		while(s.hasNext())
		{
			String a=s.next();
			String b=s.next();
			String x=a+a;
			if(a.length()<b.length())
				System.out.println("no");
			else if(x.contains(b))
				System.out.println("yes");
			else
				System.out.println("no");
		}
	}
}

 F 2784 Good Luck!

寻找最大子串。要求:是原字符串的前缀后缀和中间的一部分。

import java.util.*;

public class Main {
	static int[] next = new int[1000001];

	static void getNext(String s) {
		int j = -1;
		next[0] = -1;
		for (int i = 1; i < s.length(); i++) {
			while (j != -1 && s.charAt(i) != s.charAt(j + 1))
				j = next[j];
			if (s.charAt(i) == s.charAt(j + 1))
				j++;
			next[i] = j;
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		while (n-- > 0) {
			String str = in.next();
			getNext(str);
			int len = str.length();
			if (next[len - 1] == -1 || len < 3)
				System.out.println("Bad Luck!");
			else {
				int l = len - 1;
				boolean res = false;
				while (next[l] != -1) {
					if (str.indexOf(str.substring(0, next[l] + 1), 1) != len - 1 - next[l]) {
						System.out.println(str.substring(0, next[l] + 1));
						res = true;
						break;
					}
					l = next[l];
				}
				if (!res)
					System.out.println("Bad Luck!");
			}
		}
	}
}

G 1916 字符串扩展 

扩展:a-c变成abc 。要求是只处理[a-z]、[A-Z]、[0-9]范围内的字符扩展,即只有当扩展符前后的字符同时是小写字母、大写字母或数字时并且扩展符前面的字符不大于后面的字符才进行扩展,其它情况不进行扩展,原样输出。

Sample Input
3
ADEa-g-m02
acm-0-5-a-ac-cm-m-A-AC-CM-M
Welcometothe3rdACM/ICPCCampusProgrammingContestofSDUT-1-3-A-z-a-Z
Sample Output
ADEabcdefghijklm02
acm-012345-aaccmm-AACCMM
Welcometothe3rdACM/ICPCCampusProgrammingContestofSDUT-123-A-z-a-Z

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		s.nextLine();
		while (n-- > 0) {
			String str=s.nextLine();
			char[] a=str.toCharArray();
			for(int i=0;i<str.length();i++)
			{
				if(a[i]=='-'&&i>0&&i<str.length()-1)
				{
					if((a[i-1]>='a'&&a[i+1]<='z')||(a[i-1]>='A'&&a[i+1]<='Z')||(a[i-1]>='0'&&a[i+1]<='9'))
					{
						if(a[i-1]<=a[i+1])
						{
							char c=(char)(a[i-1]+1);
							while(c<a[i+1])
									{
										System.out.print(c);
										c=(char)(c+1);
									}
						}
						else System.out.print(a[i]);
					}
					else System.out.print(a[i]);
				}
				else System.out.print(a[i]);
			}
			System.out.println();
		}
	}
}

 H 2279骄傲的代价

大数a+b

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		s.nextLine();
		for(int i=1;i<=n;i++) {
			String str1=s.next();
			String str2=s.next();
			BigInteger a=new BigInteger(str1);
			BigInteger b=new BigInteger(str2);
			BigInteger sum=a.add(b);
			System.out.println("Case "+i+":\n"+a+"+"+b+"="+sum);
		}
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实验5 常用(2学时) 一、实验目的 1. 熟悉Java中的String、StringBuffer、Math、包装器的使用方法。 2. 使用常用解决一般性的应用问题。 3. 掌握JavaSE API文档的使用方法。 二、实验内容 1. 编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数。(字符串可以在main方法中指定) 2. 调用java.lang.Math的成员函数“public static double random()”运算下面表达式1000次,(int) (Math.random()*20+0.5),统计其中生成的整数0、1、2、……、20的个数别是多少,并输出统计结果。 3. 编写一个方法,返回一个double型的二维数组,数组中的元素通过解析字符串参数获得。例如,字符串参数:“1,2;3,4,5;6,7,8”,对应的数组为: d[0,0] = 1.0 d[0,1] = 2.0 d[1,0] = 3.0 d[1,1] = 4.0 d[1,2] = 5.0 d[2,0] = 6.0 d[2,1] = 7.0 d[2,2] = 8.0 三、实验要求 完成程序设计并提交实验报告。 实验6 容器(2学时) 一、实验目的 1. 熟悉容器库中常用的使用方法。 2. 使用常用容器解决一般性的应用问题。 二、实验内容 1. 用HashMap模拟一个网上购物车。要求:从键盘输入5本书的名称、单价、购买数量,将这些信息存入一个HashMap,然后将该HashMap作为参数调用方法getSum(HashMap books),该方法用于计算书的总价并返回。【说明:键盘输入可以使用Scanner】 2. 使用两个Stack(JDK容器库中的Stack)实现一个队列MyQueue,提供队列的入队列和出队列操作:enQueue和deQueue。 3. 写一个彩票程序:30选7。随机(1~30之间)生成7个随机数,注意不能重复。然后从键盘输入7个数,对比7个数是否与随机数有相同的。最后显示“中了几个号”。同时,如果中了7个号,显示一等奖;如果中了6个号,显示二等奖;如果中了5个号,显示三等奖。要求:首先写出程序的实现思想,特别是程序所使用的数据结构,然后写出Java实现代码。【说明:键盘输入可以使用Scanner】 三、实验要求 完成程序设计并提交实验报告。 实验7 流(2学时) 一、实验目的 1. 熟悉流库中各种常用流的使用方法。 2. 能够使用流实现基本的文件读写。 二、实验内容 1. 编写程序,在控制台窗口提示输入两个整数,然后接收这两个整数,并输出它们的和。(要求:键盘输入通过流封装System.in获取,不要使用Scanner) 2. 设计学生Student,属性:编号(整型);姓名(字符串),成绩(整型)。编写一个程序:要求:(1)输入5个学生的姓名和成绩,将其姓名和成绩保存到data.txt中;(2)然后从该文件中读取数据,求得这五个学生的平均成绩。 三、实验要求 完成程序设计并提交实验报告。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值