【LGR-(-17)】洛谷入门赛 #8 题解

· · 个人记录

第一次AK,激动所以来写个题解合集

A Hello, 2023

由于输入的数可能是负数,由题我们就可以每次把他加上 2023 ,直到这个数变成正数我们在取余。

code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long n;
    cin>>n;
    while(n<0)n+=2023;
    cout<<n%2023;
}

B 铺地毯

如果先乘后除可能会爆掉,我们可以分开来算。

我的第一感觉就是分开算可能的小学算什么多少个正方形算多了。

如果地毯的不能正好整除,就输出 -1 。

code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long a,b,c;
    cin>>a>>b>>c;
    if(a%c||b%c)cout<<"-1";
    else cout<<a/c*(b/c);
}

C 一次函数

这个就直接按题目模拟就行了吧。

根据 y=k\times x+b 判断即可。

code:

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

signed main()
{
    int n,k,b,c=0;
    cin>>n>>k>>b;
    for(int i=1;i<=n;i++){
        int x,y;
        cin>>x>>y;
        if(y==k*x+b)c++;
    }
    cout<<c;
}

D 就要 62

如果直接能被 62 整除,我们就直接输出了。

如果不能,我们逐位判断,但是我们是从各位开始,所以先遇到 2 ,紧接着再遇到 6 ,我们就输出 Yes ,否则输出 No

注:注意大小写。

code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long n;
    cin>>n;
    bool ok=0;
    if(n%62==0){
        cout<<"Yes";
        return 0;
    }
    else{
        while(n){
            if(n%10==2)ok=1;
            else if(n%10!=6&&n%10!=2)ok=0;
            else if(n%10==6&&ok){
                cout<<"Yes";
                return 0;
            }
            n/=10;
        }
    }
    cout<<"No";
}

E 九九乘方表

和打印乘法表没啥区别,把 \times 变成 pow 就行了。

貌似不爆 int 。

code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cout<<i<<" ^ "<<j<<" = "<<(long long)(pow(i,j))<<' ';
        }
        puts("");
    }
}

F 避雷针

【】【】【】怎么卡 map

每次输入看看劈到的地方是否越界,不越界就把这个点置为 1 ,最后输出 n 个点中值为 1 的个数即可。

code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int mp[1000001];
signed main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int x;
        cin>>x;
        if(x-2>0)mp[x-2]=1;
        if(x-1>0)mp[x-1]=1;
        mp[x]=1;
        if(x+2<=n)mp[x+2]=1;
        if(x+1<=n)mp[x+1]=1;
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        if(mp[i])ans++;
    cout<<ans;
}

G 华小科的旅行开始了

题目™是什么意思

大意: 给你 m,n,x,y ,然后给你 n 行,每行 m 数字,第 i 行第 j 对数字由 a[i][j],b[i][j] 构成。现在让你从 (x,y) 开始,每次输出 (x,y) 并令 x' = a[x][y] , y' = b[x][y] 。如果 x = 0y = 0 就退出。

感谢@yemuzhe_提供的大意。

了解大意后模拟就行了。

注:读题要仔细。

code:

#include<bits/stdc++.h>
using namespace std;
int a[1001][1001],b[1001][1001];
void dfs(int x,int y)
{
    if(x==0&&y==0)return;
    cout<<x<<' '<<y<<'\n';
    int X=x,Y=y;
    x=a[X][Y];
    y=b[X][Y];
    dfs(x,y);
}
int main()
{
    int n,m,x,y;
    cin>>m>>n>>x>>y;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)cin>>a[i][j]>>b[i][j];
    }
    dfs(x,y);
}

值得一提的是,这不是 dfs 。

H 新年快乐

简单模拟。

每次读取字符串中的 lr 位(下标从 1 ),然后求出上一个字符串。

如果都是 a ,说明没有上一个字符串,那么就是 NULL

如果不是全是 a ,而是后面有一部分 a ,你们我们把第一个不是 a 的字符 -1 ,而这个字符后的所有字符都为 z

可能描述不清,可以看代码自行理解:

int x=s.size()-1;
while(s[x]=='a')s[x]='z',x--;
s[x]=char(s[x])-1;
cout<<s<<'\n';

然后我们看字符串里有没有这个字符串即可。

code:

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

signed main()
{
    string st;
    cin>>st;
    st=' '+st;
    int n;
    cin>>n;
    while(n--){
        int l,r;
        cin>>l>>r;
        string s;
        for(int i=l;i<=r;i++)s+=st[i];
        bool ok=0;
        for(int i=0;i<s.size();i++)
            if(s[i]!='a')ok=1;
        if(!ok)cout<<"NULL\n";
        else {
            int x=s.size()-1;
            while(s[x]=='a')s[x]='z',x--;
            s[x]=char(s[x])-1;
            cout<<s<<'\n';
        }
        bool o=0;
        for(int i=1;i<st.size()-s.size()+1;i++){//注:要-s.size()+1,因为后面达不到s的长度就没意义了。
            //cout<<st.substr(i,s.size())<<'\n';
            if(st.substr(i,s.size())==s){//i到i+s.size()位的字符串
                o=1;
                break;
            }
        }
        cout<<(o&&ok?"Happy New Year!":"Happy Chinese New Year!")<<'\n';
    }
}

I HACK IT!

问题一:爆 int 。

hack样例:

2000000000 2000000000

问题二:超时。

strlen接近线性。

hack样例:

1e6个a

注:不是输入汉字。

当然如果你不想直接一个一个写的话,可以用文件操作。

问题三:数组越界。

a[99] 时如果再 +1 就越界了。

hack样例:

100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

310行结束~

感谢观看。