👩🏫 地址
1. 蘑菇炸弹
👩🏫 蘑菇炸弹
🎉 AC code
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
int cnt = 0;
for (int i = 1; i < n - 1; i++)
if (a[i] >= a[i - 1] + a[i + 1])
cnt++;
System.out.println(cnt);
}
}
2. 构造数字
👩🏫 构造数字
🎉 AC code
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[] ans = new char[n];
for (int i = 0; i < n; i++)
{
if (m > 0)
{
if (m >= 9)
{
ans[i] = (char) (9 + '0');
m -= 9;
} else
{
ans[i] = (char) (m + '0');
m = 0;
}
} else
{
ans[i] = '0';
}
}
// System.out.println(ans[0]);
System.out.println(String.valueOf(ans));
}
}
3. 小蓝的金牌梦
👩🏫 题目地址
🎉 AC code
import java.util.HashSet;
import java.util.Scanner;
public class Main
{
static int N = 100010;
static boolean[] st = new boolean[N];
static int[] p = new int[N];
static int cnt = 0;
static void init(int n)
{
for (int i = 2; i <= n; i++)
{
if (!st[i])
p[cnt++] = i;
for (int j = 0; p[j] <= n / i; j++)
{
st[p[j] * i] = true;
if (i % p[j] == 0)
break;
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
init(n);
int[] a = new int[n + 1];
long[] s = new long[n + 1];
s[0] = 0;
for (int i = 1; i <= n; i++)
{
a[i] = sc.nextInt();
s[i] = s[i - 1] + a[i];
}
long ans = Long.MIN_VALUE;
for (int i = 0; i < cnt; i++)
{
int len = p[i];
for (int j = 0; j + len <= n; j++)
{
long x = s[j + len] - s[j];
ans = ans < x ? x : ans;
}
}
System.out.println(ans);
}
}
4. 合并石子
⭐ 思路
找规律:最基础的三个a.b,c,不管是 a * b + (a+b)* c还是另一种选择a * c + (a+c) * b,不难发现两者完全相等(前缀和)
🎉 AC code
import java.util.*;
import java.math.BigInteger;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
BigInteger ans = new BigInteger("0");
BigInteger sum = new BigInteger(String.valueOf(a[0]));
for (int i = 0; i < n - 1; i++) {
ans = ans.add(sum.multiply(new BigInteger(String.valueOf(a[i+1]))));
sum = sum.add(new BigInteger(String.valueOf(a[i + 1])));
}
System.out.println(ans);
sc.close();
}
}