0分,不知道怎么做/错在哪

P3955 [NOIP2017 普及组] 图书管理员

ZiruiLIU @ 2024-08-12 21:52:24

#include<bits/stdc++.h>
using namespace std;
int n, q, minn = 10000000, a[1010];
int  book[1010], need[1010];
bool x=0;
int jisuanweishu(int a, int b) {
    int s = 1, d = 0;
    for (int m = 1; m <= n; m++) {

        for (int i = 1; i <= a; i++) {
            d = book[m] / s % 10 + d;
            s = s * 10;
        }
        if (d == b) {
            minn = min(minn, book[m]) ;
             x=1;
        }
    }
    if (x==1) {
        return minn;
    } else {
        return -1;
    }

}
int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> book[i];
    }
    for (int i = 1; i <= q; i++) {
        cin >> a[i] >> need[i];
    }
    for (int i = 1; i <= n; i++) {
        cout << jisuanweishu(a[i], need[i]) << endl;
    }
    return 0;
}

测试点 怎么做啊;


by C0ns1st @ 2024-08-12 22:24:23

RE的话...是不是数组范围开小了?数组开大点试试

顺便,这码风尽量改改,比如函数尽量用英文名啥的(不是我一个蒟蒻怎么还提建议啊喂


by ZiruiLIU @ 2024-08-15 20:27:03

@C0ns1st

数组开大了也没用啊;

怎么办啊(发疯);


by abc20131113 @ 2024-08-19 11:45:07

@ZiruiLIU 我其实也是蒟蒻


by abc20131113 @ 2024-08-19 11:52:27

这是我代码
我才不告诉你是我看不出来问题


#include <bits/stdc++.h>  
using namespace std;  
const int maxn = 1e3+5;  
int a[maxn];  
int main(){  
    int n,m;  
    cin >> n >> m;  
    for (int i = 1;i <= n;i++){  
        cin >> a[i];  
    }  
    int x,y;  
    for (int i = 1;i <= m;i++){  
        int minn = INT_MAX;  
        cin >> x >> y;  
        for (int j = 1;j <= n;j++){  
            if ((a[j]-y)%(int)pow(10,x)==0 && (a[j]-y) >= 0){  
                minn = min(minn,a[j]);  
            }  
        }  
        if (minn == INT_MAX)  
            cout << -1 << endl;  
        else 
            cout << minn << endl;  
    }  
    return 0;  
}

by ZiruiLIU @ 2024-08-20 15:39:37

@yunxinrui 我才不会告诉你我根本看不懂


by abc20131113 @ 2024-08-20 19:45:00

@ZiruiLIU 啊


by theSonOfTheSun @ 2024-08-21 22:22:53

```cpp
#include<iostream>//不用你的库可以节省空间
using namespace std;
int  n,q,minn,a[1010];//多写条件少用极值
int  book[1010],need[1010];
bool x=0;
int jisuanweishu(int a, int b) {
    int s = 1, d = 0;
    for(int i=1;i<=a;i++){//下一个循环产生的d的值是倒序
        d=b/s%10+d*10;//d的上一个值未保存
        s=s*10;
    }
    b=d;d=0;//将b赋予d,d更改成倒序
  //将d赋予b,d设为0,达成b的倒序
  //b的值在其中被改变,不能直接用来当原数据
    for (int m=1;m<=n;m++) {
        s=1;d=0;//每轮使用需初始化
        for (int i = 1;i<=a;i++) {
            d=book[m]/s%10+d*10;//d的上一个值未保存
            s=s*10;
        }
        if (d == b) {
             if(minn>book[m]||x==0)minn=book[m];//x==0,没有值,无需比较,"||"是或,左右两侧只要一侧为真就返回真
             x=1;//不需要递归,小的正确答案将取代大的
        }
    }
    if (x==1) {
        return minn;
    } else {
        return -1;
    }
}
int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> book[i];
    }
    for (int i = 1; i <= q; i++) {
        cin >> a[i] >> need[i];
    }
    for (int i = 1; i <= q; i++) {//用q,不用n,按人的需要输出
        x=0;
        cout << jisuanweishu(a[i], need[i]) << endl;
    }
    return 0;
}

1.仅限dev-cpp(也能过)2.我的名字嚣张了一点,罪过罪过 3.怕棕名,没实验,题目给的数据试过,对了


by theSonOfTheSun @ 2024-08-21 22:28:15

@ZiruiLIU


by ZiruiLIU @ 2024-08-22 09:09:36

@theSonOfTheSun 你不说我都没注意到你的名字; 太阳之子;


by ZiruiLIU @ 2024-08-22 09:10:51


|