2005 第几天?
Problem Description 给定一个日期,输出这个日期是该年的第几天。
Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output 对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
期初使用的套路十分简单,后偶然间发现Java Calendar类,十分好用,附上大佬博客链接,使用Java自带的类方法即可轻松解决该问题。
import java.util.Calendar;
import java.util.Scanner;
public class test2005 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String a=sc.next();
String str=new String(a);
int year=Integer.parseInt(str.substring(0,4));
int month=Integer.parseInt(str.substring(5,7));
int day=Integer.parseInt(str.substring(8));
Calendar cal=Calendar.getInstance();
cal.set(year, month-1, day);
int sum=cal.get(Calendar.DAY_OF_YEAR);
System.out.println(sum);
}
}
}
2006 求奇数的乘积
Problem Description 给你n个整数,求他们中所有奇数的乘积。
Input 输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。
Output 输出每组数中的所有奇数的乘积,对于测试实例,输出一行。
Sample Input
3 1 2 3
4 2 3 4 5
Sample Output
3
15
import java.util.Scanner;
public class test2006 {
static int res;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int a=sc.nextInt();
int res=1;
if(a%2!=0) {
res=res*a;
}
System.out.println(res);
}
}
}
2007 平方和与立方和
Problem Description 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Input 输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
Output 对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
Sample Input
1 3
2 5
Sample Output
4 28
20 152
import java.util.Scanner;
public class test2007 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int n=sc.nextInt();
int m=sc.nextInt();
int sum1=0;
int sum2=0;
int x;
if(n>m)
{
x=n;
n=m;
m=x;
}
for(int i=n;i<=m;i++)
{
if(i%2!=0)
{
sum2=sum2+i*i*i;
}
else
{
sum1=sum1+i*i;
}
}
System.out.println(sum1);
System.out.println(sum2);
}
}
}
2008 数值统计
Problem Description 统计给定的n个数中,负数、零和正数的个数。
Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
Output 对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
Sample Input
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
Sample Output
1 2 3
0 0 5
import java.util.Scanner;
public class test2008 {
static int fushu=0,ling=0,zhengshu=0;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
int[] arr=new int[k];
for(int i=0;i<k;i++) {
arr[i]=sc.nextInt();
}
for(int j=0;j<k;j++) {
if(arr[j]<0) {
fushu++;
}if(arr[j]>0) {
zhengshu++;
}else {
ling++;
}
}
System.out.println(fushu);
System.out.println(ling);
System.out.println(zhengshu);
}
}