这道ACM题目怎么用JAVA做出来。。主要是输入格式问题:数据格式为YYYY/MM/DD组成,
答案:2 悬赏:10 手机版
解决时间 2021-04-08 15:19
- 提问者网友:骑士
- 2021-04-07 17:47
这道ACM题目怎么用JAVA做出来。。主要是输入格式问题:数据格式为YYYY/MM/DD组成,
最佳答案
- 五星知识达人网友:冷風如刀
- 2021-04-07 18:25
public int getDays(String date){//输入格式要求是用斜杠“/”来分隔年月日
int y = Integer.parseInt(date.substring(0,4));
int m = Integer.parseInt(date.substring(5,date.lastIndexOf("/")));
int d = Integer.parseInt(date.substring(date.lastIndexOf("/")+1));
if(m==1){return d;}
if(m==2){return 31+d;}
int days = 0;
for(int i=1;i if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12 ){days += 31;}
else if(i != 2){days += 30;}
else{
if((y%4 == 0 && y%100 !=0)|| (y%4==0 && y%400 == 0)){days += 29;}
else{days += 28;}
}
}
days += d;
return days;
}
下面是我自测的结果:
int y = Integer.parseInt(date.substring(0,4));
int m = Integer.parseInt(date.substring(5,date.lastIndexOf("/")));
int d = Integer.parseInt(date.substring(date.lastIndexOf("/")+1));
if(m==1){return d;}
if(m==2){return 31+d;}
int days = 0;
for(int i=1;i
else if(i != 2){days += 30;}
else{
if((y%4 == 0 && y%100 !=0)|| (y%4==0 && y%400 == 0)){days += 29;}
else{days += 28;}
}
}
days += d;
return days;
}
下面是我自测的结果:
全部回答
- 1楼网友:患得患失的劫
- 2021-04-07 18:38
public static int cal(int year, int month, int day) {
int sum = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
break;
case 4:
case 6:
case 9:
case 11:
sum += 30;
break;
case 2:
if (((year % 4 == 0) & (year % 100 == 0)) | (year % 400 == 0))
sum += 29;
else
sum += 28;
}
}
return sum = sum + day;
}
public static void main(String[] args) {
System.out.println("请输入日期,确保日期格式正确:");
ArrayList<String> array = new ArrayList<String>();
Scanner scn = new Scanner(System.in);
String line;
//当你输入end的时候结束控制到输入,然后输出结果
while (!"end".equals(line = scn.nextLine())) {
array.add(line);
}
System.out.println("输出结果为:");
for(String str : array){
String[] arr= str.split("/");
int val = cal( Integer.parseInt(arr[0]), Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
System.out.println(val);
}
}
int sum = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
break;
case 4:
case 6:
case 9:
case 11:
sum += 30;
break;
case 2:
if (((year % 4 == 0) & (year % 100 == 0)) | (year % 400 == 0))
sum += 29;
else
sum += 28;
}
}
return sum = sum + day;
}
public static void main(String[] args) {
System.out.println("请输入日期,确保日期格式正确:");
ArrayList<String> array = new ArrayList<String>();
Scanner scn = new Scanner(System.in);
String line;
//当你输入end的时候结束控制到输入,然后输出结果
while (!"end".equals(line = scn.nextLine())) {
array.add(line);
}
System.out.println("输出结果为:");
for(String str : array){
String[] arr= str.split("/");
int val = cal( Integer.parseInt(arr[0]), Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
System.out.println(val);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯