提取数字
import java.util.Scanner;
public class AD {
public static void main(String[] args) {
String line;
Scanner read = new Scanner(System.in);
while(read.hasNextLine()){
line = read.next();
read.nextLine();//吃掉回车,这一步一直出错
String str="";
if(line != null && !"".equals(line)){
for(int i=0;i<line.length();i++){
if(line.charAt(i)>='0' && line.charAt(i)<='9'){
str+=line.charAt(i);
}
}
System.out.println(str);
}
}
}
}
时间戳
输入的第一行为已个正整数N,表示需要你计算活动次数,接下来的N行,每行的第一个数为一个10位正整数,
表示当前日期的时间戳秒数,接下来是两个字符串,表示活动开始日期和结束日期。字符串满足日期
“yyyy-MM-dd hh:mm:ss”格式。
样例输出:
如果当前日期在活动开始之前,就输出“Before",如果在活动之中输出"NOW", 如果在活动之后,输出"After".
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class ABC {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int N=scn.nextInt();
String s;
//吃掉空格
scn.nextLine();
for (int i=0;i<N;i++){
s = scn.nextLine();
//按空格分割字符串
String[] sa = s.split(" ");
DateQuestion dq = new DateQuestion(Long.parseLong(sa[0]),s.replace(sa[0]+" ", ""));
System.out.println(dq.getResult());
}
}
}
class DateQuestion{
private long tms;
private Date t_end,t_begin;
public DateQuestion (long t,String s){
tms = t;
String[] sa = s.split("\"");
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//将字符装换为日期
t_begin = df.parse(sa[1]);
t_end = df.parse(sa[3]);
}catch(Exception ex) {
}
}
public String getResult(){
//从日期中提取毫秒数(date.getTime()方法),再与tms的大小进行比较,算法比较简单
long begin,end;
String Result;
//从日期中提取毫秒
begin = this.t_begin.getTime()/1000;
end = this.t_end.getTime()/1000;
if (tms<begin){
Result = "Before";
}else if (tms>begin && tms<end){
Result = "NOW";
}else{
Result = "After";
}
return Result;
}
}
数字运算
计算x+x x-y x*y x/y;
import java.util.Scanner;
public class ABCD {
public static void main(String[] args) {
int n,j, i,len;
String str;
double num1, num2, num = 0;
Scanner in = new Scanner(System.in);
//in.nextLine();
while (in.hasNext()) {
//in.nextLine();
n = in.nextInt(); //输入多重用例
for (i = 0; i < n; i++) {
str=in.next();//输入字符串
len=str.length();
for(j=1;j<len;j++)
{ //查找符号
if(str.charAt(j)=='-'||str.charAt(j)=='+'||str.charAt(j)=='*'||str.charAt(j)=='/'||str.charAt(j)=='%')
break;
} //把符号前后分割
String s1=str.substring(0,j);
String s2=str.substring(j+1,len);
//转换成实型
num1=Double.parseDouble(s1);
num2=Double.parseDouble(s2);
//分类输出
if(str.charAt(j)=='-')
{
System.out.printf("%.2f\n",num1-num2);
}
if(str.charAt(j)=='+')
{
System.out.printf("%.2f\n",num1+num2);
}
if(str.charAt(j)=='*')
{
System.out.printf("%.2f\n",num1*num2);
}
if(str.charAt(j)=='/')
{
System.out.printf("%.2f\n",num1/num2);
}
if(str.charAt(j)=='%')
{
System.out.printf("%.0f\n",num1%num2);
}
}
}
}
}
多关键字排序
标准输入:
第一行为一个正整数N,表示该年纪共有的学生数,接下来的N行,每行为一个学生的信息,依次为学号、班级、语文成绩、数学成绩和英语成绩,其中学号为10个字符的字符串,班级和成绩为正整数,他们之间由一个空格隔开。
标准输出:
输出该年级学生的成绩单,即根据总分和语文、数学、英语成绩为次关键词的排序后的成绩单,每行输出一个学生的成绩,使用一个空格隔开,依次输出如下数据:
学号 班级 语文 数学 英语 总分 平均
其中平均成绩四舍五入保留2位小数。
测试用例输入:
4
0806401001 1 56 64 77
0806401002 1 75 68 54
0806401003 1 68 79 76
0806401004 1 56 57 84
测试用例输出:
0806401003 1 68 79 76 223 74.33
0806401002 1 75 68 54 197 65.67
0806401001 1 56 64 77 197 65.67
0806401004 1 56 57 84 197 65.67
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
public class test2 {
public static void main(String[] args){
ArrayList list=new ArrayList();
//创建长度可变的数组对象
Scanner in=new Scanner(System.in);
int n=in.nextInt();
in.nextLine();//吃掉空格
for( int i=0;i<n;i++){
//输入用例
String line=in.nextLine();
list.add(new Student(line));
//向集合中添加元素
}
Collections.sort(list);//调用自定义排序方法
//输出该容器的值
Iterator it=list.iterator();
//创建迭代器对象
while(it.hasNext()){
Student stu=(Student)it.next();
//取出容器数据强制转化为Student型
System.out.printf("%s %d %.0f %.0f %.0f %.0f %.2f\n",
stu.Id,stu.classname,stu.chinasorce,stu.mathsorce,
stu.englishsorce,stu.total,stu.avg);
}
}
}
class Student implements Comparable{
//让需要进行排序的对象的类实现Comparable接口(T o)方法
String Id;
int classname;
double chinasorce;
double mathsorce;
double englishsorce;
double total;
double avg;
Student(String line){
String[] ss=line.split(" ");
//按照空格将字符串分割
Id=ss[0];
//将字符串还原成不同类型的数据
classname=Integer.parseInt(ss[1]);
chinasorce=Double.parseDouble(ss[2]);
mathsorce=Double.parseDouble(ss[3]);
englishsorce=Double.parseDouble(ss[4]);
total=this.chinasorce+this.mathsorce+this.englishsorce;
avg = total/3;
}
public int compareTo(Object arg0){//,重写compareTo(T o)方法
Student stu=(Student)arg0;
//比较方法 设置返回值 控制sort的由大到小
if(this.total!=stu.total){
if(this.total<stu.total)
return 1;
//返回1表示肯定
}else if(this.chinasorce!=stu.chinasorce){
if(this.chinasorce<stu.chinasorce)
return 1;
}else if(this.mathsorce!=stu.mathsorce){
if(this.mathsorce<stu.mathsorce)
return 1;
}else{
if(this.englishsorce<stu.englishsorce)
return 1;
}
return 0;
}
}