定时比赛(题解)忘了发~

目录

找最长良序字符串

电子警察(police)

连续自然数之和

删数

幂字符串

01背包


找最长良序字符串

#include
using namespace std;
string s;
int c = 1, maxn, len;
int main()
{
    freopen("string.in", "r", stdin);
    freopen("string.out", "w", stdout);
    cin >> s;
    s = s + char('0'-1);
    for(int i = 0; i < s.size()-1; i++)
    {
        if(s[i] < s[i + 1])
        {
            c++;
        }
        else
        {
            maxn = max(maxn, c);

            c = 1;
        }
    }
    cout << maxn;
}

电子警察(police)

#include 
using namespace std;
int toTime(string s)
{
  return ((s[0]-'0')*10+s[1]-'0')*3600+((s[2]-'0')*10+s[3]-'0')*60+(s[4]-'0')*10+s[5]-'0';
}
int main()
{
  freopen("police.in","r",stdin);
  freopen("police.out","w",stdout);
  string str;
  int l,s,r,n,time;
  cin>>l>>s>>r>>n;
  while(n--)
  {
    cin>>str;
    time=toTime(str);
    time= time%(l+s+r)==0? (l+s+r) : time%(l+s+r);
    if(time<=l)
    {
      if(str[6]!='L')
      {
        cout<

连续自然数之和

#include
using namespace std;
long long l, r, m, s;
int main()
{
    cin >> m;
    l = 1, r = 2;
    //l不可能超过m/2,因为如果l,r都>m/2,则总和一定>m
    while(l <= m / 2 && r <= m)
    {
        s = (l + r) * (r - l + 1) / 2; //等差数列的区间和
        if(s == m)
        {
            cout << l << " " << r << endl;
            l++, r++;
        }
        else if(s < m) r++; //区间和太小,扩大
        else l++;//区间和太大,缩小
    }
    return 0;
}

删数

#include
using namespace std;
string s;
int n, cnt;
int main()
{
    cin >> s >> n;
    s += ' ';
    while (n--)
    {
        for (int i = 0; i < s.size() - 1; i++)
        {
            if (s[i] > s[i + 1])
            {
                s.erase(i, 1);
                break;
            }
        }
    }
    while (s[0] == '0' && s.size() > 1)
        s.erase(0, 1);
    cout << s;
    return 0;
}

幂字符串

#include
using namespace std;
string s,s1,str;
int cnt;
int main(){
    while(cin>>s&&s!="."){
        for(int i=0;i

01背包

#include
using namespace std;
int w[110], v[110], dp[110][20010]; // dp[i][j]:
int maxw, n;

int main()
{
    cin >> maxw >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> w[i] >> v[i];
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= maxw; j++)
        {
            if(j >= w[i])
            {
                dp[i][j] = max(v[i] + dp[i-1][j-w[i]],  dp[i-1][j]);
            }
            else
            {
                dp[i][j] = dp[i-1][j];
            }
        }
    }
    cout << dp[n][maxw];
    return 0;
}

你可能感兴趣的:(C++,知识点,作业,算法,c++,Mark1277,比赛)