河西-7.25上午3教室-廖

· · 个人记录

T639266 T2

题意:给出L,R,求L-R之间2的数量

正确代码


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int l,r,cnt=0;
    cin>>l>>r;
    if(l>r)
    {
        swap(l,r);
    }
    for(int i=l;i<=r;i++)
    {
        string s=to_string(i);
        for(int j=0;j<s.size();j++)
        {
            if(s[j]=='2')
            {
                cnt++;
            }
        }
    }
    cout<<cnt;
    return 0;
}

错误代码


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int l,r,cnt=0;
    cin>>l>>r;
    for(int i=l;i<=r;i++)
    {
        string s=to_string(i);
        for(int j=0;j<s.size();j++)
        {
            if(s[j]=='2')
            {
                cnt++;
            }
        }
    }
    for(int i=r;i<=l;i++)
    {
        string s=to_string(i);
        for(int j=0;j<s.size();j++)
        {
            if(s[j]=='2')
            {
                cnt++;
            }
        }
    }
    cout<<cnt;
    return 0;
}

错误原因:错误代码中因为使用了两个循环,就导致输入的两个变量相等时,结果会统计两次。

T639268 T4

题意:给出 n 个数。求 n 个数是否可以满足前一项与后一项的绝对差即为1到 n。
正确代码

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[1005],b[1005];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(i>=2)
        {
            int s=abs(a[i]-a[i-1]);
            if(s>=1&&s<n)
            {
                b[s]=1;
            }
        }
    }
    for(int i=1;i<n;i++)
    {
        if(b[i]!=1)
        {
            cout<<"Not jolly";
            return 0;
        }
    }
    cout<<"Jolly";
    return 0;
}

错误代码:


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[1005],b[1005];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(i>=2)
        {
            int s=abs(a[i]-a[i-1]);
            if(s>=1&&s<n)
            {
                b[s]=1;
            }
        }
    }
    for(int i=1;i<n;i++)
    {
        if(b[i]!=1)
        {
            cout<<"Not jolly";
            return 0;
        }
    }
    cout<<"jolly";
    return 0;
}

错误出在 cout<<"jolly";

T7

题意:给出 n 条鱼求每条鱼是否可以按照一定顺序吃掉其他鱼,提升自己的重量,并且吃掉所有的鱼。(重量高的一条鱼可以吃掉重量低的一条鱼。)

正确代码


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
struct st
{
    int zhongliang,bianhao;
}s[500005];
int cmp(st s1,st s2)//重量排序
{
    return s1.zhongliang<s2.zhongliang;
}
int cmp2(st s1,st s2)//编号排序
{
    return s1.bianhao<s2.bianhao;
}
int n;
int cheak(int wz)
{
    ll dq=s[wz].zhongliang;//当前鱼的重量
    for(int i=1;i<=n;i++)
    {
        if(i==wz)//自己吃掉自己判断 
        {
            continue;
        }
        if(dq>s[i].zhongliang)//比别的鱼重
        {
            dq=dq+s[i].zhongliang;
        }
        else
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    //第一条能通关的鱼
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i].zhongliang;
        s[i].bianhao=i;
    }
    sort(s+1,s+1+n,cmp);
    ll l=0,r=n+1;
    s[n+1].zhongliang=INT_MAX;
    while(l+1<r)
    {
        ll mid=(l+r)/2;
        if( cheak(mid) == 1)
        {
            r=mid;
        }
        else
        {
            l=mid;
        }
    }
    //r为第一个能通关的鱼的位置
    int t=s[r].zhongliang;
    sort(s+1,s+1+n,cmp2);
    for(int i=1;i<=n;i++)
    {
        if(s[i].zhongliang>=t)
        {
            cout<<"T";
        }
        else
        {
            cout<<"N";
        }
    }
    return 0;
}

错误代码


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int a[500005],b[500005];
ll c[500005];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++)
    {
        c[i]=c[i-1]+b[i];
    }
    int l=0,r=n+1,mid;
    c[n+1]=LONG_LONG_MAX;
    while(l+1<r)
    {
        mid=(l+r)/2;
        if(c[mid]<b[mid+1])
        {
            l=mid;
        }
        else
        {
            r=mid;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(a[i]>=b[r]&&a[i]!=b[1])
        {
            cout<<"T";
        }
        else
        {
            cout<<"N";
        }
    }
    return 0;
}

错误原因:判断方面有较大的错误。同时错误使用前缀和数组存储每一个鱼的重量,导致8个点出错。

总结:

遇见较简单的题目时,不能掉以轻心,需要查看输入输出,否则就会因为输入输出丢分。编写代码时需要着重考虑极端情况。同时注意数据的大小。不要因为数组过大或数组过小RE/MLE。

§ 大吉 §