U325625题解

· · 算法·理论

这一题嘛,是一个简单的A+B 问题,但是时间和空间被极度限制。
第一反应是用快读快写

#include<bits/stdc++.h>
using namespace std;
void read(int &x){
    //注:此处请勿漏掉'&'
    x=0;int fl=1;
    char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') fl=-1,ch=getchar();
    while(ch<='9'&&ch>='0') 
        x=x*10+ch-'0',ch=getchar();
    x=x*fl;
}
void write(int x){
    if(x<0) x=-x,putchar('-');//输出负号
    static int sta[35];
    int top=0;
    do{
        sta[top++]=x%10;
        x/=10;
    }while(x);
    while(top) putchar('0'+sta[--top]);
    //数字从低位到高位入栈,再按顺序出栈
    //通过栈输出,可以降低复杂度
    return ;
}
int main(){
    int a,b;
    read(a),read(b);
    write(a+b);
    return 0;
}

最终结果一般,3ms。
后面出题人放宽了要求
暴力也可以过了
我的快读快写白写了/(ㄒoㄒ)/~~
下面将暴力代码一并放出

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d",a+b);
    return 0;
}