第一题:数字母
思路:用哈希表来写,需要注意的是输入的字符串是有空格的,应该用getline或者gets来输入
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<char> mp;
string s;
getline(cin, s);
for(int i = 0; i < s.size(); i ++)
if(s[i] >= 'a' && s[i] <= 'z')
mp.insert(s[i]);
cout << mp.size();
return 0;
}
第二题:玩游戏
思路:用一个bool数组来存储当前编号的小朋友的状态,然后进行模拟。
我深刻知道了写注释的好处,这题一开始就有点思路,但可能是有点混乱导致写了半天,后来加上注释思路就非常清楚了,而且要认真看题,这题的数据的输入我是有问题的,因为我原本写的是输入n个数据,其实应该输入k个数据,但是我不知道为什么是对的,后来看题解的时候发现我的数据输入有问题
我的代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n, k;
int a[N];
bool st[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(st, false, sizeof st); //st[i] == true 表示i已经淘汰
cin >> n >> k;
//for(int i = 1; i <= n; i ++) cin >> a[i];
for(int i = 1; i <= k; i ++) cin >> a[i];
int idx = 1; //idx用来存储每次的领头人
int m = n; //m用来存储当前的人数
int cnt = 0; //cnt用来统计淘汰的人数
for (int i = 1; i <= n; i ++){
int l = a[i] % m; //l表示从idx走到要淘汰人的步数
int j = 0; //j表示这次需走的长度
while(j < l){
//因为第一步是从领头人的右边开始的,所以应该相加在判断
idx = (idx + 1) % (n + 1);//当走到n了再从1开始走,因为是1-n,所以要模n + 1
if(!idx) idx = 1; //idx是从1开始的
if(!st[idx]) j ++;
}
st[idx] = true;
cout << idx << " ";
while(st[idx]){
idx = (idx + 1) % (n + 1);
if(!idx) idx = 1;
}
m --;
cnt ++;
if(cnt == k) break;
}
return 0;
}
大佬的代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
queue<int> q; //队列:队头删除,队尾插入
for(int i = 1; i <= n; i ++) q.push(i);
while(k --){
int a;
cin >> a;
a %= q.size(); //a表示从领头人开始要走的步数
while (a --){
q.push(q.front());
q.pop();
}
cout << q.front() << " ";
q.pop(); //下一个领头人是淘汰的人的下一个
}
return 0;
}
用队列来存储,这个确实牛,学到了
第三题:找回数组
思路:暴力模拟
哎~这题题目又没有看清楚,题目时a[i] = a[i - 1] + x[(i - 1) % k],我直接看成了a[i] = a[i - 1] + x[i - 1] % k,我感觉我看清楚题目了也写不出来,哈哈哈~
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1010;
int a[N];
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
vector<int> ans;
for(int k = 1; k <= n; k ++){
bool st = true;
for(int i = k + 1; i <= n; i ++){
if(a[i] - a[i - 1] != a[i - k] - a[i - k - 1])
{
st = false;
break;
}
}
if(st) ans.push_back(k);
}
cout << ans.size() << endl;
for(auto i : ans) cout << i << " ";
return 0;
}
总结:以后一定要写注解和看清楚题目再开始写,这都是血的教训。