1.
Long Loong
思路:通过for循环控制打印输出字母o的数量
代码:
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
cout << "L";
for (int i = 0; i < x; i++)
cout << "o";
cout << "ng";
return 0;
}
2.
YES or YES?
思路:直接对可能出现的情况做判断
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s;
int l = s.length();
if (l == 3)
{
if (s[0] == 'y' || s[0] == 'Y')
{
if (s[1] == 'e' || s[1] == 'E')
{
if (s[2] == 's' || s[2] == 'S')
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
else
cout << "NO" << endl;
}
else
cout << "NO" << endl;
}
else
cout << "NO" << endl;
}
}
3.
Even? Odd? G
思路:
由于所输入的数据范围太大,,不论是int还是long long都无法储存,因此采用字符串来储存数据,最后只需判断字符串的最后一项是奇数还是偶数即可
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
string x;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x;
int l = x.length();
if ((x[l-1] - '0') % 2 == 0)
cout << "even" << endl;
else
cout << "odd" << endl;
}
}
4.
rules
思路:按照题目所提供信息,先遍历判断一天中遵守规则的人数,过半就计入有效天数cnt,最后再判断有效天数是否大于等于总天数的一半
#include <iostream>
using namespace std;
int main()
{
int n, m, k;
cin >> n >> m >> k;
int cnt = 0;
int x,c;
for (int i = 0; i < m; i++)
{
c = 0;
for (int j = 0; j < n; j++)
{
cin >> x;
if (x == k)
c++;
}
if (c >= (double)n * 0.5)
cnt++;
}
if (cnt >= (double)m * 0.5)
{
cout << "YES";
}
else
cout << "NO";
}
5.
Problem Generator
思路:
统计各个难度问题的个数是否大于m,若大于,则将m值储存在对应难度的数组变量中,反之,则储存原数量,最后用m乘以从‘A’到‘G’总共7个难度再减去对应题目个数,就是还需要出的题目数
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t,n,m;
cin >> t;
for (int i = 0; i < t; i++)
{
cin >> n >> m;
string a;
cin >> a;
int l = a.length();
int x[7] = { 0 };
for (int i = 0; i < l; i++)
{
if (a[i] == 'A')
{
if (x[0] < m)
x[0]++;
}
else if (a[i] == 'B')
{
if (x[1] < m)
x[1]++;
}
else if (a[i] == 'C')
{
if (x[2] < m)
x[2]++;
}
else if (a[i] == 'D')
{
if (x[3] < m)
x[3]++;
}
else if (a[i] == 'E')
{
if (x[4] < m)
x[4]++;
}
else if (a[i] == 'F')
{
if (x[5] < m)
x[5]++;
}
else if (a[i] == 'G')
{
if (x[6] < m)
x[6]++;
}
}
int c = m * 7;
for (int i = 0; i < 7; i++)
{
c -= x[i];
}
cout << c << endl;
}
}
6.
更好的交换
思路:
为了避免超时,用交换每行或列对应的行列标操作来替换直接对数组元素的交换,最后通过交换后得到的行列标位置,即可得出答案
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
int a[1001][1001],ans[1001][1001];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
int tx[1001],ty[1001];
for (int i = 1; i <= n; i++)
{
tx[i] = ty[i] = i;
}
for (int i = 1; i <= m; i++)
{
int op, x, y;
cin >> op >> x >> y;
if (op)
{
int t = tx[x];
tx[x] = tx[y];
tx[y] = t;
}
else
{
int t = ty[x];
ty[x] = ty[y];
ty[y] = t;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
ans[i][j] = a[tx[i]][ty[j]];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}
7.
Many Replacement
思路:
同样为了避免超时,选择只看最后得出的结果,中间过程经历变换后得到的临时结果是不重要的,因此记录26个字母在经历q次操作后变换成的字母,再在字符串中进行一次遍历变换,即可的到最后的答案
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n, q;
string s;
cin >> n;
cin >> s;
cin >> q;
char a[26];
char c[200000], d[200000];
for (int i = 0; i < q; i++)
{
cin >> c[i] >> d[i];
}
char o;
for (char i = 'a'; i <= 'z'; i++)
{
o = i;
for (int j = 0; j < q; j++)
{
if (o == c[j])
{
o = d[j];
}
}
a[i - 'a'] = o;
}
for (int i = 0; i < (int)s.size() ; i++)
{
cout << a[s[i]-'a'];
}
}