大家有什么疑问可以在评论区评论,一起交流进步啊o(∩_∩)o
分段计算居民水费
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double s = scan.nextDouble();
double y;
if (s <= 15){
y = 1.0*4*s/3;
}else {
y = 2.5 * s - 17.5;
}
System.out.printf("%.2f",y);
}
}
简单题……
两个数的简单计算器
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
String next = scan.next();
int i1 = scan.nextInt();
switch (next) {
case "+":
i += i1;
break;
case "-":
i -= i1;
break;
case "*":
i *= i1;
break;
case "/":
i /= i1;
break;
case "%":
i %= i1;
break;
default:
next = "ERROR";
}
if ("ERROR".equals(next))
System.out.print("ERROR");
else
System.out.print(i);
}
}
简单题
日K蜡烛图
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double open = scan.nextDouble();
double high = scan.nextDouble();
double low = scan.nextDouble();
double close = scan.nextDouble();
String candle = "";
if (close < open) {
candle = "BW-Solid";
} else if ((close > open)) {
candle = "R-Hollow";
} else {
candle = "R-Cross";
}
if (low < open && low < close && high > open && high > close) {
System.out.print(candle + " with Lower Shadow and Upper Shadow");
return;
} else if (high > open && high > close) {
System.out.print(candle + " with Upper Shadow");
return;
} else if (low < open && low < close) {
System.out.print(candle + " with Lower Shadow");
return;
} else {
System.out.print(candle);
}
}
}
简单题
求整数段和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int A = scan.nextInt();
int B = scan.nextInt();
int sum = 0;
for (int i = A ;i<=B;i+=5){
for (int j = i;j<i+5&&j<=B;j++){
sum += j;
System.out.printf("%5d",j);
}
System.out.println();
}
System.out.print("Sum = "+sum);
}
}
简单题
计算圆周率
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double threshold = scan.nextDouble();
double pi = 1;
double Numerator=1,denominator=3;
int numeratorTem=1,denominatorTem=3;
while (true) {
double numn = Numerator / denominator;
pi += numn;
if (numn < threshold)
break;
Numerator *= (++numeratorTem);
denominatorTem += 2;
denominator *= denominatorTem;
}
System.out.printf("%.6f", pi * 2);
}
}
这道题有巨坑,分子分母必须一开始就是double,否则数据类型转换会出问题,过不了第二个测试点,具体哪里出问题我也不清楚,总之看了比人的博客知乎改成double就好了。
求符合给定条件的整数集
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int A = scan.nextInt();
for (int i = A; i < A + 4; i++) {
int flag = 1;
for (int j = A; j < A + 4; j++) {
if (j==i)
continue;
for (int k = A ;k<A+4;k++){
if (k ==i||k==j)
continue;
System.out.print(i*100+j*10+k);
flag++;
if(flag!=7)
System.out.print(" ");
}
}
System.out.println();
}
}
}
还行吧,算简单的
爬动的蠕虫
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int U = scan.nextInt();
int D = scan.nextInt();
int length = 0;
int mins = 1;
while (true){
if (mins%2 !=0)
length += U;
else {
length -= D;
}
if (length>=N)
break;
mins++;
}
System.out.println(mins);
}
}
蠕虫长度是糊弄人的,只需要关注头部即可。
二分法求多项式单根
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a3 = scan.nextDouble();
double a2 = scan.nextDouble();
double a1 = scan.nextDouble();
double a0 = scan.nextDouble();
double a = scan.nextDouble();
double b = scan.nextDouble();
double aResult = calculateResult(a3, a2, a1, a0, a);
double bResult = calculateResult(a3, a2, a1, a0, b);
double abResult = calculateResult(a3, a2, a1, a0, (a + b) / 2.0);
while (b - a >= 0.001) {
if (aResult == 0) {
System.out.printf("%.2f", a);
return;
} else if (bResult == 0) {
System.out.printf("%.2f", b);
return;
} else if (abResult == 0) {
System.out.printf("%.2f", (a + b) / 2.0);
return;
} else if (aResult * abResult < 0) {
b = (a + b) / 2.0;
bResult = calculateResult(a3, a2, a1, a0, b);
abResult = calculateResult(a3, a2, a1, a0, (a + b) / 2.0);
} else {
a = (a + b) / 2.0;
aResult = calculateResult(a3, a2, a1, a0, a);
abResult = calculateResult(a3, a2, a1, a0, (a + b) / 2.0);
}
}
System.out.printf("%.2f", (a + b) / 2.0);
}
private static double calculateResult(double a3, double a2, double a1, double a0, double x) {
return a0 + x * (a1 + x * (a2 + x * a3));
}
}
有点坑,精确到小数点后两位是什么意思呢?首先区间长度内的值都算它的解,这说明区间长度就是选出的这个解的误差范围,所以要保证小数点后两位精确,只需要把区间长度设置为0.001(或许0.0001才真正能保证),那这个解就可以表示为(X正负0.001,就是表示误差的那个意思),这是一个。还有一个是我自己是因为while循环内的输出格式忘了控制了导致有一个检测点出问题,妈的找了半天。
支票面额
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
double f = 0;
int y=0;
int cont = 0;
while (y < 100 && f < 100) {
f = (n + 199*y)/98.0;
if (f-(int)f == 0){
System.out.println(y+"."+(int)f);
cont++;
}
y++;
}
if (cont == 0){
System.out.println("No Solution");
}
}
}
思路就是得到公式之后一个一个试,两个值都是从0~99。
打印九九口诀表
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int i=1;i<=n;i++){
for (int j =1;j<=i;j++){
System.out.printf("%d*%d=%-4d",j,i,i*j);
}
System.out.println();
}
}
}
简单题