【题目描述】
输入两个int型整数,输出它们的和。
这题很好的体现了一题多解。
普通解法:
#include<cstdio>
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
return 0;
}
使用Math库的神奇解法:
uses math;
var a:array[0..3] of float;
i,p:longint;
c,b:float;
s:ansistring;
begin
for i:=1 to 2 do read(a[i]);
sumsandsquares(a,b,c);
p:=trunc(b);
str(p,s);
for i:=1 to length(s) do write(s[i]);
end.
高精度加法:
var
i,l1,l2:longint;
a,b,c:array [1..502] of longint;
ch:ansistring;
begin
readln(ch);
l1:=length(ch);
for i:=1 to l1 do a[i]:=ord(ch[l1-i+1])-48;
readln(ch);
l2:=length(ch);
for i:=1 to l2 do b[i]:=ord(ch[l2-i+1])-48;
if l1<l2 then l1:=l2;
for i:=1 to l1 do begin
inc(c[i],a[i]+b[i]);
if c[i]>=10 then begin
dec(c[i],10);
inc(c[i+1]);
end;
end;
if c[l1+1]>0 then inc(l1);
for i:=l1 downto 1 do write(c[i]);
end.
神奇地结合位运算
#include <cstdio>
int m, n;
int main()
{
scanf("%d%d", &m, &n);
int u = m & n;
int v = m ^ n;
while (u) {
int s = v;
int t = u << 1;
u = s & t;
v = s ^ t;
}
printf("%d\n", v);
}
倍增(用Tarjan或ST表效率更高)搞LCA:
#include<cstdio>
#define NI 2
struct edge
{
int to,next,data;
}e[30];
int v[10],d[10],lca[10][NI+1],f[10][NI+1],tot=0;
void build(int x,int y,int z)
{
e[++tot].to=y; e[tot].data=z; e[tot].next=v[x]; v[x]=tot;
e[++tot].to=x; e[tot].data=z; e[tot].next=v[y]; v[y]=tot;
}
void dfs(int x)
{
for(int i=1;i<=NI;i++)
f[x][i]=f[x][i-1]+f[lca[x][i-1]][i-1],
lca[x][i]=lca[lca[x][i-1]][i-1];
for(int i=v[x];i;i=e[i].next)
{
int y=e[i].to;
if(lca[x][0]==y) continue;
lca[y][0]=x;
f[y][0]=e[i].data;
d[y]=d[x]+1;
dfs(y);
}
}
int ask(int x,int y)
{
if(d[x]<d[y]) {int t=x;x=y;y=t;}
int k=d[x]-d[y],ans=0;
for(int i=0;i<=NI;i++)
if(k&(1<<i))
ans+=f[x][i],
x=lca[x][i];
for(int i=NI;i>=0;i--)
if(lca[x][i]!=lca[y][i])
ans+=f[x][i]+f[y][i],
x=lca[x][i],y=lca[y][i];
return ans+f[x][0]+f[y][0];
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
build(1,2,a);
build(1,3,b);
dfs(1);
printf("%d",ask(2,3));
}
字典树:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node{
int str[26];
int sum;
}s[1000];
char str1[100];
int t=0,tot=0,ss=0;
bool f1;
void built()
{
t=0;
for(int i=0;i<strlen(str1);i++)
{
if(str1[i]=='-'){
f1=true;continue;
}
if(!s[t].str[str1[i]-'0'])
s[t].str[str1[i]-'0']=++tot;
t=s[t].str[str1[i]-'0'];
s[t].sum=str1[i]-'0';
}
}
int query()
{
int t=0;int s1=0;
for(int i=0;i<strlen(str1);i++)
{
if(str1[i]=='-') continue;
if(!s[t].str[str1[i]-'0']) return s1;
t=s[t].str[str1[i]-'0'];
s1=s1*10+s[t].sum;
}
return s1;
}
int main()
{
for(int i=1;i<=2;i++)
{
f1=false;
scanf("%s",str1);
built();
if(f1)
ss-=query();
else ss+=query();
}
printf("%d",ss);
return 0;
}
快读版:
#include <cstdio>
int read()
{
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return f*x;
}
int main()
{
printf("%d",read()+read());
return 0;
}
线段树:
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
struct node{
int val,l,r;
};
node t[5];
int a[5],f[5];
int n,m;
void init(){
for(int i=1;i<=2;i++){
scanf("%d",&a[i]);
}
}
void build(int l,int r,int node){
t[node].l=l;t[node].r=r;t[node].val=0;
if(l==r){
f[l]=node;
t[node].val=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,node*2);
build(mid+1,r,node*2+1);
t[node].val=t[node*2].val+t[node*2+1].val;
}
void update(int node){
if(node==1)return;
int fa=node>>1;
t[fa].val=t[fa*2].val+t[fa*2+1].val;
update(fa);
}
int find(int l,int r,int node){
if(t[node].l==l&&t[node].r==r){
return t[node].val;
}
int sum=0;
int lc=node*2;int rc=lc+1;
if(t[lc].r>=l){
if(t[lc].r>=r){
sum+=find(l,r,lc);
}
else{
sum+=find(l,t[lc].r,lc);
}
}
if(t[rc].l<=r){
if(t[rc].l<=l){
sum+=find(l,r,rc);
}
else{
sum+=find(t[rc].l,r,rc);
}
}
return sum;
}
int main(){
init();
build(1,2,1);
printf("%d",find(1,2,1));
}
Dijkstra+优先队列
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;
const int N=405;
struct Edge {
int v,w;
};
vector<Edge> edge[N*N];
int n;
int dis[N*N];
bool vis[N*N];
struct cmp {
bool operator()(int a,int b) {
return dis[a]>dis[b];
}
};
int Dijkstra(int start,int end)
{
priority_queue<int,vector<int>,cmp> dijQue;
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
dijQue.push(start);
dis[start]=0;
while(!dijQue.empty()) {
int u=dijQue.top();
dijQue.pop();
vis[u]=0;
if(u==end)
break;
for(int i=0; i<edge[u].size(); i++) {
int v=edge[u][i].v;
if(dis[v]==-1 || dis[v]>dis[u]+edge[u][i].w) {
dis[v]=dis[u]+edge[u][i].w;
if(!vis[v]) {
vis[v]=true;
dijQue.push(v);
}
}
}
}
return dis[end];
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
Edge Qpush;
Qpush.v=1;
Qpush.w=a;
edge[0].push_back(Qpush);
Qpush.v=2;
Qpush.w=b;
edge[1].push_back(Qpush);
printf("%d",Dijkstra(0,2));
return 0;
}
最小生成树:
#include <cstdio>
#include <algorithm>
#define INF 2140000000
using namespace std;
struct tree{int x,y,t;}a[10];
bool cmp(const tree&a,const tree&b){return a.t<b.t;}
int f[11],i,j,k,n,m,x,y,t,ans;
int root(int x){if (f[x]==x) return x;f[x]=root(f[x]);return f[x];}
int main(){
for (i=1;i<=10;i++) f[i]=i;
for (i=1;i<=2;i++){
scanf("%d",&a[i].t);
a[i].x=i+1;a[i].y=1;k++;
}
a[++k].x=1;a[k].y=3,a[k].t=INF;
sort(a+1,a+1+k,cmp);
for (i=1;i<=k;i++){
x=root(a[i].x);y=root(a[i].y);
if (x!=y) f[x]=y,ans+=a[i].t;
}
printf("%d\n",ans);
}
网络流:
#include<bits/stdc++.h>
using namespace std;
#define set(x) Set(x)
#define REP(i,j,k) for (int i=(j),_end_=(k);i<=_end_;++i)
#define DREP(i,j,k) for (int i=(j),_start_=(k);i>=_start_;--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()-1))
#define ALL(x) ((x).begin()+1),(x).end()
template<typename T> inline bool chkmin(T &a,const T &b){ return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a,const T &b){ return a < b ? a = b, 1 : 0; }
typedef long long LL;
typedef pair<int,int> node;
const int dmax=1010,oo=0x3f3f3f3f;
int n,m;
int a[dmax][dmax] , ans;
int d[dmax],e[dmax];
priority_queue <node> q;
inline bool operator >(node a,node b){ return a.y>b.y; }
bool p[dmax];
void Set(int x){ p[x]=1; }
void unset(int x){ p[x]=0; }
bool check(int x){ return x!=1 && x!=n && !p[x] && e[x]>0; }
void preflow(){
e[1]=oo;
d[1]=n-1;
q.push(mp(1,n-1));
set(1);
while (!q.empty()){
bool flag=1;
int k=q.top().x;
q.pop(),unset(k);
DREP(i,n,1)
if ((d[k]==d[i]+1 || k==1) && a[k][i]>0){
flag=0;
int t=min(a[k][i],e[k]);
e[k]-=t;
a[k][i]-=t;
e[i]+=t;
a[i][k]+=t;
if (check(i)){
q.push(mp(i,d[i]));
set(i);
}
if (e[k]==0) break;
}
if (flag){
d[k]=oo;
REP(i,1,n)
if (a[k][i]>0)
chkmin(d[k],d[i]+1);
}
if (check(k)){
q.push(mp(k,d[k]));
set(k);
}
}
ans=e[n];
}
int main(){
n = 2, m = 2;
int x, y;
scanf("%d%d", &x, &y);
a[1][2] += x + y;
preflow();
printf("%d\n",ans);
return 0;
}