寒假测试2

https://vjudge.net/problem/CodeForces-977A/origin

题意:如果数字的最后一位不为零,该数减1;如果数字的最后一位是0,去掉最后一位数字,即除10。给定一个整数n。求执行k次操作后的数。(易)

Input
512 4
Output
50
Input
1000000000 9
Output
1
#include<iostream>
using namespace std;

int main()
{
    
    
    int n,k;
    cin>>n>>k;
    while(k--)
    {
    
    
        if(n%10==0)
            n/=10;
        else n--;
    }
    cout<<n<<endl;
    return 0;
}

https://vjudge.net/problem/CodeForces-977B/origin

题意:从一串字符串中选出出现次数最多的两个连续的字符,并输出;(易)

思路:每个连续的两个字符计数,找最大的一个。

Input
7
ABACABA
Output
AB
Input
5
ZZZAA
Output
ZZ
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string,int> m;
int main()
{
    
    
    int n,x=0;
    cin>>n;
    string a,b,ans;
    cin>>a;
    for(int i=0; i<n; i++)
    {
    
    
        b=a.substr(i,2);
        m[b]++;
        if(m[b]>x)
        {
    
    
            x=m[b];
            ans=b;
        }
    }
    cout<<ans<<endl;
    return 0;
}

https://vjudge.net/problem/CodeForces-977C/origin

题意:给定一个n个数字组成的序列,问是否存在一个整数x(>=1),使得该序列刚好有k个数小于或等于x,存在即输出该x,否则输出-1。(偏易)

思路:先对数组从小到大排序若k=0时,最小值如果小于等于1,不存在x,否则x=1满足所以情况。若k不为0,判断第k个数与k+1的数是否相等,若相等则不存在,否则第k个数即为x。若k等于n,最大的数即为x。

Input
7 4
3 7 5 1 10 3 20
Output
6
Input
7 2
3 7 5 1 10 3 20
Output
-1
#include<iostream>
#include<algorithm>
using namespace std;
long long a[2000008];
int main()
{
    
    
  int n,k,ans=0,temp;
  cin>>n>>k;
  for(int i=1;i<=n;i++)
      cin>>a[i];
  sort(a+1,a+1+n);
  if(k==0)
  {
    
    
      if(a[1]>1)
          cout<<1<<endl;
      else
          cout<<-1<<endl;
      return 0;
  }
  if((a[k]!=a[k+1]&&k<n)||n==k)
      cout<<a[k]<<endl;
  else cout<<-1<<endl;
  return 0;
}

https://vjudge.net/problem/CodeForces-999A/origin

题意:给出 n 个问题的难度以及可以解决问题的能力 k,每次只能从问题列表的左边或右边解决问题,如果问题难度高于 k,其后或其前的问题就无法解决,求最多可以解决多少问题。(易)

思路:左右都判断是否能解决,如果能解决的个数等于总问题的两倍,答案为n,否则直接输出解决问题数。

Input
8 4
4 2 3 1 5 1 6 4
Output
5
Input
5 2
3 1 2 1 3
Output
0
Input
5 100
12 34 55 43 21
Output
5
#include<iostream>
using namespace std;
int a[105];
int main()
{
    
    
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    int c=0;
    for(int i=1; i<=n; i++)
    {
    
    
        if(a[i]<=k)
            c++;
        else
            break;
    }
    for(int i=n; i>=1; i--)
    {
    
    
        if(a[i]<=k)
            c++;
        else
            break;
    }
    if(c==2*n)
        cout<<n<<endl;
    else
        cout<<c<<endl;
    return 0;
}

https://vjudge.net/problem/CodeForces-999B/origin

题意:给你一个字符串,字符串按照字符串个数n的因子d,将子串s[1…d]倒过来,如"codeforces" → (10)“secrofedoc” → (5)“orcesfedoc” → (2)“rocesfedoc” → (1)“rocesfedoc” ,你的任务是将其还原。(偏易)

思路:从2开始判断n的因子,然后交换,注意交换序号和范围。

Input
10
rocesfedoc
Output
codeforces
Input
16
plmaetwoxesisiht
Output
thisisexampletwo
Input
1
z
Output
z
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
    
    
    int n,i,j;
    string s;
    cin>>n>>s;
    for(i=2;i<=n;i++)
    {
    
    
      if(n%i==0)
      {
    
    
         for(j=0;j<i/2;j++)
         {
    
    
           swap(s[j],s[i-j-1]);
         }
      }
    }
    cout<<s<<"\n";
}

https://vjudge.net/problem/CodeForces-999C/origin

题意:给你一个字符串,要去掉k个字符,删除的规则是每次从左边按字典序删除一个字符,比如有a就删除a,没有就删除b,还没有继续往下删除直到把k个字符删除完。(一般)

思路:先将字符串按字典序排序,用map计下要删除的字符的个数,然后再选择的输出字符。

Input
15 3
cccaabababaccbc
Output
cccbbabaccbc
Input
15 9
cccaabababaccbc
Output
cccccc
Input
1 1
u
Output
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
map<char,int>m;
int main()
{
    
    
    int n,k;
    string s,a;
    cin>>n>>k>>s;
    a=s;
    sort(a.begin(),a.end());
    for(int i=0; i<k; i++)
        m[a[i]]++;
    for(int i=0; i<n; i++)
        if(m[s[i]]) m[s[i]]--;
        else cout<<s[i];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51443397/article/details/114475445
今日推荐