P5718 找最小值
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
Arrays.sort(arr);
System.out.println(arr[0]);
}
}
P5719 分类平均
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
float sum = 0;float ans;
for (int i = k; i <= n; i += k) {
sum += i;
}
ans = (1 + n) * n / 2 - sum;
float a = sum / (n / k);
float b = ans / (n - (n / k));
String A = String.format("%.1f",a);
String B = String.format("%.1f",b);
System.out.println(A + " " + B);
//也可以直接使用c语言输出
System.out.printf("%.1f ",a);
System.out.printf("%.1f",b);
}
}
P5720 一尺之棰
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int count = 0;
for (int i = n; i > 0; i /= 2) {
count++;
}
System.out.println(count);
}
}
P5721 数字直角三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int num = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
if (num < 10){
System.out.print("0" + num);
}else System.out.print(num);
num++;
}
System.out.print("\n");
}
}
}
P1009 阶乘之和
这道题我写了两种解法,一种是利用BigInteger类;一种是for循环;
想了解更多BigInterger类用法,参考https://blog.csdn.net/weixin_44259720/article/details/87002816
import java.util.*;
import java.math.*;
public class Main {
static BigInteger jc(int n) {
BigInteger i=new BigInteger("1");
for (int j=2;j<=n;j++) i=i.multiply(new BigInteger(Integer.toString(j)));
return i;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
scan.close();
BigInteger sum=new BigInteger("0");
for (int i=1;i<=n;i++) sum=sum.add(jc(i));
scan.close();
System.out.println(sum);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
long N = 1;long sum = 0;
for (int i = 1; i <= n; i++) {
N *= i;
sum += N;
}
System.out.println(sum);
}
}
P1980 计数问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int count = 0;
for (int i = 1; i <= n; i++) {
String str1 = String.valueOf(i);
String str2 = String.valueOf(a);
int x = 0;
char[] ch = str1.toCharArray();
for (char c :ch){
if (c == str2.charAt(0)){
x++;
}
}
count += x;
}
System.out.println(count);
}
}
P1035 级数求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int k = input.nextInt();
double sum = 0;int n;
for (int i = 1; ;i++) {
sum += 1.0 / i;
if (sum > k){
System.out.println(i);
break;
}
}
}
}
P2669 金币
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int k;
k = (int) Math.ceil((Math.sqrt(8 * n + 1) - 1) / 2);
System.out.println(n * k - k * (k * k - 1) / 6);
}
}
P5722 数列求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println(sum);
}
}
P5723 质数口袋
质数经常会在题目中出现,我用的是欧拉筛选法,效率比较高
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();int count = 0;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)){
sum += i;
if (sum > n){
break;
}
System.out.println(i);
count++;
}
}
System.out.println(count);
}
public static boolean isPrime(int n){
if (n <= 3) return n > 1;
if (n % 6 != 1 && n % 6 != 5)return false;
int sqrt = (int)Math.sqrt(n);
for (int i = 5; i <= sqrt; i += 6) {
if (n % i == 0 || n % (i + 2) == 0){
return false;
}
}
return true;
}
}
P1217 回文质数 Prime Palindromes
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
for (int i = a; i <= b; i++) {
if (isPalindrome(i) && isPrime(i)){
System.out.println(i);
}
}
}
public static boolean isPrime(int n){
if (n <= 3) return n > 1;
if (n % 6 != 1 && n % 6 != 5)return false;
int sqrt = (int)Math.sqrt(n);
for (int i = 5; i <= sqrt; i += 6) {
if (n % i == 0 || n % (i + 2) == 0){
return false;
}
}
return true;
}
public static boolean isPalindrome(int n){
if (n < 0 || (n > 0 && n % 10 == 0)) return false;
int c = 0;
while (n > c){
c = c * 10 + n % 10;
n /= 10;
}
return c == n || n == c / 10;
}
}
P1423 小玉在游泳
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s = input.nextDouble();
//java中log函数是以e为底数
//ceil函数是向上取整
int n = (int) Math.ceil(Math.log(1 - s / 100) / Math.log(0.98));
System.out.println(n);
}
}
P1307 数字反转
这里用的是BufferedReader类,详细参考https://juejin.cn/post/7291133134036123706
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer(br.readLine().trim());
sb.reverse();
if (sb.charAt(sb.length() - 1) == '-'){
sb.delete(sb.length() - 1, sb.length());
sb.insert(0, '-');
}
System.out.println(Integer.parseInt(sb.toString()));
}
}
P1720 月落乌啼算钱(斐波那契数列)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] f = new double[100];
Scanner input = new Scanner(System.in);
int n = input.nextInt();
f[0] = 0;
f[1] = 1;
f[2] = 1;
for (int i = 3; i <= n; i++) {
f[i] = f[i - 1] + f[i - 2];
}
String x = String.format("%.2f",f[n]);
System.out.println(x);
}
}
P5724 求极差/最大跨度值
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
Arrays.sort(arr);
int d = arr[n - 1] - arr[0];
System.out.println(d);
}
}
P1420 最长连号
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}int count = 1;int ans = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] + 1 == a[i + 1]){
count++;
}else {
count = 1;
}
if (ans < count) {
ans = count;
}
}
System.out.println(ans);
}
}
P1075 质因数分解
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
System.out.println(n / i);
break;
}
}
}
}
P5725 求三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int i;int count = 0;
for (i = 1; i <= n * n; i++) {
if (i % n == 1 && i != 1){
System.out.print("\n");
}
if (i < 10){
System.out.print(0);
}
System.out.print(i);
}
System.out.println("\n");
i = 2 * n;
while (i > 0){
i -= 2;
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j < (2 * n - i) / 2; j++) {
count++;
if (count < 10){
System.out.print(0);
}
System.out.print(count);
}
System.out.print("\n");
}
}
}
P5726 打分
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] a = new int[n];int sum = 0;
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
sum += a[i];
}
Arrays.sort(a);
double ans = (sum - a[0] - a[n - 1])/(n - 2.0);
String s = String.format("%.2f", ans);
System.out.println(s);
}
}
P4956 Davor
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int x = 1, k = 1;int sum = (7 * x + 21 * k) * 52;
for (int i = 1; i <= 100; i++) {
if ((n - 364 * i) <= 0){
break;
}
if ((n - 364 * i) % 1092 == 0) {
x = i;
}
}
k = (n - 364 * x) / 1092;
System.out.println(x + "\n" + k);
}
}
P1089 津津的储蓄计划
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int b, i, c = 0;
int sum = 300;
int x = 0;
int m = 0;
double ssum = 0;
Scanner input = new Scanner(System.in);
for (i = 1; i <= 12; i++) {
int a = input.nextInt();
if (sum < a && x == 0) {
x = i;
} else {
b = sum - a;
if (b >= 100) {
c = b % 100;
m = m + b / 100; // m=1,
sum = c + 300;
ssum = c + m * 100 + m * 100 * 0.2;
} else {
sum = b + 300;
ssum = b + m * 100 + m * 100 * 0.2;
}
}
}
if (x != 0) {
System.out.println("-" + x);
} else {
System.out.println((int) ssum);
}
}
}