A题 代码提交 链接
按照题意输出即可,注意读取空格;
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <unordered_map>
#include <map>
#include <set>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef pair<pii, int> ppi;
typedef pair<pii, pii> ppp;
typedef long long ll;
typedef int type;
const int N = 1e6 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
void solve()
{
string s;
cin >> s;
if(s[0] == 'A') cout << "守序善良";
if(s[0] == 'W') cout << "中立善良";
if(s[0] == 'T') cout << "混乱善良";
if(s[0] == 'M') cout << "守序中立";
if(s[0] == 'S') cout << "绝对中立";
if(s[0] == 'C') cout << "混乱中立";
if(s[0] == 'R') cout << "守序邪恶";
if(s[0] == 'P') cout << "中立邪恶";
if(s[0] == 'O') cout << "混乱邪恶";
}
int main()
{
int T = 1;
// cin >> T;
while(T --)
{
solve();
}
return 0;
}
B题 删字符 链接
输入三个字符,只打印首个字符和结尾字符;
#include <iostream>
using namespace std;
int main (){
char c[3];
cin >> c ;
cout << c[0] << c[2] << endl;
}
C题 字符串 链接
将每个子串存入vector中,排序后输出
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string str;
vector<string> nums;
int main(){
cin >> str;
for(int i = 0 ; i < str.size() - 1 ; i ++){
string t = "";
t += str[i];
t += str[i + 1];
nums.push_back(t);
}
sort(nums.begin() , nums.end());
for(int i = 0 ; i < nums.size() ; i ++)
cout << nums[i] << endl;
}
D题 田地灌溉(easy) 链接
灌溉时不重复,所以必然是按层排,
又根据等边三角形的特性,每从尖端往下一层,所需的无人机加2,
所以本质上是求等差数列的和;
这里设大三角的边长为x,小三角的边长为y,层数为c,
则c = ceil(x / y)(ceil:向上取整),
则结果为(1 + 2 * c - 1) * c / 2,整理后结果得c * c;
注意数据范围!
#include <iostream>
#include <cmath>
using namespace std;
int main(){
long long x , y ;cin >> x >> y ;
long long t = ceil((double)x / y);
long long res = t * t;
cout << res << endl;
}
E题 数字拆解 链接
数据范围表示,本题最优解为使用字符串处理;
题目只需要偶数,不妨可以定义一个临时字符串,循环遍历输入数字的每一位,
当某一位置的数字为奇数时,临时字符串添加该字符,
当某一位置的数字为偶数时,将组合好的字符串存入vector数组中,然后初始化临时字符串,
循环执行此操作;
比较字符串数字大小时,若是位数相同,则按照字典序排列,若是位数不同,则按照位数排列;
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<LL , LL> PII;
int T;
const int N = 2 * 100010;
int n , cnt = 0;
int nums[N];
bool cmp(string a , string b){
if(a.size() == b.size())
return a < b;
else
return a.size() < b.size();
}
void resoval()
{
string str , t = "";
cin >> str;
vector<string> res;
for(int i = 0 ; i < str.size() ; i ++)
{
t += str[i];
if(str[i] % 2 == 0){
res.push_back(t);
t = "";
}
}
sort(res.begin() , res.end() , cmp);
for(int i = 0 ; i < res.size() ; i ++)
cout << res[i] << endl;
}
int main()
{
ios::sync_with_stdio(false);
T = 1;
while(T --)
{
resoval();
}
}
F题 小成的最小公倍数 链接
经典最大公约数的模板题;
如果两数的最大公约数为1,则输出YES;
否则输出NO;
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
long long x , y; cin >>x >> y;
if(__gcd(x , y) == 1) cout << "YES" << endl;
else cout << "NO" << endl;
}
G题 田地灌溉(hard) 链接
模拟题,思路与easy版本类似,每累一层计算一次弦长,用弦长除以正方形的边长就是这一层所需的个数,弦长可以使用弦长公式,也可以使用勾股定理,(弦长公式比较麻烦,所以这里解释勾股定理的做法),做法见下图:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N = 100000;
int x;
string str;
queue<int> res;
typedef long double LL;
typedef long long L;
const double PI = 3.14;
int main()
{
L r, a;
cin >> r >> a;
L d = 2 * r;
if(a >= d){
cout << 1;
return 0;
}
if(a >= r){
cout << 4;
return 0;
}
if(a < r){
L cnt = 0;
L i = 1;
for( ; r - (i * a) > 0 ; i ++)
{
LL xian = 2 * sqrt(pow(r , 2) - pow(r - (i * a) , 2));
cnt += ceil(xian / a);
}
cnt *= 2;
L y = (r - a * (i - 1));
if((2 * y) > a)
{
LL xian = 2 * sqrt(pow(r , 2) - pow(y , 2));
cnt += 2 * ceil(xian / a);
}
else
cnt += ceil((LL)d / a);
cout << cnt;
}
}
H题 字符二叉树层序遍历 链接
经典二叉树问题,由于该二叉树较为特殊,所以有两种解法,递归解法与非递归解法;
递归解法:
使用dfs遍历每此划分出的字符串,建立一个map<int , string>, 哈希的键为深度,值为该深度下的字符串,跑完dfs后按顺序输出每一层的字符串即可;
#include <iostream>
#include <map>
using namespace std;
int cnt;
map<int , string> res;
void dfs(string str)
{
int len = str.size();
if(len <= 0) return;
int mid = len / 2;
res[cnt] += str[mid];
cnt ++;
dfs(str.substr(0 , mid));
dfs(str.substr(mid + 1 , mid));
cnt --;
}
int main()
{
int n; cin >> n;
string str; cin >> str;
dfs(str);
for(auto i : res)
cout << i.second;
}
非递归解法:
由于是满二叉树,所以按照 2^k 的步长进行+1运算,最后按照大小关系打印出字符串即可;
#include <iostream>
#include <cmath>
#include <map>
#include <cstring>
#include <vector>
using namespace std;
const int N = 2e5 + 10;
int q[N];
int k = 2;
int main()
{
int n;
cin >> n;
string s;
cin >> s;
map<int , int> st;
for(int i = 1; pow(2 , i) <= n; i ++)
{
int t = pow(2 , i);
for(int j = t - 1 ; j < n ; j += t)
st[j] ++;
}
map<int , vector<int>> res;
for(int i = n - 1 ; i >= 0 ; i --)
{
//cout << st[i] << ' ';
res[st[i]].push_back(i);
}
//return 0;
string ans = "";
for(auto i : res){
for(auto j : i.second)
ans += s[j];
}
for(int i = ans.size() - 1 ; i >= 0 ; i --)
cout << ans[i];
}
I题 编辑器IDE括号问题 链接
数据结构的题,我用的deque(双端队列),将I两边的字符串分为两个deque,按照题意进行操作即可;
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<LL , LL> PII;
int T;
const int N = 2 * 100010;
vector<char> str;
deque<char> lstr , rstr;
int n , k;
void delet()
{
if(rstr.size()){
rstr.pop_front();
}
}
void resoval()
{
cin >> n >> k;
for(int i = 0 ; i < n ; i ++){
char x; cin >> x;
str.push_back(x);
}
vector<char> t;
for(int i = 0 ; i < n ; i ++)
{
if(str[i] == 'I'){
for(int i = 0 ; i < t.size() ; i ++)
lstr.push_back(t[i]);
t.clear();
}
else t.push_back(str[i]);
}
for(int i = 0 ; i < t.size() ; i ++)
rstr.push_back(t[i]);
while(k --)
{
string x; cin >> x;
if(x == "backspace"){
if(lstr.size() > 0){
if(lstr.back() == '(')
{
lstr.pop_back();
if(rstr.front() == ')')
delet();
}
else
lstr.pop_back();
}
}
else if(x == "delete"){
delet();
}
else if(x == "<-"){
if(lstr.size()){
char x = lstr.back();
lstr.pop_back();
rstr.push_front(x);
}
}
else if(x == "->"){
if(rstr.size()){
char x = rstr.front();
//cout << x << endl;
rstr.pop_front();
lstr.push_back(x);
}
}
}
while(lstr.size()){
cout << lstr.front();
lstr.pop_front();
}
cout << "I";
while(rstr.size()){
cout << rstr.front();
rstr.pop_front();
}
cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
T = 1;
while(T --)
{
resoval();
}
}