目录
这场比赛感觉还行,但是罚时挺多的,多是因为忘记关freopen,然后去搜了搜相关的资料。可以用宏来判断oj的变量,提交oj时会不编译这块。还挺好用的
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
A. Number Transformation
题意:每次给定正整数x,和y,输出整数a和b,使x在a次变为原来的bx倍后能等于y。
思路:特殊情况,a为1的时候,也就是x乘上b要等于y,如果满足输出1和b。最后再特判y/x不为整数的情况即可
#include <iostream>
#include<ctime>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#pragma warning(disable:4996);
using namespace std;
const int N = 10;
int x, y;
int n;
int t,a,b;
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> t;
while (t--) {
cin >> x >> y;
int ok = 0;
if (x > y)
cout << 0 << " " << 0 << endl;
else {
double tem = (double)y / x;
if (tem - (int)tem != 0)
cout << 0 << " " << 0 << endl;
else {
int k = (int)((double)y / x);
cout << 1 << " " << (int)((double)y / x) << endl;
}
}
}
}
B. Dictionary
题意:有两个不同字母组成的字符串,按字典序排列,给出一个字符串输出他的字典序排位。
思路:数据很小,开头打表输出即可。
#include <iostream>
#include<ctime>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#pragma warning(disable:4996);
using namespace std;
const int N = 10;
int x, y;
int n;
int t,a,b;
int arr[200][200];
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> t;
int k = 0;
for (int j = 0; j < 26; j++) {
for (int i = 0; i < 26; i++) {
if (j != i)
arr[j + 'a'][i + 'a'] = ++k;
}
}
while (t--) {
string s;
cin >> s;
int a = s[0], b = s[1];
cout << arr[a][b] << endl;
}
}
C. Infinite Replacement
题意:给定一个不定长度的只含a的字符串 s1,和一个任意字符串s2,每次可以把s1中的a替换为s2.问当前的情况下可以得到多少个不同的串(内容和下标都不同)
思路:有点麻烦的题,注意如果s2中含有a和其他字符,那么不同的字符串必为无穷,因为可以不断替换a,换完还有。再特判一次s2只有一个a的情况,因为这种替换不改变元素和长度不影响s1,最后就是替换的正常情况了。每个字符都可以选择换不换,那么就变成了求s1的子集个数
输出pow(2,s1.length)即可
#include <iostream>
#include<ctime>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#pragma warning(disable:4996);
using namespace std;
const int N = 10;
int x, y;
int n;
ll power(ll a,ll b) {
ll ans = 1;//初始化ans,应对幂次为1和0的情况
while (b) {
if (b % 2 == 0) {
b /= 2;
a = a * a;
}
else {
b -= 1;
ans = ans * a;
b /= 2;
a = a * a;
}
}
return ans;
}
int t,a,b;
int arr[200][200];
int judge(string s) {
int ok = 0;
for (auto x : s) {
if (x == 'a'&&s.length()>1)
return 1;
ok++;
}
return 0;
}
ll cnt(string s) {
int n = s.length();
return power(2, n);
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
// 提交记得注释啊啊啊啊啊啊啊
cin >> t;
while (t--) {
string a, b;
cin >> a >> b;
ll sum = cnt(a);
if (judge(b)) {//特判长度大于一的字符串是否含a
cout << -1 << endl;
}
else if (b.length() == 1 && b[0] == 'a')
cout << 1 << endl;
else if (b.length() == 1 && a.length() == 1) {
cout << 2 << endl;
}
else {
cout << sum << endl;
}
}
}
D. A-B-C Sort
题意:给定n个元素的a,空数组b和c。首先将a从后往前pop,pop出来的元素放入b的中间,如果b的长度为偶数就放在中间,奇数则选择放在中间数的左右两边。清空a后。
重复这个步骤把b的元素放入c,问最后c的元素能否保证不降序。
思路:
比赛的时候没想明白,if讨论了好久。其实整个过程就是把a的元素两个两个交换,问最后能否得到不降序的c,模拟然后判断有序即可
for _ in range(int(input())):
n,l=int(input()),list(map(int,input().split()))
for x in range(n-1,0,-2):
if l[x]<l[x-1]:
l[x],l[x-1]=l[x-1],l[x]
if l==sorted(l):
print("YES")
else:
print("NO")