目录
T1:计算摄氏温度
思路:
一道签到题
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int c, f=100;
c = 5 * (f - 32) / 9;
cout << "fahr = 100, celsius = " << c << endl;
}
T2:查验身份证
思路:
一道模拟题,可以用数组简化,具体实现看代码
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
char b[] = { '1','0','X','9','8','7','6','5','4','3','2' };
int n; cin >> n;
bool flag = 1;
while (n--)
{
string s; cin >> s;
long long ans = 0;
for (int i = 0; i < s.size() - 1; i++)
{
ans += (s[i] - '0') * a[i];
}
ans %= 11;
if (b[ans] != s[s.size() - 1])
{
flag = 0;
cout << s << endl;
}
}
if (flag)cout << "All passed" << endl;
return 0;
}
T3:帅到没朋友
思路:
主要是注意细节,一是假设k等于1,虽然此人不需要进行处理但是还需要有cin的操作,不能直接continue,要不然会运行超时,二是要注意如果用的是数组储存,对于00001,它只会输出1,故需要人为进行补全。
代码:
#include<bits/stdc++.h>
using namespace std;
int mp[1000005];
bool vis[1000005];
int main()
{
int t; cin >> t;
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
while (t--)
{
int n; cin >> n;
if (n == 1)
{
int x; cin >> x;
}
else
{
for (int i = 1; i <= n; i++)
{
int x; cin >> x;
mp[x]++;
}
}
}
cin >> t;
bool flag = 1, first = 1;
while (t--)
{
int x; cin >> x;
if (mp[x] == 0 && !vis[x])
{
flag = 0;
if (first)
{
string s;
s = to_string(x);
while (s.size() < 5)
{
s = '0' + s;
}
cout << s;
first = 0;
}
else
{
string s;
s = to_string(x);
while (s.size()<5)
{
s = '0' + s;
}
cout << " " << s;
}
vis[x] = 1;
}
}
if (flag)
{
cout << "No one is handsome" << endl;
}
else
{
cout << endl;
}
return 0;
}
T4:输出GPLT
思路:
运用map对各个字符的个数进行存储,同时与前面第二题类似运用数组简化操作,还可以先求最多可以有多少个完整的GPLT来缩短运行时间。
代码:
#include<bits/stdc++.h>
using namespace std;
map<char,int> mp;
int main()
{
string s; cin >> s;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == 'p' || s[i] == 'P')
{
mp['P']++;
}
else if(s[i] == 'g' || s[i] == 'G')
{
mp['G']++;
}
else if (s[i] == 'l' || s[i] == 'L')
{
mp['L']++;
}
else if (s[i] == 't' || s[i] == 'T')
{
mp['T']++;
}
}
int n = min(mp['P'], mp['G']);
int m = min(mp['L'], mp['T']);
int minn = min(n, m);
for (int i = 1; i <= minn; i++)
{
cout << "GPLT";
}
char c[] = { 'G','P','L' ,'T'};
int now=0;
mp['P'] -= minn, mp['G'] -= minn, mp['L'] -= minn, mp['T'] -= minn;
while (mp['P'] != 0 || mp['G'] != 0 || mp['L'] != 0 || mp['T'] != 0)
{
if (mp[c[now]] != 0)
{
cout << c[now];
mp[c[now]]--;
}
now++;
now %= 4;
}
return 0;
}
T5:判断素数
思路:
主要是注意对1,2的特判
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
long long a;
cin >> a;
if (a==2)
{
cout << "Yes" << endl;
continue;
}
if (a == 1)
{
cout << "No" << endl;
continue;
}
bool flag = 1;
for (int i = 2; i <= sqrt(a); i++)
{
if (a % i == 0)
{
flag = 0;
break;
}
}
if (flag)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
return 0;
}
T7:最佳情侣身高差
思路:
简单模拟
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
char c; cin >> c;
double H; cin >> H;
double ans;
if (c == 'M')
{
ans = H / 1.09;
}
else if(c=='F')
{
ans = H * 1.09;
}
cout << fixed << setprecision(2) << ans << endl;
}
return 0;
}
T7:连续因子
思路:
主要是找到它的每一个因子,然后再判断每个因子可以具有的最大连续且均为所求数的因子的连续数,找到最大连续数后再记录其起点位置,如果其为素数,则其最大连续因子数为1,且输出最大连续因子为1.
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n; cin >> n;
int start = 0, longs = 0, res = 0;
for (int i = 2; i <= sqrt(n); i++)
{
int temp = n;
int cnt = 0;
for (int j = i; temp % j == 0 && temp; j++)
{
temp /= j;
cnt++;
}
if (res < cnt)
{
res = cnt;
longs = i;
}
}
if (res == 0)
{
cout << "1" << endl;
cout << n << endl;
}
else
{
cout << res << endl;
int now = longs;
while (now < longs + res)
{
if (now == longs)
{
cout << now;
}
else
{
cout << "*" << now;
}
now++;
if (now == longs + res)
{
cout << endl;
}
}
}
return 0;
}
T8:出生年
思路:
可以利用map来存各个数字,如果有那个数字则该数字++,最后利用map.size()来判断有多少个不同的数字
代码:
#include<bits/stdc++.h>
using namespace std;
int a[5];
int main()
{
string s; cin >> s;
int n; cin >> n;
while (s.size()<4)
{
s = '0' + s;
}
for (int i = 1; i <= 4; i++)
{
a[i] = s[i - 1]-'0';
}
int cnt = 0;
while (true)
{
map<int , int >mp;
mp[a[1]]++;
mp[a[2]]++;
mp[a[3]]++;
mp[a[4]]++;
if (mp.size() == n)
{
break;
}
a[4]++;
cnt++;
if (a[4] == 10)
{
a[4] -= 10;
a[3]++;
}
if (a[3] == 10)
{
a[3] -= 10;
a[2]++;
}
if (a[2] == 10)
{
a[2] -= 10;
a[1]++;
}
}
cout << cnt << " ";
for (int i = 1; i <= 4; i++)
{
cout << a[i];
}
}
T9:红色警报
思路:
主要是并查集的运用,首先可以跑一遍并查集,查看有多少个联通块,后续每失去一个城市就跑一遍并查集,如果相较于之前的联通块的数量增加,则说明这个城市的失去会导致国家的分裂,则会有红色警报,同时及更新目前的联通块个数,同时要注意细节处理如果k==n,则在最后要输入Game Over
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5050;
int x[maxn], y[maxn], vis[maxn];
int fa[maxn];
void init(int n) { for (int i = 0; i < n; i++)fa[i] = i; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) { x = find(x); y = find(y); if (x != y)fa[x] = y; }
int count(int n) {//统计并查集集合个数
int cnt = 0;
for (int i = 0; i < n; i++)//节点从0-n编号
if (fa[i] == i)cnt++;
return cnt;
}
int main() {
int n, m;
cin >> n >> m;
init(n);
for (int i = 1; i <= m; i++) {
cin >> x[i] >> y[i];
merge(x[i], y[i]);
}
int k; cin >> k;
int cc1 = count(n), cc2;
for (int i = 1; i <= k; i++) {
int t; cin >> t;
vis[t] = 1;//被攻占的不再联通
init(n);
for (int j = 1; j <= m; j++) {
if (vis[x[j]] || vis[y[j]])continue;
merge(x[j], y[j]);
}
cc2 = count(n);
if (cc2 == cc1 || cc2 == cc1 + 1)
printf("City %d is lost.\n", t);
else
printf("Red Alert: City %d is lost!\n", t);
cc1 = cc2;
}
if (k == n)printf("Game Over.\n");
return 0;
}
T10:秀恩爱分得快
思路:
是一道大型模拟题,主要是对人性别的储存是一个难点,如果既有正又有负数则后续遍历将会有困难,并且如果直接储存数字的话则+0和-0无法区分,故采取用sx[]数组来储存性别,然后再将编号储存到另外一个数组中,后续遍历求最大亲密度时便方便了,同时对与同一性别我们不用计算他们的亲密度,来减少运行时间,防止超时
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, k, kk, xb[N], a[505], p, x, y;
double g[N][N];
string ct[N], t;
int toi(string s) {//将字符串转化为正整数
int x = 0;
if (s[0] == '-')for (int i = 1; i < s.size(); ++i)x *= 10, x += s[i] - '0';
else for (int i = 0; i < s.size(); ++i)x *= 10, x += s[i] - '0';
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
while (m--) {
cin >> k; kk = k; p = 0;
while (kk--)
{
cin >> t; int tn = toi(t);
if (t[0] == '-')xb[tn] = 1;
else xb[tn] = 2;
a[p++] = tn;
}
double k1 = 1.0 / k;
for (int i = 0; i < k - 1; ++i) {
for (int j = i + 1; j < k; ++j) {
if (xb[a[i]] == xb[a[j]])continue;
g[a[i]][a[j]] += k1; g[a[j]][a[i]] = g[a[i]][a[j]];
}
}
}
cin >> t; x = toi(t);
if (t[0] == '-')xb[x] = 1; else xb[x] = 2;
cin >> t; y = toi(t);
if (t[0] == '-')xb[y] = 1; else xb[y] = 2;
for (int i = 0; i < n; ++i)if (xb[i] == 1)ct[i] = "-"; else ct[i] = "";//ct[i]存储编号i的人的性别符合,方便输出
double ma = 0, mb = 0;//x,y的最大亲密度
for (int i = 0; i < n; ++i)if (xb[i] != xb[x])ma = max(ma, g[x][i]);
for (int i = 0; i < n; ++i)if (xb[i] != xb[y])mb = max(mb, g[y][i]);
if (g[y][x]=mb && g[x][y] == ma)cout << ct[x] << x << " " << ct[y] << y << endl;
else {
for (int i = 0; i < n; ++i)if (g[x][i] == ma && xb[x] != xb[i])cout << ct[x] << x << " " << ct[i] << i << endl;
for (int i = 0; i < n; ++i)if (g[y][i] == mb && xb[y] != xb[i])cout << ct[y] << y << " " << ct[i] << i << endl;
}
return 0;
}
T11:插松枝
思路:
考察vector,stack,queue的综合运用,一道大型模拟题,注意细节就好了
代码:
#include<bits/stdc++.h>
using namespace std;
stack<int >S;
queue<int >Q;
vector<int >V[2000];
int main()
{
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
int x; cin >> x;
Q.push(x);
}
int now = 1;
while (!Q.empty()||!S.empty())
{
if (V[now].empty())
{
if (S.empty())
{
V[now].push_back(Q.front());
Q.pop();
}
else
{
V[now].push_back(S.top());
S.pop();
}
}
else
{
if (S.empty())
{
if (V[now].back() >= Q.front())
{
V[now].push_back(Q.front());
Q.pop();
if (V[now].size() >= k)
{
now++;
}
}
else
{
S.push(Q.front());
Q.pop();
}
}
else
{
if (V[now].back() >= S.top())
{
V[now].push_back(S.top());
S.pop();
if (V[now].size() >= k)
{
now++;
}
}
else if(!Q.empty()&&V[now].back() >= Q.front())
{
V[now].push_back(Q.front());
Q.pop();
if (V[now].size() >= k)
{
now++;
}
}
else
{
if (S.size() == m)
{
now++;
}
else
{
if (!Q.empty())
{
S.push(Q.front());
Q.pop();
}
else
{
now++;
}
}
}
}
}
}
for (int i = 1; i <= now; i++)
{
if (V[i].size() == 0)continue;
for (int j = 0; j < V[i].size(); j++)
{
if (j == 0)
{
cout << V[i][j];
}
else
{
cout <<" " << V[i][j];
}
}
cout << endl;
}
return 0;
}
T12:哲哲打游戏
思路:
根据题意走一遍模拟就好了
代码:
#include<bits/stdc++.h>
using namespace std;
vector<int >V[105000];
int Q[105000];
int main()
{
int n,m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
int a; cin >> a;
for (int j = 0; j <a; j++)
{
int x; cin >> x;
V[i].push_back(x);
}
}
int now = 1;
while (m--)
{
int x,y; cin >> x>>y;
if (x == 1)
{
Q[y] = now;
cout << now << endl;
}
else if (x==0)
{
now = V[now][y - 1];
}
else if(x==2)
{
now = Q[y];
}
}
cout << now ;
return 0;
}