【笔试训练】day20

简介: 【笔试训练】day20

1.经此一役小红所向无敌

默认小红血量无限。直接计算出经过几轮攻击后,会出现人员伤亡。

对于对立来说他最多承受n轮光的攻击,对于光来说,他最多承受立得m轮攻击。

所以在经过min(n,m)轮回合之后,他们两个人至少死一个。活下来的人就自杀。

代码:

#include <iostream>
using namespace std;
typedef long long LL;
 
int main() {
    LL a,h,b,k;
    cin>>a>>h>>b>>k;
 
    LL cnt1=(h-1+b)/b;
    LL cnt2=(k-1+a)/a;
    LL cnt=min(cnt1,cnt2);
    LL ans=cnt*(a+b);
  //  cout<<cnt1<<" "<<cnt2<<endl;
    if(cnt1==cnt2){
        cout<<ans<<endl;
        return 0;
    }else if(cnt1>cnt2){
        ans+=a*10;
    }else{
        ans+=b*10;
    }
 
    cout<<ans<<endl;
   return 0;
}

2.连续子数组得最大和

遇到这种连续得区间题目,要么枚举左右端点,要么就是枚举右端点。

枚举每一个区间的右端点,f[i]表示以第i个元素结尾的区间的最大和是多少。

所以f[i]=max(f[i-1]+a[i],a[i])

哦对了这是一个sb题目,数组开1e5居然过不了。得开1e6。

代码:

#include <iostream>
#include<algorithm>
using namespace std;
const int N=1e6+100;
long long f[N];
 
int main() {
   int n;
   cin>>n;
    long long ans=-1e16;
   for(int i=1;i<=n;i++){
    cin>>f[i];
    f[i]=max(f[i-1]+f[i],f[i]);
    ans=max(f[i],ans);
   }
   cout<<ans<<endl;
  return 0;
}
// 64 位输出请用 printf("%lld")

3.非对称之美

思考这样一个事实:如果下标[l,r]是一个回文串,那么[l+1,r]一定不是回文串,除非里面元素全部相等。自己去推一下就好了。

代码:

#include <iostream>
#include<string>
using namespace std;
 
int main() {
   string str;
   cin>>str;
   if(str.size()<=1){
    cout<<0<<endl;
    return 0;
   }
   int l=0;
   int r=str.size()-1;
   
   while(l<r){
    if(str[l]==str[r]){
        l++;
        r--;
    }else{
        cout<<str.size()<<endl;
        return 0;
    }
   }
   l=1;
   r=str.size()-1;
   while(l<r){
     if(str[l]==str[r]){
        l++;
        r--;
    }else{
        cout<<str.size()-1<<endl;
        return 0;
    }
   }
    cout<<0<<endl;
   return 0;
}


相关文章
|
1月前
【笔试训练】day21
【笔试训练】day21
|
1月前
【笔试训练】day23
【笔试训练】day23
|
1月前
|
算法 Shell
【笔试训练】day12
【笔试训练】day12
|
1月前
【笔试训练】day16
【笔试训练】day16
|
1月前
|
并行计算
【笔试训练】day14
【笔试训练】day14
|
1月前
【笔试训练】day22
【笔试训练】day22
|
1月前
|
人工智能 BI
【笔试训练】day24-day48合集(2)
【笔试训练】day24-day48合集(2)
|
1月前
|
人工智能
【笔试训练】day2
【笔试训练】day2
|
17天前
笔试训练2
笔试训练2
|
1月前
【笔试训练】day15
【笔试训练】day15