A SPOJ EASYMRKS Easy Marks
B SPOJ CANDY Candy I
输入第一行N。
N行每一行每个人的糖果数量。
最后求每个人糖果一样需要移动的糖果数量如果不能平均输出-1。
先求和sum然后判断sum%N==0如果不等于则不能平均分输出-1。如果==0然后求平均值b循环当a[i]>b则num+=a[i]-b。
/*********************
author : MengFanzhuang
*********************/
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int N;
while(scanf("%d",&N)!=EOF&&N!=-1)
{
int a[10005]={0},sum=0;
for(int j=0;j<N;++j)
{
scanf("%d",&a[j]);
sum+=a[j];
}
if(sum%N!=0) cout<<"-1"<<endl;
else
{
int b=sum/N,num=0;
for(int i=0;i<N;++i)
{
if(a[i]>b)num+=(a[i]-b);
}
cout<<num<<endl;
}
}
return 0;
}
C UVA 10346 Peter’s Smokes
输入n支烟然后每k个烟头可以组成一支新烟最后输出吸烟的数量。
wa几次之后开始胡乱改最后静下心来又分析了一下才改对。
/*********************
author : MengFanzhuang
*********************/
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
int num=a; //初始化num=a
while(a>=b)
{
num+=a/b; //a/b是a个烟头组成新烟的数量
int c=a/b; //存一下新烟的数量
a%=b; //剩下烟头的数量
a+=c; //再加上新烟的数量
}
cout<<num<<endl;
}
return 0;
}
D SPOJ AE00 Rectangles
E UVA 12708 GCD The Largest
F CodeForces 304A Pythagorean Theorem II
G URAL 1573 Alchemy
组合数
输入第一行b r y表示各颜料数量。
下面的blue red yellow每出现一个表示每次需要用到的b r y颜料。
统计blue red yellow分别出现的次数b1 r1 y1然后求 C(b,b1)*C(r,r1)*C(y,y1)。
/*********************
author : MengFanzhuang
*********************/
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
long long C(int n,int r)
{
if(r==0) return 1;
else return C(n,r-1)*(n-r+1)/r;
}
int main()
{
int b,r,y;
while(scanf("%d%d%d",&b,&r,&y)!=EOF)
{
int k;
int b1,r1,y1;
b1=r1=y1=0;
scanf("%d",&k);
for(int i=0; i<k; ++i)
{
char a1[10];
scanf("%s",a1);
if(strcmp(a1,"Red")==0) r1++;
else if(strcmp(a1,"Blue")==0) b1++;
else if(strcmp(a1,"Yellow")==0) y1++;
}
if(r1>r/2) r1=r-r1;
if(b1>b/2) b1=b-b1;
if(y1>y/2) y1=y-y1;
long long a=C(r,r1)*C(b,b1)*C(y,y1);
cout<<a<<endl;
}
return 0;
}
H URAL 1022 Genealogical Tree
拓扑排序
输入第一行N N个数。
N行 第i行表示排在i后面的数。
最后输出正确的序列。
用队列做的wa了好多次因为这个题可能存在多解然后输出任意一个解就可以。
5
0
1 0
1 0
1 0
1 0
比如这个我写的那个队列就处理不了。
/*********************
author : MengFanzhuang
*********************/
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[105][105]= {0};
int b,in[105]= {0},num[105];
for(int i=1; i<=n; ++i)
{
while(scanf("%d",&b),b)
{
a[i][b]=1;
in[b]++;
}
}
int j=0,flag=0;
for(int i=1; i<=n; ++i)
{
for(int i1=1; i1<=n; ++i1)
{
if(in[i1]==0)
{
if(flag) cout<<" ";
cout<<i1;
flag=1;
in[i1]=-1;
for(int i2=1; i2<=n; ++i2)
{
if(a[i1][i2])
{
in[i2]--;
}
}
break;
}
}
}
cout<<endl;
}
return 0;
}
队列处理虽然不对但还是备份一下。
/*********************
author : MengFanzhuang
*********************/
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
vector<int> a[105];
int b,in[105]= {0};
for(int i=1; i<=n; ++i)
{
while(scanf("%d",&b)!=EOF&&b)
{
a[i].push_back(b);
in[b]++;
}
}
queue<int> q;
for(int i=1; i<=n; ++i)
{
if(in[i]==0)
{
q.push(i);
cout<<i;
in[i]=-1;
break;
}
}
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int i=0; i<a[temp].size(); ++i)
{
in[a[temp][i]]--;
if(in[a[temp][i]]==0)
{
q.push(a[temp][i]);
in[a[temp][i]]=-1;
cout<<" "<<a[temp][i];
}
}
for(int i=1; i<=n; ++i)
{
if(in[i]==0)
{
q.push(a[temp][i]);
in[a[temp][i]]=-1;
cout<<" "<<a[temp][i];
}
}
}
cout<<endl;
}
return 0;
}