题目来源: http://ac.jobdu.com/oldexamset.php
public class a_b_2005 {
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
String s=null;
int a=0;
int b=0;
Map<String,Integer>map=new HashMap<String,Integer>();
map.put("zero",0);
map.put("one",1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
map.put("six", 6);
map.put("seven", 7);
map.put("eight", 8);
map.put("nine",9);
while(true){
s=cin.next();
if (s.equals("+")) {
b=a;//记住前一个数
a=0;
continue;
}
if (s.equals("=")){
if ((a==0)&&(b==0)) break;
System.out.println(a+b);
a=0;
b=0;
continue;
}
a=a*10+map.get(s);
}
}
}
-
题目描述:
-
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和为20。现在增加一个要求,即还需要输出该子序列的第一个和最后一个元素。
-
输入:
-
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( K< 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
-
输出:
-
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
-
样例输入:
-
6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
-
样例输出:
-
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0
public class max_length_subsquence {
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
while(true){
int sum;
int lefttemp;
int left;
int right;
int max;
int n=cin.nextInt();
if (n==0) break;
int []s=new int[n];
for (int i=0;i<n;i++)
{
int m=cin.nextInt();
s[i]=m;
}
sum=0;
max=lefttemp=left=right=s[0];
for (int i=0;i<n;i++)
{
if (sum<0){
sum=s[i];
lefttemp=s[i];
}
else
sum=sum+s[i];
if (sum>max){
max=sum;
left=lefttemp;
right=s[i];
}
}
if (max<0){
System.out.println(0+" "+s[0]+" "+s[n-1]);
}else
System.out.println(max+" "+left+" "+right);
}
}
}
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
-
输入:
-
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
-
输出:
-
对每个测试用例,在1行里输出最少还需要建设的道路数目。
-
样例输入:
-
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
-
样例输出:
-
1 0 2 998
public class changtong_2005 {
static int []pre;
public static int find(int i){
int j=i,temp;
while(pre[i]!=i){
i=pre[i];
}
while(j!=i){
temp=pre[j];
pre[j]=i;
j=temp;
}
return i;
}
public static void merge(int c,int d){
int t1=find(c);
int t2=find(d);
if (t1!=t2)
pre[t1]=t2;
return;
}
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
while(true){
int n=cin.nextInt();//城市
if (n==0) break;
int m=cin.nextInt();//道路
pre=new int[n+1];
for (int i=1;i<=n;i++)
pre[i]=i;
for (int i=1;i<=m;i++){
int c=cin.nextInt();
int d=cin.nextInt();
merge(c,d);//并查集处理
}
Set<Integer>set=new HashSet<Integer>();
for (int i=1;i<=n;i++)
{
set.add(find(i));
}
System.out.println(set.size()-1);//现在有多少类,相关类-1就是最少需要添加的桥梁
}
cin.close();
}
}
-
题目描述:
-
每天第一个到机房的人要把门打开,最后一个离开的人要把门关好。现有一堆杂乱的机房签到、签离记录,请根据记录找出当天开门和关门的人。
-
输入:
-
测试输入的第一行给出记录的总天数N ( N> 0 ),下面列出了N天的记录。
每天的记录在第一行给出记录的条目数M (M > 0 ),下面是M行,每行的格式为
证件号码 签到时间 签离时间
其中时间按“小时:分钟:秒钟”(各占2位)给出,证件号码是长度不超过15的字符串。
-
输出:
-
对每一天的记录输出1行,即当天开门和关门人的证件号码,中间用1空格分隔。
注意:在裁判的标准测试输入中,所有记录保证完整,每个人的签到时间在签离时间之前,且没有多人同时签到或者签离的情况。
-
样例输入:
-
3 1 ME3021112225321 00:00:00 23:59:59 2 EE301218 08:05:35 20:56:35 MA301134 12:35:45 21:40:42 3 CS301111 15:30:28 17:00:10 SC3021234 08:00:00 11:25:25 CS301133 21:45:00 21:58:40
-
样例输出:
-
ME3021112225321 ME3021112225321 EE301218 MA301134 SC3021234 CS301133
class door{
String passsword;//证件号
int hour;//小时
int minute;//分钟
int second;//秒钟
}
class mycmp implements Comparator<door>{//优先顺序小时,分钟,秒钟,从小到大
public int compare(door A,door B){
if (A.hour-B.hour!=0)
return A.hour-B.hour;
else
if (A.minute-B.minute!=0)
return A.minute-B.minute;
else
return A.second-B.second;
}
}
public class opendoor {
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
while(n--!=0){
int m=cin.nextInt();
door []d=new door[m];
door []d2=new door[m];//离开时间
int i=0;
while(i<m){
d[i]=new door();
d[i].passsword=cin.next();
String temp=cin.next();
String s[]=temp.split(":");
String h=s[0];
d[i].hour=Integer.parseInt(h);
String mi=s[1];
d[i].minute=Integer.parseInt(mi);
String se=s[2];
d[i].second=Integer.parseInt(se);
//处理离开时间
d2[i]=new door();
d2[i].passsword=d[i].passsword;
String temp2=cin.next();
String s2[]=temp2.split(":");
String h2=s2[0];
d2[i].hour=Integer.parseInt(h2);
String mi2=s2[1];
d2[i].minute=Integer.parseInt(mi2);
String se2=s2[2];
d2[i].second=Integer.parseInt(se2);
i++;
}
Arrays.sort(d,0,m,new mycmp());
Arrays.sort(d2,0,m,new mycmp());
System.out.println(d[0].passsword+" "+d2[m-1].passsword);//前面去最小的,后面取最大的
}
cin.close();
}
}
-
题目描述:
-
今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的考生,并将他们的成绩按降序打印。
-
输入:
-
测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N < 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G;第2行排序给出第1题至第M题的正整数分值;以下N行,每行给出一名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号(题目号由1到M)。
当读入的考生人数为0时,输入结束,该场考试不予处理。
-
输出:
-
对每场考试,首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考号的升序输出。
-
样例输入:
-
4 5 25 10 10 12 13 15 CS004 3 5 1 3 CS003 5 2 4 1 3 5 CS002 2 1 2 CS001 3 2 3 5 1 2 40 10 30 CS001 1 2 2 3 20 10 10 10 CS000000000000000001 0 CS000000000000000002 2 1 2 0
-
样例输出:
-
3 CS003 60 CS001 37 CS004 37 0 1 CS000000000000000002 20
class student{
public student(String name, int sum) {
this.name=name;
grade=sum;
}
String name;
int grade;
}
class mycmp2 implements Comparator<student>{
@Override
public int compare(student a, student b){
if (a.grade!=b.grade)//从大到小排序
return b.grade-a.grade;
else
return a.name.compareTo(b.name);
}
}
public class rank {
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
while(true){
int n=cin.nextInt();//考生人数
if (n==0)
break;
int m=cin.nextInt();//考题数
int g=cin.nextInt();//分数线
List<student>list=new ArrayList<student>();
int i=1;
Map<Integer,Integer>map=new HashMap<Integer,Integer>();
while(i<=m){
int c=cin.nextInt();
map.put(i, c);
i++;
}
while(n--!=0){
String name=cin.next();//读入学号
int k=cin.nextInt();
int sum=0;
while(k--!=0){
sum=sum+map.get(cin.nextInt());
}
if (sum>=g)//如果大于分数线,加入进入list
list.add(new student(name,sum));
}
System.out.println(list.size());
Collections.sort(list,new mycmp2());
for (int j=0;j<list.size();j++)
System.out.println(list.get(j).name+" "+list.get(j).grade);
}
cin.close();
}
}