述:
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);
}
}
}