跳到正文

题解

题解:P1001 A+B Problem

ouxiyao约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟
题目
P1001

版本

  1. v1当前

STL来也!

栈

C++
#include<bits/stdc++.h>

using namespace std;
stack<int>st;
int main(){
    int x,y;
    cin>>x>>y;
    st.push(x);
    st.push(y);
    int a,b;
    a = st.top();
    st.pop();
    b = st.top();
	cout<<a+b;
    return 0;
}

队列

C++
#include<bits/stdc++.h>

using namespace std;
queue<int>q;
int main(){
    int x,y;
    cin>>x>>y;
    q.push(x);
    q.push(y);
    int a,b;
    a = q.front();
    q.pop();
    b = q.front();
	cout<<a+b;
    return 0;
}

优先队列

C++
#include<bits/stdc++.h>

using namespace std;
priority_queue<int>q;
int main(){
    int x,y;
    cin>>x>>y;
    q.push(x);
    q.push(y);
    int a,b;
    a = q.top();
    q.pop();
    b = q.top();
	cout<<a+b;
    return 0;
}

可重集合

C++
#include<bits/stdc++.h>

using namespace std;
multiset<int>s;
int main(){
    int x,y;
    cin>>x>>y;
    s.insert(x);
    s.insert(y);
    int sum = 0;
    for(auto i:s)sum+=i;
    cout<<sum;
    return 0;
}

双端队列

C++
#include<bits/stdc++.h>

using namespace std;
deque<int>s;
int main(){
    int x,y;
    cin>>x>>y;
    s.push_back(x);
    s.push_back(y);
    int a,b;
    a = s.front();
    b = s.back();
    cout<<a+b;
    return 0;
}

动态数组

C++
#include<bits/stdc++.h>

using namespace std;
vector<int>s;
int main(){
    int x,y;
    cin>>x>>y;
    s.push_back(x);
    s.push_back(y);
    int sum = 0;
    for(auto i:s)sum+=i;
    cout<<sum;
    return 0;
}

不够整活?再来个快速幂:

C++
#include<bits/stdc++.h>

using namespace std;
int quickpow(int x,int y){
	int ans = 1,s = x,mod = 2147483647;
	while(y){
		if(y&1)ans*=s,ans%=mod;
		s = s*s%mod;
		y>>=1;
	}
	return ans;
}
int main(){
    int x,y;
    cin>>x>>y;
    cout<<quickpow(x,1)+quickpow(y,1);
    return 0;
}

再来一个双向循环链表!

C++
#include <iostream>
using namespace std;
struct node{
    int data;
    node *pre,*nxt;
}*head,*q,*r,*t;
int main(){
    head = new node;
    head->nxt = head;
    head->pre = head;
    head->data = 0;
    q = head;
    int m,s,n;
    m = 2;
    for(int i = 1;i<=m;i++){
        t = new node;
        cin>>s;
        t->data = s;
        q->nxt->pre = t;
        t->nxt = q->nxt;
        q->nxt = t;
        t->pre = q;
        q = q->nxt;
    }
    int sum = 0;
    q = head;
    q = q->pre;
    for(int i = 0;i<=m;i++){
        q = q->nxt;
        sum+=q->data;
    }
    cout<<sum;
    return 0;
}

还有私信。

原文