2022csp-j复赛试题及答案

发布时间 2023-09-07 09:12:51作者: 关于42号星球

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main() {
 5     int a,b;
 6     cin>>a>>b;
 7     long long ans=1;//注意long long ,不能用int
 8     for(int i=1; i<=b; i++) {
 9         ans*=a;
10         if(ans>1e9) {
11             cout<<-1;
12             return 0;
13         }
14     }
15     cout<<ans;
16     return 0;
17 }

//60分代码
#include <iostream>
using namespace std;
long long n,e,d;
int k;

int main() {
    scanf("%d",&k);
    while(k--) {
        scanf("%lld %lld %lld",&n,&e,&d);
        int f=0;
        for(long long  i=1; i*i<=n; i++) {
            if(n%i==0) { //找到了能够整除的数
                if((i-1)*(n/i-1)+1==e*d) {
                    f=1;
                    printf("%lld %lld\n",i,n/i);
                    break;
                }
            }
        }
        if(!f) printf("NO\n");
    }
    return 0;
}
 1 #include <iostream>//满分代码
 2 #include<cmath>
 3 using namespace std;
 4 long long n,e,d,p,q,t,m;
 5 int k;
 6 
 7 int main() {
 8     scanf("%d",&k);
 9     while(k--) {
10         scanf("%lld %lld %lld",&n,&e,&d);
11         m=n-e*d+2;//p+q=m,p*q=n
12         t=sqrt(m*m-4*n);//q-p=t
13         q=(m+t)/2;
14         p=m-q;
15         if(p*q==n&&e*d==(p-1)*(q-1)+1) printf("%lld %lld\n",p,q);
16         else printf("NO\n");
17     }
18     return 0;
19 }

 1 //P8816 [CSP-J 2022] 上升点列
 2 #include <iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,k;
 7 struct points {
 8     int x,y;
 9 } p[510];
10 
11 bool cmp(const points a,const points b) {
12     if(a.x==b.x) return a.y<b.y;
13     else return a.x<b.x;
14 }
15 int f[510][110];//以i点为终点,再添加上j个点的情况下最大序列长度
16 int main() {
17     scanf("%d %d",&n,&k);
18     for(int i=1; i<=n; i++) {
19         scanf("%d %d",&p[i].x,&p[i].y);
20     }
21     sort(p+1,p+1+n,cmp);
22     int ans=0;
23     for(int i=1; i<=n; i++) {//以i为终点
24         for(int j=0; j<=k; j++) {//添加j个点
25             f[i][j]=j+1;//以i为终点,后续添加j个点最短的长度 
26             for(int l=1; l<i; l++) { //l为i的前一个点
27                 if(p[l].x<=p[i].x&&p[l].y<=p[i].y) {//横坐标、纵坐标值均单调不减
28                     int t=p[i].x-p[l].x+p[i].y-p[l].y-1;//需要添加的点数量
29                     if(j>=t) f[i][j]=max(f[i][j],f[l][j-t]+t+1);
30                     //cout<<p[i].x<<p[l].x<<p[i].y<<p[l].y<<t<<endl;
31                 }
32                 //cout<<"i="<<i<<" j="<<j<<" l="<<l<<" f="<<f[i][j]<<endl;
33             }
34 
35             ans=max(ans,f[i][j]);
36         }
37     }
38     printf("%d",ans);
39     return 0;
40 }

 1 //P8815 [CSP-J 2022] 逻辑表达式
 2 #include <iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stack>
 6 using namespace std;
 7 char s[1000010];
 8 struct number {
 9     int num;
10     int op1,op2; //&,|
11 };
12 stack<number> n;//数字
13 stack<char> f;
14 int l;
15 void cul() {
16     number n2=n.top();//操作数1
17     n.pop();
18     number n1=n.top();//操作数2
19     n.pop();
20     char c=f.top();
21     f.pop();
22 
23     int ans;
24     if(c=='&') {
25         ans=n1.num&n2.num;
26         if(n1.num==0) n.push({ans,n1.op1+1,n1.op2});//通过&操作 ,且短路
27         else n.push({ans,n1.op1+n2.op1,n1.op2+n2.op2});
28     } else {
29         ans=n1.num|n2.num;
30         if(n1.num==1) n.push({ans,n1.op1,n1.op2+1});//通过|操作 ,且短路
31         else n.push({ans,n1.op1+n2.op1,n1.op2+n2.op2});
32     }
33     //cout<<"cul"<<n1.num<<c<<n2.num<<'='<<ans<<" "<<n.top().op1<<' '<<n.top().op2<<endl;
34     return;
35 }
36 void work() {
37     for(int i=0; i<l; i++) {
38         if(s[i]=='0'||s[i]=='1') {
39             n.push({s[i]-'0',0,0});
40         }//数字进栈
41         else if(s[i]=='(') {
42             f.push(s[i]);
43         } else if(s[i]=='&')    {
44             while(!f.empty()&&f.top()=='&') {
45                 cul();
46             }
47             f.push(s[i]);
48         } else if(s[i]=='|') {
49             while(!f.empty()&&(f.top()=='|'||f.top()=='&')) {
50                 cul();
51             }
52             f.push(s[i]);
53         } else if(s[i]==')') {
54             while(!f.empty()&&f.top()!='(') {
55                 cul();
56             }
57             f.pop();
58         }
59     }
60     while(!f.empty()) {
61         cul();
62     }
63 }
64 
65 int main() {
66     cin>>s;
67     l=strlen(s);
68     work();
69     number t=n.top();
70     cout<<t.num<<endl<<t.op1<<' '<<t.op2;
71     return 0;
72 }