1. 修建灌木
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+1;i++){
int distance = Math.max(i-1,n-i);
System.out.println(distance*2);
}
scan.close();
}
}
2. 最少砝码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long target = scan.nextLong();
int num = 1;
long weight = 1l;
long total = 1l;
while (total < target){
num ++;
weight *= 3;
total += weight;
}
System.out.print(num);
scan.close();
//只有一个砝码 1
//只有两个砝码 4: 1 3
//只有三个砝码 13
}
}
3. 求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
long arr[] = new long[num];
long sum = 0l;
for(int i = 0;i<arr.length;i++){
arr[i] = scan.nextLong();
sum += arr[i];
}
long total = 0l;
for (int j = 0;j<arr.length;j++){
total += arr[j] * (sum - arr[j]);
sum -= arr[j];
}
System.out.println(total);
scan.close();
}
}
4. 成绩分析
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int arr[] = new int[num];
double sum = 0;
for(int i = 0;i < arr.length;i++){
arr[i] = scan.nextInt();
sum += arr[i];
}
double average = sum / num;
int min = arr[0];
int max = arr[0];
for(int j =0;j<arr.length;j++){
if(arr[j] > max){
max = arr[j];
}
if(arr[j] < min){
min = arr[j];
}
}
System.out.println(max);
System.out.println(min);
System.out.printf("%.2f",average);
scan.close();
}
}
//用sort
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int arr[] = new int[num];
double sum = 0;
for(int i = 0;i < arr.length;i++){
arr[i] = scan.nextInt();
sum += arr[i];
}
double average = sum / num;
Arrays.sort(arr);
System.out.println(arr[num-1]);
System.out.println(arr[0]);
System.out.printf("%.2f",average);
scan.close();
}
}
5. 合法日期
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int month = scan.nextInt();
int day = scan.nextInt();
int[] a = {31,28,31,30,31,30,31,31,30,31,30,31};
if(month <= 12 && day <= a[month-1]){
System.out.println("yes");
}else{
System.out.println("no");
}
scan.close();
}
}
6. 时间加法
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 t = scan. nextInt();
a += (b+t)/60;
b = (b+t)%60;
System.out.println(a);
System.out.println(b);
scan.close();
}
}
7. 扫雷
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int hang = scan.nextInt();
int lie = scan.nextInt();
int a[][] = new int[hang+2][lie+2];
for(int i = 1;i<hang+1;i++){
for (int j =1;j<lie+1;j++){
a[i][j] = scan.nextInt();
}
}
for(int i = 1;i<hang+1;i++){
for(int j =1;j<lie+1;j++){
if(a[i][j] == 1){
System.out.print(9 + " ");
}else{
System.out.print(a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1] + " ");
}
}
System.out.println();
}
scan.close();
}
}
8. 大写
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String zifuchuan = scan.next();
System.out.print(zifuchuan.toUpperCase());
scan.close();
}
}
9. 标题统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String title = scan.nextLine().replace(" ","");
System.out.print(title.length());
scan.close();
}
}
nextLine()可以接受空格
10. 求和
import java.util.*;
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i=1;i<=2019;i++){
String shuzi = String.valueOf(i);
if (shuzi.contains("2") || shuzi.contains("0") || shuzi.contains("1") || shuzi.contains("9")){
sum += i;
}
}
System.out.print(sum);
}
}
11. 天数
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int month = scan.nextInt();
int[] a = {31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print(a[month-1]);
scan.close();
}
}
12. 最大间隙
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int len = scan.nextInt();
int arr[] = new int[len];
for (int i =0;i<arr.length;i++){
arr[i] = scan.nextInt();
}
int max = 0;
int low = 0;
int fast = 1;
int cha = 0;
while(fast <= arr.length-1){
cha = arr[fast] - arr[low];
if(cha > max){
max = cha;
}
low++;
fast++;
}
System.out.print(max);
scan.close();
}
}
13. 金币
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int day = scan.nextInt();
int i = 0;
int count = 1;
int money =0;
while(i<day){
money += count*count;
i += count++;
}
if(i>day){
money -= (i-day)*(count-1);
}
System.out.print(money);
scan.close();
}
}