A题 解题思路:
A题,暴力做就行,k也就 50,如果 % 10 == 0 就除,如果 = 0 ,就减,最后输出即可。
#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300005;
typedef long long ll ;
int main(){
int n ,k;
scanf("%d%d",&n,&k);
while(k != 0){
if (n % 10 == 0){
n /= 10;
k --;
}
else{
int t = n % 10;
if (t >= k){
n -= k;
k = 0;
}
else{
n -= t;
k -= t;
}
}
}
printf("%d\n",n);
return 0;
}
给予一个字符串,然后求出现最多次数的2个字符,我们从头遍历就好,然后我们存储相邻的字符(2个字符),然后我们存入map中,最终我们遍历map,在其中求出最大值。处理好最后的细节就OK(for 循环中注意下在最后时break即可)
#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300;
typedef long long ll ;
map <string,int> ans;
int main(){
int n;
scanf("%d",&n);
string st;
cin>>st;
for (int i = 0; i < n ; i ++){
string s = "";
if (i == n - 1) break; // 这里记得break,不然最后i + 2 会溢出
for (int j = i ; j < i + 2; j ++){
s += st[j];
}
ans[s] += 1;
}
int mx = -1;
string ss ;
for (map <string,int> ::iterator it = ans.begin();it != ans.end();it++){
if (it->second > mx){
ss = it->first;
mx = it->second;
}
}
cout<<ss<<endl;
return 0;
}
C题水题,不过细节要注意下,求 一个 x,让他大于等于数组中的 k 个数。
- 注意 k 为 0 的时候,如果数组里的最小值为 1,那么肯定不存在(x的取值范围是1 — 1e9)
- 排序后我们判断 ans[k - 1] 和 ans[ k ] 是否相同,如果相同,那么肯定不能让他们小于等于x 的个数是 k 个。
#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const long long M = 1e9 + 7;
const int N = 300005;
typedef long long ll ;
int ans[N];
int main(){
int n,k;
scanf("%d%d",&n,&k);
for (int i = 0 ; i < n; i++){
scanf("%d",&ans[i]);
}
sort(ans,ans + n);
if (k == 0){
if (ans[0] == 1){
printf("-1");
}
else{
printf("1");
}
return 0;
}
if (k == n){
printf("%d",ans[n-1]);
return 0;
}
if (ans[k] == ans[k - 1]){
printf("-1");
}
else{
printf("%d",ans[k -1]);
}
return 0;
}