这几天兴趣来了刚好刷一刷杭电oj,顺便分享一下答案,都是些基础题,如果有问题欢迎随时指出,会保持更新.
1000:太简单
1001:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n = 0;
int sum = 0;
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
n=sc.nextInt();
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println(sum);
//
System.out.println();
sum=0;
}
sc.close();
}
}
1002:
格式很重要-回车一般都是用**\r\n,**所以一般我们也用这个,还要注意大数运算
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger a;
BigInteger b;
Scanner sc = new Scanner(System.in);
int T=0;
T=sc.nextInt();
for (int i = 1; i <= T; i++) {
a=sc.nextBigInteger();
b=sc.nextBigInteger();
//每个实例间需要换行
if(i==T) {
System.out.println("Case " + (i) + ":\r\n" + a + " + " + b + " = " + a.add(b));
}
else {
System.out.println("Case " + (i) + ":\r\n" + a + " + " + b + " = " + a.add(b)+"\r\n");
}
}
sc.close();
}
}
这里不想看英文了,开始刷中文题了哈哈
2000:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
while(in.hasNext()){
String charlist=in.nextLine();
char[] chars = charlist.toCharArray();
Arrays.sort(chars);
for(int i=0;i<chars.length;i++){
if(i==chars.length-1){
System.out.println(chars[i]);
}
else{
System.out.print(chars[i]+" ");
}
}
}
in.close();
}
}
2001:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double x1 = 0, y1 = 0;
double x2 = 0, y2 = 0;
while (in.hasNext()) {
x1 = in.nextDouble();
y1 = in.nextDouble();
x2 = in.nextDouble();
y2 = in.nextDouble();
System.out.println(String.format("%.2f",cau(x1,x2,y1,y2)));
}
in.close();
}
static double cau(double x1, double x2, double y1, double y2) {
return Math.sqrt(((x1-x2))*(x1-x2)+(y1-y2)*(y1-y2));
}
}
2002:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double r=0;
final Double pi=3.1415927;
while (in.hasNext()) {
r=in.nextDouble();
System.out.println(String.format("%.3f",4*(pi)*Math.pow(r,3)/3));
}
in.close();
}
}
2003:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
double A = in.nextDouble();
double B = Math.abs(A);
System.out.println((String.format("%.2f", B)));
}
in.close();
}
}
2004:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int score=in.nextInt();
System.out.println(judge(score));
}
in.close();
}
static String judge(Integer score){
if(0 <= score&&score<= 59){
return "E";
}
if(60 <= score&&score<= 69){
return "D";
}
if(70 <= score&&score<= 79){
return "C";
}
if(80 <= score&&score<= 89){
return "B";
}
if(90 <= score&&score<= 100){
return "A";
}
else{
return "Score is error!";
}
}
}
2005:开始陷入了判断语句过多的困境,没有想到直接定义每个月的天数然后用循环的方式
在这里import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
// 数组初始化 每个月的天数
int m[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
while (sn.hasNext()) {
String s = sn.nextLine();
String[] str = s.split("/");
int yy = Integer.parseInt(str[0]);
int mm = Integer.parseInt(str[1]);
int dd = Integer.parseInt(str[2]);
int flag = 0; // 表示闰年的标记
// 是闰年
if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) {
flag = 1;
}
int ans = 0; // 定义统计天数的变量
for (int i = 0; i < mm - 1; i++) {
ans = ans + m[i];
}
ans = ans + dd;
if (flag == 1 && mm > 2) {
ans++;
}
System.out.println(ans);
}
}
}
2006:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
int count=0;
int result=1;
while (sn.hasNext()) {
count = sn.nextInt();
int a = 0;
try {
for (int i = 0; i < count; i++) {
a = sn.nextInt();
if (a % 2 ==1) {
result = result * a;
}
}
}
catch (RuntimeException e){
e.printStackTrace();
}
finally {
if(result==1){
throw new RuntimeException("请输入至少一个奇数");
}
System.out.println(result);
result=1;//作初始化
}
}
}
}