三角形面积
提取
小方格面积是1
三角形面积是多少
分析
复述
思路
三角形面积=底*高/2
用该三角形的外接矩形减去3个三角形的面积
模拟
时间复杂度
收获
import java.io.IOException;
class MC{
public void run(){
int s = 8 * 8 - 8 * 4 / 2 - 4 * 6 / 2 - 2 * 8 / 2;
System.out.println(s);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
立方变自身
提取
某个数字的立方,按位累加仍然等于自身
分析
复述
某个数字的立方,按位累加仍然等于自身
思路
就直接模拟
模拟
时间复杂度
O ( 1 ) O(1) O(1)
收获
import java.io.IOException;
class MC{
public void run(){
int cnt = 0;
for (int i = 1; i <= Math.pow(Integer.MAX_VALUE, 1.0/3); i++) {
int j = i * i * i;
int r = 0;
while (j > 0){
r += j % 10;
j /= 10;
}
if(r == i) cnt ++;
}
System.out.println(cnt);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
三羊献瑞
提取
相同的汉字代表相同的数字
不同的汉字代表不同的数字
分析
每个字代表不同数字,按加法相加得到特定数字
复述
用0-9的数字代表每个字,用几个字组成词相加后得到特定的值,问三羊献瑞代表哪几个字
思路
经典全排列
全排列0-9,取前8位分别代表每个字
模拟
时间复杂度
O ( 9 ! ) O(9!) O(9!)
收获
import java.io.IOException;
import java.text.ParseException;
class MC{
int N = 11, n = 9;
//三 羊 献 瑞 祥 生 辉 气
//0 1 2 3 4 5 6 7 8 9
int[] a = new int[N];
int[] an = new int[5];
boolean[] st = new boolean[N];
boolean check() {
if (a[0] == 0) return false;
int r1, r2, r3;
//祥 瑞 生 辉
r1 = a[6] + a[5] * 10 + a[3] * 100 + a[4] * 1000;
//三 羊 献 瑞
r2 = a[3] + a[2] * 10 + a[1] * 100 + a[0] * 1000;
//三 羊 生 瑞 气
r3 = a[7] + a[3] * 10 + a[5] * 100 + a[1] * 1000 + a[0] * 10000;
return r1 + r2 == r3;
}
void dfs(int cnt){
if(cnt == n + 1){
if(check()) {
for (int i = 0; i < 4; i++) {
an[i] = a[i];
}
}
return;
}
for (int i = 0; i <= n; i++) {
if(st[i]) continue;
st[i] = true;
a[cnt] = i;
dfs(cnt + 1);
st[i] = false;
}
}
public void run(){
dfs(0);
System.out.printf("%d%d%d%d\n", an[0], an[1], an[2], an[3]);
}
}
public class Main {
public static void main(String[] args) throws IOException, ParseException {
new MC().run();
}
}
加法变乘法
提取
分析
复述
思路
若第i个数与第i+1个数相差,则总和s要减去这两个数,再加上这两个数的乘积
所以我们枚举第一个乘号,再枚举第二个乘号即可
模拟
时间复杂度
O ( 4 8 2 ) O(48 ^ 2) O(482)
收获
import java.io.IOException;
class MC{
public void run(){
//枚举第一个乘号
for (int i = 1; i <= 48; i++) {
//不相邻
//枚举第二个乘号
for (int j = i + 2; j <= 48; j++) {
int s;
s = 1225 + ((i * (i + 1)) - i - (i + 1)) + ((j * (j + 1)) - j - (j + 1));
if(s == 2015) System.out.printf("%d %d\n", i, j);
}
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
牌型种数
提取
分析
复述
思路
多重背包
//物品是1-13
//物品个数是4
//物品体积是1
//背包容量是13
//前i个物品的体积恰好是j的选法
模拟
时间复杂度
O(13^2*5)
收获
什么问题都往背包想一想
import java.io.IOException;
class MC{
//物品是1-13
//物品个数是4
//物品体积是1
//背包容量是13
//前i个物品的体积恰好是j的选法
int f[][] = new int[20][20];
public void run(){
f[0][0] = 1;
for (int i = 1; i <= 13; i++) {
for (int j = 0; j <= 13; j++) {
for (int k = 0; k <= 4 && k <= j; k++) {
f[i][j] += f[i -1][j - k];
}
}
}
System.out.println(f[13][13]);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
import java.util.Scanner;
class MC{
int res = 0;
void dfs(int k, int cnt){
if (k > 13|| cnt > 13) return;
//只有13张牌且
if (k == 13 && cnt == 13){
res++;
return;
}
for (int i = 0; i < 5; i++) {
//k:考虑第k种牌,cnt:去第取第k种牌i次
dfs(k + 1, cnt + i);
}
}
public void run(){
dfs(0, 0);
System.out.println(res);
}
Scanner sc = new Scanner(System.in);
}
public class Main {
public static void main(String[] args) {
new MC().run();
}
}
饮料换购
提取
分析
复述
思路
就直接模拟
模拟
时间复杂度
O ( n ) O(n) O(n)
收获
模拟大法好
import java.io.IOException;
import java.util.Scanner;
class MC{
//瓶盖和瓶子分开计算
public void run(){
Scanner sc= new Scanner(System.in);
while(sc.hasNext()){
int sum = sc.nextInt();
int c1 = sum;
while(true){
int t = c1;
c1 = t / 3;
if(c1 == 0) break;
sum += c1;
c1 += t % 3;
}
System.out.println(sum);
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}