今天简单刷了几道NOIP2014的题,今天发代码。
————————————————————————————————————————————————————————————————————————-—
【NOIP201401-珠心算测验】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b); int main() { int n,i,a[103]={0}; int ans=0; int j,k,flag; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } qsort(a,n,sizeof(a[0]),cmp); for(i=0;i<n;i++) { flag=0; for(j=0;j<n&&(a[j]<=a[i]);j++) { for(k=j+1;k<n&&(a[j]+a[k]<=a[i]);k++) { if(a[i]==a[j]+a[k]) { ans++; flag=1; break; } } if(flag==1) break; } } printf("%d ",ans); return 0; } int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; }
【NOIP201402-比例化简】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> using namespace std; int gcd(int x,int y) { if(y==0)return x; else return gcd(y,x%y); } int main() { int i,j,k; int a,b,l,a1,b1; double k1,k2,k3; cin>>a>>b>>l; k1=a*1.0/b; k3=l*1.0; for(i=1;i<=l;i++) for(j=1;j<=l;j++) if(gcd(i,j)==1) { k2=i*1.0/j; if(k2>=k1 && k2-k1<k3) { a1=i;b1=j; k3=k2-k1; } } cout<<a1<<" "<<b1; return 0; }
【NOIP201403-螺旋矩阵】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> using namespace std; int main(int argc, char *argv[]) { int n,a,b,i,j,q=0,sum=1,t,l; cin>>n>>a>>b; for(i=1;i<=n;i++) if(a>=i&&a<=n-i+1&&b>=i&&b<=n-i+1) q++; else break; l=n+2; for(i=1;i<=q-1;i++) { l=n-2*(i-1); sum=sum+4*(l-1); } l=l-2; if(a==q) t=b-(q-1)-1; if(b==n-(q-1)) t=a-(q-1)-1+(l-1); if(a==n-(q-1)) t=n-(q-1)-b+(l-1)*2; if(b==q&&a!=q) t=n-(q-1)-a+(l-1)*3; cout<<sum+t; //system("pause"); return 0; }
【NOIP201405-生活大爆炸版石头剪刀布】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> #define ll long long using namespace std; ll read() { int x=0,f=1;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 x*f; } int n,na,nb; int ans1,ans2; int a[205],b[205]; int c[5][5]={ {0,0,1,1,0}, {-1,0,0,1,0}, {-1,-1,0,0,1}, {-1,-1,-1,0,1}, {-1,-1,-1,-1,0}, }; int cal(int x,int y) { if(x>y)return c[y][x]^1; else return c[x][y]; } int main() { n=read();na=read();nb=read(); for(int i=1;i<=na;i++) a[i]=read(); for(int i=1;i<=nb;i++) b[i]=read(); for(int i=1;i<=n;i++) { int x=a[(i-1)%na+1],y=b[(i-1)%nb+1]; ans1+=cal(x,y); ans2+=cal(y,x); } printf("%d %d ",ans1,ans2); return 0; }
【NOIP201408-无线网络发射器选址】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; long long rsum=-1; int rcnt; int d,n; struct Q { int x,y,k; }q[21]; void work(void) { for (int x=0;x<=128;x++) for (int y=0;y<=128;y++) { long long sum=0; for (int i=1;i<=n;i++) if (x-d<=q[i].x&&q[i].x<=x+d&&y-d<=q[i].y&&q[i].y<=y+d) sum+=q[i].k; if (rsum<sum) { rsum=sum; rcnt=1; } else rcnt+=rsum==sum; } printf("%d %lld ",rcnt,rsum); } int main(void) { scanf("%d%d",&d,&n); for (int i=1;i<=n;i++) scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].k); work(); //system("pause"); return 0; }
【NOIP201409-寻找道路】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <cmath> #include <queue> #include <iostream> #define INF 0x7fffffff #define mes(a,x) memset(a,x,sizeof(a)) #define maxn 10010 #define maxR 200010 using namespace std; struct Road { int x,y,d,next; }road[maxR]; int first[maxn],tot; int d[maxn]; bool inq[maxn]; queue< int > Q; int x[maxR],y[maxR],n,m,st,ed; void Ins(int x,int y,int d) { tot++; road[tot].x=x; road[tot].y=y; road[tot].d=d; road[tot].next=first[x]; first[x]=tot; } void Change() { int i,j,x,y,k; for(i=1;i<=n;i++) { if(d[i]==INF) { x=i; for(k=first[x];k!=-1;k=road[k].next) { y=road[k].y; inq[y]=1; } inq[x]=1; } } } void Spfa(int ST) { int x,y,k,i,j; while(!Q.empty()) Q.pop(); inq[ST]=1; d[ST]=0; Q.push(ST); while(!Q.empty()) { x=Q.front(); for(k=first[x];k!=-1;k=road[k].next) { y=road[k].y; if(d[y]>d[x]+road[k].d) { d[y]=d[x]+road[k].d; if(!inq[y]) { Q.push(y); inq[y]=1; } } } inq[x]=0; Q.pop(); } } void Solve() { int i,j; mes(first,-1); tot=0; for(i=1;i<=m;i++) Ins(y[i],x[i],1); for(i=1;i<=n;i++) {d[i]=INF; inq[i]=0;} Spfa(ed); if(d[st]==INF) {printf("-1 "); return ;} mes(inq,0); Change(); for(i=1;i<=n;i++) d[i]=INF; mes(first,-1); tot=0; for(i=1;i<=m;i++) Ins(x[i],y[i],1); Spfa(st); if(d[ed]==INF) printf("-1 "); else printf("%d ",d[ed]); } int main() { int i,j; scanf("%d%d",&n,&m); for(i=1;i<=m;i++) scanf("%d%d",&x[i],&y[i]); scanf("%d%d",&st,&ed); Solve(); //system("pause"); return 0; }