目录
P5709 【深基2.习6】Apples Prologue / 苹果和虫子
P1217 [USACO1.5] 回文质数 Prime Palindromes
【入门1】顺序结构
B2002 Hello,World!
public class Main {
public static void main(String[] args) {
System.out.println("Hello,World!");
}
}
B2025 输出字符菱形
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 3; i >= 0; i--) {
for (int j = 0; j <= 3 - i; j++) {
System.out.print(" ");
}
for (int j = 2; j <= 2 * i - 2 ; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
P1000 超级玛丽游戏
public class Main {
public static void main(String[] args) {
System.out.println(" ********\n" +
" ************\n" +
" ####....#.\n" +
" #..###.....##....\n" +
" ###.......###### ### ###\n" +
" ........... #...# #...#\n" +
" ##*####### #.#.# #.#.#\n" +
" ####*******###### #.#.# #.#.#\n" +
" ...#***.****.*###.... #...# #...#\n" +
" ....**********##..... ### ###\n" +
" ....**** *****....\n" +
" #### ####\n" +
" ###### ######\n" +
"##############################################################\n" +
"#...#......#.##...#......#.##...#......#.##------------------#\n" +
"###########################################------------------#\n" +
"#..#....#....##..#....#....##..#....#....#####################\n" +
"########################################## #----------#\n" +
"#.....#......##.....#......##.....#......# #----------#\n" +
"########################################## #----------#\n" +
"#.#..#....#..##.#..#....#..##.#..#....#..# #----------#\n" +
"########################################## ############");
}
}
P1001 A+B Problem
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextInt();
long b = scanner.nextInt();
System.out.println(a + b);
}
}
B2005 字符三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double p = (a + b + c) / 2;
double s = Math.sqrt(p * ((p - a) * (p - b) * (p - c)));
System.out.printf("%.1f", s);
}
}
P5703 【深基2.例5】苹果采购
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextInt();
long b = scanner.nextInt();
System.out.println(a * b);
}
}
P5704 【深基2.例6】字母转换
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.println(s.toUpperCase());
}
}
P5705 【深基2.例7】数字反转
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
StringBuffer sb = new StringBuffer(s);
System.out.println(sb.reverse());
}
}
P5706 【深基2.例8】再分肥宅水
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double t = scanner.nextDouble();
int n = scanner.nextInt();
double ml = t / n;
int cup = n * 2;
System.out.printf("%.3f", ml);
System.out.println();
System.out.println(cup);
}
}
P5708 【深基2.习2】三角形面积
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double p = (a + b + c) / 2;
double s = Math.sqrt(p * ((p - a) * (p - b) * (p - c)));
System.out.printf("%.1f", s);
}
}
P5707 【深基2.例12】上学迟到
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int s = scanner.nextInt();
int v = scanner.nextInt();
int H ,M;
//路途所需时间
int i;
if (s % v == 0){
//踩点
i = 10 + s / v;
}else {
//早到一分钟
i = 11 + s/v;
}
int j;
if (480 - i < 0){
//提前一天
j = 1440 + (480 - i);
}else {
//不提前
j = 480 - i;
}
H = j / 60;
M = j % 60;
System.out.printf("%02d:%02d" , H, M);
}
}
B2029 大象喝水
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int h = scanner.nextInt();
int r = scanner.nextInt();
double a = 3.14 * r * r * h;
int b = 20 * 1000;//单位换算
int result = (int)Math.ceil(b / a);
System.out.println(result);
}
}
P1425 小鱼的游泳时间
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int starth = scanner.nextInt();
int startm = scanner.nextInt();
int endh = scanner.nextInt();
int endm = scanner.nextInt();
int resulth;
int resultm;
if (endm - startm >= 0){
resulth = endh - starth;
resultm = endm - startm;
}else {
resulth = endh - starth - 1;
resultm = 60 - Math.abs(endm - startm);
}
System.out.println(resulth + " " + resultm);
}
}
P1421 小玉买文具
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int total = a * 10 + b;
int count = total / 19;
System.out.println(count);
}
}
P3954 [NOIP2017 普及组] 成绩
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int ans = (int)(a * 0.2 + b * 0.3 + c * 0.5);
System.out.println(ans);
}
}
【入门2】分支结构
P2433 【深基1-2】小学数学 N 合一
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
if (T == 1) {
System.out.println("I love Luogu!");
} else if (T == 2) {
System.out.println(6 + " " + 4);
} else if (T == 3) {
System.out.println("3\n12\n2");
} else if (T == 4) {
System.out.println("166.667");
} else if (T == 5) {
System.out.println(15);
} else if (T == 6) {
System.out.println("10.8167");
} else if (T == 7) {
System.out.println("110\n90\n0");
} else if (T == 8) {
System.out.println("31.4159\n78.5398\n523.599");
} else if (T == 9) {
System.out.println(22);
} else if (T == 10) {
System.out.println(9);
} else if (T == 11) {
System.out.println("33.3333");
} else if (T == 12) {
System.out.println("13\nR");
} else if (T == 13) {
double pi = 3.141593;
System.out.println((int)(Math.pow(4.0/3*pi*(4*4*4+10*10*10),1.0*1/3)));
} else if (T == 14) {
System.out.println(50);
}
}
}
P5709 【深基2.习6】Apples Prologue / 苹果和虫子
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int t = scanner.nextInt();
int s = scanner.nextInt();
// 排0
if (t == 0) {
System.out.println(0);
} else if (s / t <= m) {
// 吃的苹果 不能比 有的苹果 多
int i = s / t;
// 正在吃的苹果没吃完
if (s % t > 0) {
i += 1;
}
System.out.println(m - i);
} else {
System.out.println(0);
}
}
}
P5710 【深基3.例2】数的性质
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
//小A
if ((a % 2 == 0) && (4 < a & a <= 12)) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
//Uim
if (a % 2 == 0 || 4 < a && a <= 12) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
//八尾勇
//" ^ " 与boolean类型是一样的,当两个boolean类型的变量同真或同假时,结果为假;两个变量一真一假时,结果为真。
if (a % 2 == 0 ^ 4 < a && a <= 12) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
//正妹
if ((a % 2 != 0) && (a <= 4 || a > 12)) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
}
}
P5711 【深基3.例3】闰年判断
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
P5712 【深基3.例4】Apples
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
if (x == 0 || x == 1) {
System.out.println("Today, I ate " + x + " apple.");
} else {
System.out.println("Today, I ate " + x + " apples.");
}
}
}
P5713 【深基3.例5】洛谷团队系统
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int local = n * 5;
int luogu = n * 3 + 11;
System.out.println(local > luogu ? "Luogu" : "Local");
}
}
P5714 【深基3.例7】肥胖问题
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double m = scanner.nextDouble();
double h = scanner.nextDouble();
double BMI = m / (h * h);
if (BMI < 18.5) {
System.out.println("Underweight");
} else if (BMI < 24) {
System.out.println("Normal");
} else if (BMI >= 24) {
BigDecimal a = new BigDecimal(String.valueOf(BMI));
//相当于 BigDecimal b = new BigDecimal("1")
BigDecimal b = BigDecimal.ONE;
//传入所占位数
MathContext mc = new MathContext(6);
System.out.println(a.divide(b, mc) + "\n" + "Overweight");
}
}
}
P5715 【深基3.例8】三位数排序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
if (a > b) {
int temp = a;
a = b;
b = temp;
}
System.out.println(a + " " + b + " " + c);
}
}
P5716 【深基3.例9】月份天数
import java.time.YearMonth;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
int month = scanner.nextInt();
// 使用YearMonth获取天数
YearMonth ym = YearMonth.of(year, month);
System.out.println(ym.lengthOfMonth());
}
}
P1085 [NOIP2004 普及组] 不高兴的津津
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int max = 0;
int index = 0;
int count = 0;
for (int i = 0; i < 7; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
if (a + b > 8) {
if (a + b > max) {
count++;
max = a + b;
index = i;
}
}
}
if (count == 0) {
System.out.println(0);
} else {
System.out.println(index + 1);
}
}
}
P1909 [NOIP2016 普及组] 买铅笔
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int price = 0;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int mod = n % a;
int count = n / a;
if (mod == 0) {
price = count * b;
} else {
price = ++count * b;
}
list.add(price);
}
Integer min = Collections.min(list);
System.out.println(min);
}
}
P5717 【深基3.习8】三角形分类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
// 判定三角形类型
judgeTriangleType(a, b, c);
}
private static void judgeTriangleType(int a, int b, int c) {
//判断是否三角形
if (a + b > c && a + c > b && b + c > a) {
//直角
if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) {
System.out.println("Right triangle");
} else {
//锐角和钝角
if (a * a + b * b > c * c && b * b + c * c > a * a && a * a + c * c > b * b) {
System.out.println("Acute triangle");
} else {
System.out.println("Obtuse triangle");
}
//等腰
if (a == b || b == c || a == c) {
System.out.println("Isosceles triangle");
//等边
if (a == b && b == c && a == c) {
System.out.println("Equilateral triangle");
}
}
}
} else {
System.out.println("Not triangle");
}
}
}
P1422 小玉家的电费
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
double prise;
if (n <= 150) {
prise = n * 0.4463;
} else if (n <= 400) {
prise = 150 * 0.4463 + 0.4663 * (n - 150);
} else {
prise = 150 * 0.4463 + 250 * 0.4663 + 0.5663 * (n - 400);
}
System.out.println(String.format("%.1f", prise));
}
}
P1424 小鱼的航程(改进版)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int n = scanner.nextInt();
long ans = 0;
// 循环n天
for (int i = 0; i < n; i++) {
// 如果当前位置不是周六或周日
if (x != 6 && x != 7) {
// 增加250到答案中
ans += 250;
}
// 如果当前位置是周日
if (x == 7) {
// 将位置设置为周一
x = 1;
} else {
// 否则将位置加1
x++;
}
}
System.out.println(ans);
}
}
P1888 三角函数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int min = Math.min(a, Math.min(b, c));
int max = Math.max(a, Math.max(b, c));
//求最大公约数
int gcd = getGcd(min, max);
System.out.println(min / gcd + "/" + max / gcd);
}
private static int getGcd(int a, int b) {
if (b == 0) {
return a;
}
return getGcd(b, a % b);
}
}
P1046 [NOIP2005 普及组] 陶陶摘苹果
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] temp = scanner.nextLine().split(" ");
int h = scanner.nextInt() + 30;
int count = 0;
for (int i = 0; i < temp.length; i++) {
int n = Integer.parseInt(temp[i]);
if (h >= n) {
count++;
}
}
System.out.println(count);
}
}
P4414 [COCI2006-2007#2] ABC
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[3];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
TreeMap<Character, Integer> map = new TreeMap<>();
for (int i = 0; i < arr.length; i++) {
map.put((char) ('A' + i), arr[i]);
}
char[] ch = scanner.next().toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(map.get(ch[i]) + " ");
}
}
}
P1055 [NOIP2008 普及组] ISBN 号码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next().replace("-", "");
//号码
int n = Integer.parseInt(s.substring(0, 9));
//校验码
String c = s.substring(9);
int m;
if ("X".equals(c)) {
m = 10;
} else {
m = Integer.parseInt(c);
}
//求和
int sum = 0;
int i = 9;
while (n > 0) {
int num = n % 10;
sum += num * i;
n /= 10;
i--;
}
int mod = sum % 11;
if (mod == m) {
System.out.println("Right");
} else {
if (mod ==10) {
System.out.println(s.substring(0, 1) + "-" + s.substring(1, 4) + "-" + s.substring(4, 9) + "-X");
} else {
System.out.println(s.substring(0, 1) + "-" + s.substring(1, 4) + "-" + s.substring(4, 9) + "-" + mod);
}
}
}
}
【入门3】循环结构
P5718 【深基4.例2】找最小值
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
System.out.println(arr[0]);
}
}
P5719 【深基4.例3】分类平均
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
double sum1 = 0, sum2 = 0;
int m = 0, j = 0;
//方法一:
for (int i = 1; i <= n; i++) {
if (i % k == 0) {
sum1 += i;
m++;
} else {
sum2 += i;
j++;
}
}
System.out.printf("%.1f %.1f", sum1 / m, sum2 / j);
//方法二:
/*for (int i = k; i <= n; i+=k) {
sum1 += i;
}
//通过等差数列求和公式 Sn=[n(A1+An)]/2
sum2 = n * (1 + n) / 2 - sum1;
System.out.printf("%.1f %.1f", sum1/(n/k),sum2/(n-n/k));*/
}
}
P5720 【深基4.例4】一尺之棰
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
// 从第一天开始
int count = 1;
while (n != 1) {
n = (int) Math.ceil(n / 2);
count++;
}
System.out.println(count);
}
}
P5721 【深基4.例6】数字直角三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 1;
// 打印数字直角三角形
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.printf("%02d", count++);
}
System.out.println();
}
}
}
P1009 [NOIP1998 普及组] 阶乘之和
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
BigInteger sum = new BigInteger("0");
for (int i = 1; i <= n; i++) {
BigInteger a = new BigInteger("1");
for (int j = 1; j <= i; j++) {
BigInteger b = new BigInteger(String.valueOf(j));
a = a.multiply(b);
}
sum = sum.add(a);
}
System.out.println(sum);
}
}
P1980 [NOIP2013 普及组] 计数问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
//方法一:
/*char x = scanner.next().charAt(0);
int count = 0;
for (int i = 1; i <= n; i++) {
String str = "" + i;
for (int j = 0; j <= str.length() - 1; j++) {
if (str.charAt(j) == x) {
count++;
}
}
}
System.out.println(count);*/
//方法二:
int x = scanner.nextInt();
int count = 0;
for (int i = 1; i <= n; i++) {
int t = i;
while (t > 0) {
if (t % 10 == x) {
count++;
}
t /= 10;
}
}
System.out.println(count);
}
}
P1035 [NOIP2002 普及组] 级数求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
double sum = 0;
int i = 0;
while (true) {
sum += 1.0 / ++i;
if (sum > k) {
System.out.println(i);
return;
}
}
}
}
P2669 [NOIP2015 普及组] 金币
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
//记录天数
int day = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
day++;
sum += i;
if (day == n) {
System.out.println(sum);
return;
}
}
}
}
}
P5722 【深基4.例11】数列求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println(sum);
}
}
P5723 【深基4.例13】质数口袋
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println(sum);
}
}
P1217 [USACO1.5] 回文质数 Prime Palindromes
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
//只有奇数才有可能是质数
if (a % 2 == 0) {
a++;
}
for (int i = a; i <= b; i += 2) {
//最大的回文质数是 7 位数
if (i >= 10000000) {
break;
}
if (isPalindrome(i)) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
}
private static boolean isPalindrome(long i) {
StringBuffer sb = new StringBuffer(Long.toString(i));
if (String.valueOf(i).equals(sb.reverse().toString())) {
return true;
}
return false;
}
private static boolean isPrime(long n) {
for (int j = 3; j <= Math.sqrt(n); j += 2) {
if (n % j == 0) {
return false;
}
}
return true;
}
}
P1423 小玉在游泳
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double s = scanner.nextDouble();
double sum = 0;
for (int i = 0;i < 9999 ; i++) {
sum += 2 * Math.pow(0.98, i);
if (sum >= s) {
System.out.println(i + 1);
break;
}
}
}
}
P1307 [NOIP2011 普及组] 数字反转
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
if (s.charAt(0) == '-') {
System.out.println("-" + Integer.parseInt(new StringBuilder(s.substring(1)).reverse().toString()));
} else {
System.out.println(Integer.parseInt(new StringBuilder(s).reverse().toString()));
}
}
}
P1720 月落乌啼算钱(斐波那契数列)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
//方法一:
//斐波那契数列公式
double temp = Math.sqrt(5.0);
double result = (Math.pow((1 + temp) / 2, n) - Math.pow((1 - temp) / 2, n)) / temp;
System.out.printf("%.2f", result);
//方法二:
//System.out.printf("%.2f", f(n));
}
private static double f(int n) {
if (n == 0 || n == 1) {
return n;
}
return f(n - 1) + f(n - 2);
}
}
P5724 【深基4.习5】求极差 / 最大跨度值
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(scanner.nextInt());
}
int max = Collections.max(list);
int min = Collections.min(list);
System.out.println(max - min);
}
}
P1420 最长连号
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
//记录长度
int len = 1;
//最大长度
int maxLen = 1;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1] - 1) {
len++;
} else {
len = 1;
}
maxLen = Math.max(len, maxLen);
}
System.out.println(maxLen);
}
}
P1075 [NOIP2012 普及组] 质因数分解
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
// 计算 sqrt(n) 的值
int limit = (int) Math.sqrt(n);
// 标记是否找到了最大质因数
boolean flag = false;
// 去掉所有的 2 的因子
while (n % 2 == 0) {
n /= 2;
}
// 从 3 开始遍历奇数
for (int i = 3; i <= limit; i += 2) {
// 判断 i 是否是质数
if (isPrime(i)) {
// 如果 i 是 n 的因子,就输出 i
while (n % i == 0) {
n /= i;
}
// 如果 n 已经被分解完了,就找到了最大质因数
if (n == 1) {
flag = true;
break;
}
}
}
// 如果没有找到最大质因数,说明 n 本身就是质数
if (!flag) {
System.out.print(n);
}
}
private static boolean isPrime(long n) {
for (int j = 3; j <= Math.sqrt(n); j += 2) {
if (n % j == 0) {
return false;
}
}
return true;
}
}
P5725 【深基4.习8】求三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 1;
//正方形
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.printf("%02d", count++);
}
System.out.println();
}
System.out.println();
count = 1;
//右三角形
for (int i = 1; i <= n; i++) {
//每行的空格
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
//每行输出的数字
for (int j = 1; j <= i; j++) {
System.out.printf("%02d", count++);
}
System.out.println();
}
}
}
P5726 【深基4.习9】打分
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(scanner.nextInt());
}
Integer max = Collections.max(list);
Integer min = Collections.min(list);
list.remove(max);
list.remove(min);
// 使用 reduce() 方法,传入初始值 0 和累加函数 Integer::sum,得到总和
double sum = list.stream().reduce(0, Integer::sum);
System.out.printf("%.2f", sum / list.size());
}
}
P4956 [COCI2017-2018#6] Davor
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//52周在一开始就可以除掉了,反正这52周是完全一样的。再来看每一周。把每天的数加起来,
//得到7x + 21k,化简得 7(x + 3k),于是7也可以开始除掉。
//所以,只要在开始把 52 × 7 = 364除掉,那么方程就变为:x + 3k = n.
//解这个不定方程时,也需要关注一些题目的条件。这道题的条件有:1≤ ≤100, 和k 为正整数,且使得 最大,最小。
int n = scanner.nextInt() / 364;
for (int i = 1; ; i++) {
for (int j = 1; j <= 100; j++) {
if (j + 3 * i == n) {
System.out.println(j);
System.out.println(i);
return;
}
}
}
}
}
P1089 [NOIP2004 提高组] 津津的储蓄计划
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int money = 0;
int bank = 0;
for (int i = 1; i <= 12; i++) {
money += 300;
int expense = scanner.nextInt();
if (money < expense) {
System.out.println(-i);
return;
}
money -= expense;
if (money / 100 > 0) {
int temp = money % 100;
//存入妈妈那
bank += money - temp;
//存入后剩余的钱
money -= money - temp;
}
}
System.out.println((int) (bank * 1.2 + money));
}
}
【入门4】数组
P1428 小鱼比可爱
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] fish = new int[n];
for (int i = 0; i < n; i++) {
fish[i] = scanner.nextInt();
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (fish[j] < fish[i]) {
count++;
}
}
System.out.print(count + " ");
count = 0;
}
}
}
P1427 小鱼的数字游戏
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] arr = scanner.nextLine().split(" ");
for (int i = arr.length - 2; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
P5727 【深基5.例3】冰雹猜想
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> list = new ArrayList<>();
list.add(n);
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
list.add(n);
}
for (int i = list.size() - 1; i >= 0; i--) {
System.out.print(list.get(i) + " ");
}
}
}
P1047 [NOIP2005 普及组] 校门外的树
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//实际上有501棵树
boolean[] trees = new boolean[scanner.nextInt()+ 1];
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int start = scanner.nextInt(), end = scanner.nextInt();
for (int j = start; j <= end; j++) {
trees[j] = true;
}
}
int count = 0;
for (boolean tree : trees) {
if (!tree) {
count++;
}
}
System.out.println(count);
}
}
P5728 【深基5.例5】旗鼓相当的对手
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] arr = new int[n][4];
for (int i = 0; i < arr.length; i++) {
int sum = 0;
for (int j = 0; j < 3; j++) {
arr[i][j] = scanner.nextInt();
sum += arr[i][j];
}
//把个人总成绩放入最后
arr[i][3] = sum;
}
//记录旗鼓相当的对手
int ans = 0;
//从后往前遍历
for (int i = arr.length - 1; i >= 0; i--) {
//从倒数第二个开始遍历
for (int j = i - 1; j >= 0; j--) {
//记录单个学生的单科成绩
int count = 0;
//三门成绩
for (int k = 0; k < 3; k++) {
if (Math.abs(arr[i][k] - arr[j][k]) <= 5) {
count++;
}
}
if (count == 3 && Math.abs(arr[i][3] - arr[j][3]) <= 10) {
ans++;
}
}
}
System.out.println(ans);
}
}
P5729 【深基5.例7】工艺品制作
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
int n = scanner.nextInt();
boolean[][][] arr = new boolean[x + 1][y + 1][z + 1];
for (int i = 0; i < n; i++) {
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
int z1 = scanner.nextInt();
int x2 = scanner.nextInt();
int y2 = scanner.nextInt();
int z2 = scanner.nextInt();
for (int j = x1; j <= x2; j++) {
for (int k = y1; k <= y2; k++) {
for (int l = z1; l <= z2; l++) {
arr[j][k][l] = true;
}
}
}
}
int count = 0;
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
for (int k = 1; k <= z; k++) {
if (!arr[i][j][k]) {
count++;
}
}
}
}
System.out.println(count);
}
}
P2550 [AHOI2001] 彩票摇奖
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine(); // 读取换行符
//存取中奖号码
List<Integer> list = new ArrayList<>();
String[] strs = scanner.nextLine().split(" ");
for (int i = 0; i < strs.length; i++) {
list.add(Integer.parseInt(strs[i]));
}
//存放中奖个数
int[] ans = new int[7];
//计算中奖号码个数
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < 7; j++) {
int temp = scanner.nextInt();
if (list.contains(temp)) {
count++;
}
}
ans[7 - count]++;
}
//遍历输出
for (int i = 0; i < ans.length; i++) {
System.out.print(ans[i] + " ");
}
}
}
P2615 [NOIP2015 提高组] 神奇的幻方
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//奇数
int n = scanner.nextInt();
int[][] arr = new int[n][n];
int row = 0, col = n / 2;
//第一行中间的为1
arr[row][col] = 1;
for (int i = 2; i <= n * n; i++) {
if (row == 0 && col != n - 1) {
row = n - 1;
col++;
} else if (col == n - 1 && row != 0) {
col = 0;
row--;
} else if (row == 0 && col == n - 1) {
row++;
} else if (arr[row - 1][col + 1] == 0) {
row--;
col++;
} else {
row++;
}
arr[row][col] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
P5730 【深基5.例10】显示屏
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入数字的位数
int n = scanner.nextInt();
// 输入数字
String num = scanner.next();
char[][][] digits = {
// 0
{{'X', 'X', 'X'}, {'X', '.', 'X'}, {'X', '.', 'X'}, {'X', '.', 'X'}, {'X', 'X', 'X'}},
// 1
{{'.', '.', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}},
// 2
{{'X', 'X', 'X'}, {'.', '.', 'X'}, {'X', 'X', 'X'}, {'X', '.', '.'}, {'X', 'X', 'X'}},
// 3
{{'X', 'X', 'X'}, {'.', '.', 'X'}, {'X', 'X', 'X'}, {'.', '.', 'X'}, {'X', 'X', 'X'}},
// 4
{{'X', '.', 'X'}, {'X', '.', 'X'}, {'X', 'X', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}},
// 5
{{'X', 'X', 'X'}, {'X', '.', '.'}, {'X', 'X', 'X'}, {'.', '.', 'X'}, {'X', 'X', 'X'}},
// 6
{{'X', 'X', 'X'}, {'X', '.', '.'}, {'X', 'X', 'X'}, {'X', '.', 'X'}, {'X', 'X', 'X'}},
// 7
{{'X', 'X', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}, {'.', '.', 'X'}},
// 8
{{'X', 'X', 'X'}, {'X', '.', 'X'}, {'X', 'X', 'X'}, {'X', '.', 'X'}, {'X', 'X', 'X'}},
// 9
{{'X', 'X', 'X'}, {'X', '.', 'X'}, {'X', 'X', 'X'}, {'.', '.', 'X'}, {'X', 'X', 'X'}}
};
// 对于每一行
for (int i = 0; i < 5; i++) {
// 对于每一位数字
for (int j = 0; j < n; j++) {
// 获取数字的值
int digit = num.charAt(j) - '0';
// 对于每一列
for (int k = 0; k < 3; k++) {
// 输出对应的点阵
System.out.print(digits[digit][i][k]);
}
// 输出间隔, 最后一位数字不需要加 .
if (j != n - 1) {
System.out.print('.');
}
}
// 换行
System.out.println();
}
}
}
P1554 梦中的统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scannerc = new Scanner(System.in);
int m = scannerc.nextInt();
int n = scannerc.nextInt();
int[] ans = new int[10];
for (int i = m; i <= n; i++) {
int num = i;
while (num != 0) {
ans[num % 10]++;
num /= 10;
}
}
for (int i = 0; i < ans.length; i++) {
System.out.print(ans[i] + " ");
}
}
}
P2141 [NOIP2014 普及组] 珠心算测验
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
int res = 0;
int target = 0;
for (int i = arr.length - 1; i > 1; i--) {
target = arr[i];
for (int left = 0, right = i; left < right;) {
int sum = arr[left] + arr[right];
if (sum < target) {
left++;
} else if (sum > target) {
right--;
} else {
res++;
break;
}
}
}
System.out.println(res);
}
}
持续更新中……