"A003" Sharing solitaire "difficulty a" ———————————————————————————————————————————————————— "topic Requirements"
There are n stacks of cards, numbered separately, ..., N. There are several sheets on each heap, but the total number of cards must be multiples of N. You can take a number of cards on either heap and move them. The rules for the move are: the cards on the numbered 1 heap can only be moved to the heap numbered 2, and the cards taken on the heap numbered N can only be moved to the heap numbered N-1, and the cards on the other heap may be moved to the adjacent left or right heap. Now it's time to find a way to move, with the fewest number of moves to make as many cards as you can on each heap.
For example, the n=4,4 heap card number is: ①9②8③17④6
Move 3 times to achieve the goal: from ③ take 4 cards put to ④ (9 8), from ③ take 3 cards put to ② (9), from ② take 1 cards put to ① (10 10 10 10).
"Input Requirements"
First line: N (n heap Solitaire, 1 <= n <= 100)
Second line: A1 A2 ... An (for N heap solitaire, each pile of solitaire initial number is greater than 0 and not more than 10000), 22 is separated by a space.
"Output Requirements"
A number that represents the minimum number of moves for each heap of cards that are equal.
"Input Sample"
49 8 17 6
"Output Sample"
3
Resolution
The main thing to examine is the mathematical thinking of data processing, the grammatical aspects only need to use simple loops and arrays to solve the problem. Since we want to divide, we may as well calculate the target value, that is, all the number and then divided by N, since it can only move left or right, then we will put the difference between the current value and the target value "hands" to the current +1 of the heap to achieve the goal.
Code
#include <iostream> #include <cstdio>using namespace Std;int main () { int n,a[100],sum=0,k=0; cin>>n; for (int i=0;i<n;i++) { cin>>a[i]; Sum+=a[i]; } int mid=sum/n; for (int i=0;i<n;i++) { int t=0; if (A[i]!=mid) { t=a[i]-mid; a[i+1]+=t; k++; } } cout<<k; return 0;}
Water together NOIP2002 raise the group "A003"