Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: Code01_AppleMinBags","url":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code01_AppleMinBags.cpp","tests":[{"id":1762100204708,"input":"","output":"1 -1\r\n2 -1\r\n3 -1\r\n4 -1\r\n5 -1\r\n6 1\r\n7 -1\r\n8 1\r\n9 -1\r\n10 -1\r\n11 -1\r\n12 2\r\n13 -1\r\n14 2\r\n15 -1\r\n16 2\r\n17 -1\r\n18 3\r\n19 -1\r\n20 3\r\n21 -1\r\n22 3\r\n23 -1\r\n24 3\r\n25 -1\r\n26 4\r\n27 -1\r\n28 4\r\n29 -1\r\n30 4\r\n31 -1\r\n32 4\r\n33 -1\r\n34 5\r\n35 -1\r\n36 5\r\n37 -1\r\n38 5\r\n39 -1\r\n40 5\r\n41 -1\r\n42 6\r\n43 -1\r\n44 6\r\n45 -1\r\n46 6\r\n47 -1\r\n48 6\r\n49 -1\r\n50 7\r\n"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code01_AppleMinBags.cpp","group":"local","local":true}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: Code02_EatGrass","url":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code02_EatGrass.cpp","tests":[{"id":1762101974289,"input":"","output":"1 A\r\n2 B\r\n3 A\r\n4 A\r\n5 B\r\n6 A\r\n7 B\r\n8 A\r\n9 A\r\n10 B\r\n11 A\r\n12 B\r\n13 A\r\n14 A\r\n15 B\r\n16 A\r\n17 B\r\n18 A\r\n19 A\r\n20 B\r\n21 A\r\n22 B\r\n23 A\r\n24 A\r\n25 B\r\n26 A\r\n27 B\r\n28 A\r\n29 A\r\n30 B\r\n31 A\r\n32 B\r\n33 A\r\n34 A\r\n35 B\r\n36 A\r\n37 B\r\n38 A\r\n39 A\r\n40 B\r\n41 A\r\n42 B\r\n43 A\r\n44 A\r\n45 B\r\n46 A\r\n47 B\r\n48 A\r\n49 A\r\n50 B\r\n"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code02_EatGrass.cpp","group":"local","local":true}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: Code03_IsSumOfConsecutiveNumbers","url":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code03_IsSumOfConsecutiveNumbers.cpp","tests":[{"id":1762103056617,"input":"16","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code03_IsSumOfConsecutiveNumbers.cpp","group":"local","local":true}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: Code04_RedPalindromeGoodStrings","url":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code04_RedPalindromeGoodStrings.cpp","tests":[{"id":1762144808384,"input":"12","output":"1 0\r\n2 3\r\n3 18\r\n4 30\r\n5 36\r\n6 42\r\n7 48\r\n8 54\r\n9 60\r\n10 66\r\n"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\Users\\damin\\Documents\\Microsoft VS Code\\files\\learn\\algorithm-journey\\src\\class042\\Code04_RedPalindromeGoodStrings.cpp","group":"local","local":true}
69 changes: 69 additions & 0 deletions src/class042/Code01_AppleMinBags.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 问题:
// 有装下8个苹果的袋子、装下6个苹果的袋子,一定要保证买苹果时所有使用的袋子都装满
// 对于无法装满所有袋子的方案不予考虑,给定n个苹果,返回至少要多少个袋子
// 如果不存在每个袋子都装满的方案返回-1

// 输入:
// 一行一个正整数n

// 输出:
// 一个整数表示答案

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int ans = INF;
void dfs(int x, int n)
{
if (x < 0)
return;
if (x == 0)
ans = min(ans, n);
dfs(x - 6, n + 1);
dfs(x - 8, n + 1);
}
int main()
{
// 打表找规律
// for (int i = 1; i <= 50; ++i)
// {
// ans = INF;
// dfs(i, 0);
// cout << i << " " << (ans == INF ? -1 : ans) << "\n";
// }

// 验证规律
// for (int n = 1; n <= 50; ++n)
// {
// cout << n << " ";
// if (n == 6 || n == 8)
// {
// cout << "1\n";
// }
// else if (n % 2 || n <= 10)
// {
// cout << "-1\n";
// }
// else
// {
// cout << (n - 1) / 8 + 1 << "\n";
// }
// }

// 答案
int n;
cin >> n;
if (n % 2 || n <= 10)
{
cout << "-1\n";
}
else if (n == 6 || n == 8)
{
cout << "1\n";
}
else
{
cout << (n - 1) / 8 + 1 << "\n";
}
return 0;
}
63 changes: 63 additions & 0 deletions src/class042/Code02_EatGrass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 问题:
// 草一共有n的重量,两只牛轮流吃草,A牛先吃,B牛后吃
// 每只牛在自己的回合,吃草的重量必须是4的幂,1、4、16、64....
// 谁在自己的回合正好把草吃完谁赢,根据输入的n,返回谁赢

// 输入:
// 一行一个整数表示草的重量

// 输出:
// 一行一个字符串表示赢的牛的名字

#include <bits/stdc++.h>
using namespace std;
string dfs(string cur, string ene, int rest)
{
if (rest == 0)
return ene;
int w = 1;
while (w <= rest)
{
if (dfs(ene, cur, rest - w) == cur)
{
return cur;
}
w *= 4;
}
return ene;
}
int main()
{
// 打表找规律
// for (int i = 1; i <= 50; ++i)
// {
// cout << i << " " << dfs("A", "B", i) << "\n";
// }

// 验证规律
// for (int n = 1; n <= 50; ++n)
// {
// cout << n << " ";
// if (n % 5 == 2 || n % 5 == 0)
// {
// cout << "B\n";
// }
// else
// {
// cout << "A\n";
// }
// }

// 答案
int n;
cin >> n;
if (n % 5 == 2 || n % 5 == 0)
{
cout << "B\n";
}
else
{
cout << "A\n";
}
return 0;
}
50 changes: 50 additions & 0 deletions src/class042/Code03_IsSumOfConsecutiveNumbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 问题:
// 判断一个数字是否是若干数量(数量>1)的连续正整数的和

// 输入:
// 一行一个正整数n

// 输出:
// 若n是若干连续正整数的和,输出T,否则输出F

#include <bits/stdc++.h>
using namespace std;
bool chk(int n)
{
for (int i = 1; i < n; ++i)
{
int sum = 0;
for (int j = i; sum <= n; ++j)
{
if (sum == n)
{
return true;
}
sum += j;
}
}
return false;
}
int bitn(int x)
{
int cnt = 0;
while (x)
{
cnt += x & 1;
x >>= 1;
}
return cnt;
}
int main()
{
// 打表
// for (int i = 1; i <= 80; ++i)
// {
// cout << i << " " << (chk(i) ? "T" : "F") << "\n";
// }

// 答案
int n;
cin >> n;
cout << (bitn(n) == 1 ? "F" : "T") << "\n";
}
111 changes: 111 additions & 0 deletions src/class042/Code04_RedPalindromeGoodStrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 问题:
// 可以用r、e、d三种字符拼接字符串,如果拼出来的字符串中
// 有且仅有1个长度>=2的回文子串,那么这个字符串定义为"好串"
// 返回长度为n的所有可能的字符串中,好串有多少个
// 结果对 1000000007 取模, 1 <= n <= 10^9

// 输入:
// 一行一个正整数n表示字符串长度

// 输出:
// 一个正整数表示好串的个数

// 示例:
// n = 1, 输出0
// n = 2, 输出3
// n = 3, 输出18

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string path;
ll ans = 0;
bool chk(int l, int r)
{
for (int i = l; i <= r; ++i)
{
if (path[i] != path[r - i + l])
{
return false;
}
}
return true;
}
void dfs(int x, int n)
{
if (x == n)
{
int sum = 0;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (sum > 1)
return;
sum += chk(i, j);
}
}
ans += (sum == 1);
return;
}
path[x] = 'r';
dfs(x + 1, n);
path[x] = 'e';
dfs(x + 1, n);
path[x] = 'd';
dfs(x + 1, n);
}
int main()
{
// 打表
// for (int i = 1; i <= 12; ++i)
// {
// path.resize(i);
// ans = 0;
// dfs(0, i);
// cout << i << " " << ans << "\n";
// }

// 验证
// for (int n = 1; n <= 12; ++n)
// {
// cout << n << " ";
// if (n == 1)
// {
// cout << "0\n";
// }
// else if (n == 2)
// {
// cout << "1\n";
// }
// else if (n == 3)
// {
// cout << "2\n";
// }
// else
// {
// cout << (n + 1) * 6 << "\n";
// }
// }

// 答案
int n;
cin >> n;
if (n == 1)
{
cout << "0\n";
}
else if (n == 2)
{
cout << "1\n";
}
else if (n == 3)
{
cout << "2\n";
}
else
{
cout << (n + 1) * 6 << "\n";
}
return 0;
}