215 条题解
-
0bodhiye LV 8 @ 2017-09-15 13:46:45
#include<iostream> using namespace std; int n, a[101], sum, ans; int main() { std::ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } int ave = sum / n; for (int i = 1; i <= n; i ++ ) { if (a[i] != ave) { a[i + 1] += a[i] - ave; a[i] = ave; ans++; } } cout << ans << endl; return 0; }
-
02017-09-15 13:45:54@
#include<iostream> using namespace std; int n, a[101], sum, ans; int main() { std::ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } int ave = sum / n; for (int i = 1; i <= n; i ++ ) { if (a[i] != ave) { a[i + 1] += a[i] - ave; a[i] = ave; ans++; } } cout << ans << endl; return 0; }
-
02017-09-09 19:59:01@
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int n, cards[105], avg, sum, act; int main() { cout.sync_with_stdio(false), cin.sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> cards[i], sum += cards[i]; avg = sum / n; for (int i = 1; i < n; i++) { if (cards[i] != avg) act++, cards[i + 1] -= avg - cards[i]; } cout << act << endl; return 0; } //贪心。。不需要管移开以后是正还是负,反正如果反过来移动就没毛病了,次数还是一样的
-
02017-08-26 23:23:17@
include "cstdio"
include "iostream"
include "algorithm"
using namespace std;
int a[105];
int main()
{
int n,x=0,ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
x+=a[i];
}
x/=n;
for(int i=1;i<=n;i++)
a[i]-=x;
for(int i=1;i<=n;i++)
{
if(a[i]<0)
{
a[i+1]+=a[i];
ans++;
}
if(a[i]>0)
{
a[i+1]+=a[i];
ans++;
}
}
cout<<ans<<endl;
return 0;
} -
02017-07-28 10:49:50@
经典的贪心算法
Pascalvar n,i,t,x:longint;a:array[1..100]of longint; begin read(n); for i:=1 to n do begin read(a[i]);x:=x+a[i];end; x:=x div n; t:=0; for i:=1 to n-1 do if a[i]-x<>0 then begin a[i+1]:=a[i]-x+a[i+1]; a[i]:=x; t:=t+1; end; write(t); end .
-
02017-07-21 21:26:02@
#include <stdio.h> #include <stdlib.h> using namespace std; int num,//几堆 avl;//平均值 int* order; int step = 0; void range(){ int now = 0; for(int i=0;i<num-1;i++){ now+=order[i]-avl; if(now!=0){ step++; } } printf("%d",step); } int main() { scanf("%d",&num); order = (int*)malloc(num*sizeof(int)); for(int i=0;i<num;i++){ scanf("%d",&order[i]); avl+=order[i]; } avl/=num; range(); return 0; }
-
02017-07-21 21:25:18@
从前向后考虑,只要前面所有牌堆成为平均值需要的牌数!=0,步数就加一(注意只有一堆的情况)。
#include <stdio.h> #include <stdlib.h> using namespace std; int num,//几堆 avl;//平均值 int* order; int step = 0; void range(){ int now = 0; for(int i=0;i<num-1;i++){ now+=order[i]-avl; if(now!=0){ step++; } } printf("%d",step); } int main() { scanf("%d",&num); order = (int*)malloc(num*sizeof(int)); for(int i=0;i<num;i++){ scanf("%d",&order[i]); avl+=order[i]; } avl/=num; range(); return 0; }
-
02017-06-08 21:58:34@
#include<iostream> #include<cstdio> #include<map> #include<string> #include<algorithm> #include<numeric> #include<vector> using namespace std; int main() { //freopen("data.in", "r", stdin); //freopen("data.out", "w", stdout); int a[105] = { 0 }; //vector<int>s(a,a+ 4); int i = 0; int count = 0; int sum = 0, size = 4, ave = 0; int temp = 0; int sub = 0; scanf("%d", &size); for (i = 0; i < size; i++) { scanf("%d", &a[i]); //printf("%d ", a[i]); } //printf("\n"); for (i = 0; i < size; i++) sum += a[i]; ave = sum / size; for (i = size - 1; i >= 0; i--) { if (a[i] < ave) { sub = ave - a[i]; temp = i - 1; while ((a[temp] - sub) < 0) { sub -= a[temp]; a[temp] = 0; temp--; //count++; } a[temp] -= sub; a[i] = ave; count++; } else if (a[i]>ave) { a[i - 1] += (a[i] - ave); a[i] = ave; count++; } else { continue; } } printf("%d\n", count); return 0; }
-
02017-05-23 22:07:25@
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int a[110];
int main()
{
int n,sum=0,ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
}
int aver=sum/n;for(int i=1,j=n;i<j;i++,j--)
{
if(a[i]<aver){
ans++;
a[i+1]-=aver-a[i];
}else if(a[i]>aver){
ans++;
a[i+1]+=a[i]-aver;
}if(a[j]<aver){
ans++;
a[j-1]-=aver-a[j];
}else if(a[j]>aver){
ans++;
a[j-1]+=a[j]-aver;
}}
cout<<ans<<endl;
return 0;
} -
02017-03-18 09:52:25@
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
int n,i,ai[10005],h,ans,p;
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>ai[i];
h=h+ai[i];
}
h=h/n;
for(i=0;i<n-1;i++)
{
if(ai[i]!=h)
{
ai[i+1]=ai[i+1]+ai[i]-h;
ans++;
}
}
cout<<ans<<endl;
system("pause");
return 0;
} -
02017-03-18 09:38:54@
Pascal AC
var
a:array[1..100]of integer;
ave,step,i,n,j:longint;
begin
ave:=0; step:=0;
read(n);
for i:=1 to n do
begin
read(a[i]);
inc(ave,a[i]);
end;
ave:=ave div n;
for i:=1 to n do dec(a[i],ave);
i:=1; j:=n;
while (a[i]=0)and(i<n)do inc(i);
while (a[j]=0)and(j>1)do dec(j);
while i<j do
begin
inc(a[i+1],a[i]);
a[i]:=0;
inc(step);
inc(i);
while(a[i]=0)and(i<j)do inc(i);
end;
write(step);
end. -
02017-02-26 19:50:52@
#include <iostream> #include <cstring> using namespace std; int N; int cards[200]; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int sum=0,avr,res=0; cin>>N; for (int i = 1 ; i <= N ; i++){ cin>>cards[i]; sum += cards[i]; } avr = sum/N; int temp_sum=0; for (int i = 1 ; i <= N ; i++){ temp_sum = (avr+temp_sum) - cards[i]; if(temp_sum){ res++; } } cout<<res<<endl; return 0; }
-
02016-12-04 14:17:58@
#include<cstdio> #include<cstring> using namespace std; int main() { int p,i,n,a[233],t=0; a[0]=0; scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d",&a[i]); a[0]+=a[i]; } p=a[0]/n; for(i=1;i<=n;++i) a[i]-=p; for(i=1;i<n;++i) { if(a[i]==0); else { a[i+1]+=a[i]; a[i]=0; t++; } } printf("%d",t); return 0; }
-
02016-11-20 21:02:08@
贪心算法一步到位
#include<cstdio>
#include<iostream>
using namespace std;
int n,a[1001],ping,s;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) { cin>>a[i]; ping+=a[i]; }
ping/=n;
for(int i=1;i<=n;i++) a[i]-=ping;
int i=1,j=n;
while(a[i]==0&&i<=n) i++;
while(i<j)
{
a[i+1]+=a[i];
a[i]=0;
s++;
i++;
while(i<j&&a[i]==0) i++;
}
cout<<s;
} -
02016-11-17 16:26:33@
#include <cstdio>
int main(){
int n,ave=0,count=0,a[200];
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
ave+=a[i];
}
ave/=n;
for(int i=1;i<=n-1;i++){
if(a[i]!=ave){
count++;
a[i+1]-=ave-a[i];
}
}
printf("%d",count);
return 0;
} -
02016-10-29 10:06:20@
#include<iostream>
using namespace std;
int main()
{
int i,n,a[100],h=0,x=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
h+=a[i];
}
h=h/n;
for(i=0;i<n;i++)
{
if(a[i]<h)
{
a[i+1]-=h-a[i];
x++;
}
if(a[i]==h)
continue;
if(a[i]>h)
{
a[i+1]+=a[i]-h;
x++;
}
}
cout<<x<<endl;
return 0;
} -
02016-10-08 20:37:56@
AC
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<map>
#include<cctype>
#define maxn 110
#define f(x,y) for(int x=1;x<=y;x++)
using namespace std;
inline int read() {
register int x = 0;
register char c = getchar() ;
register int f = 1;
while (isspace(c)) c = getchar() ;
if(c == '-') f = -1,c = getchar();
do x = x * 10 + c - '0', c = getchar();
while (isdigit(c)) ;
return x * f;
}
int a [maxn];
int main(){
int n = read ();
int tol = 0;
f(i,n) a [i] = read (),tol += a[i] ;
tol /= n ;
int ans = 0;
f (i,n) a[i + 1] += a[i] != tol ? ans ++ ,a[i] - tol : 0 ;
printf ("%d",ans);
return 0;
} -
02016-10-02 09:26:59@
总算搞懂了
开始的时候,对于第1堆纸牌,如果等于平均数M,则不操作
如果多了或少了,一定会将纸牌移动给下一堆,或者下一堆补充道第一堆,操作数+1
此时,第二队纸牌视为第一堆,重复操作
有点数学归纳法的感觉 -
02016-09-14 21:35:51@
var
a:array[1..100]of longint;
i,n,e,z:longint;
begin
readln(n); e:=0; z:=0;
for i:=1 to n do begin
read(a[i]);inc(e,a[i]);
end;
e:=e div n;
for i:=1 to n-1 do
if a[i]<>e then begin
a[i+1]:=a[i+1]+a[i]-e;inc(z);a[i]:=e;end;
writeln(z);
end. -
02016-09-06 17:22:05@
#include <iostream> using namespace std; int main() { int n,i,j,k,t=0,move=0; cin>>n; int a[n+1]; for(i=1;i<=n;i++) { cin>>a[i]; t+=a[i]; } t/=n; for(i=1;i<=n;i++) { if(a[i]>t) {move++; a[i+1]+=a[i]-t;} if(a[i]<t) {move++; a[i+1]-=t-a[i];} } cout<<move; }