1 条题解

  • 0
    @ 2021-6-14 23:29:25

    C :

    #include<stdio.h>
    int main()
    {int and1=0,and2=0,ave=0,i,j=0,n,a[1000]={0};
     scanf("%d",&n);
     for(i=0;i<n;i++)
     {scanf("%d",&a[i]);
     and1+=a[i];}
     ave=and1/n;
     for(i=0;i<n;i++)
     {if(a[i]!=ave)
      {a[i+1]=a[i+1]+a[i]-ave;
      j++;}
    }
    printf("%d",j);
    return 0;
    }
    
    

    C++ :

    #include<cstdio>
    int main()
    {
    	int n,a=0,s=0,num[101];
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	{scanf("%d",&num[i]);a+=num[i];}
    	a/=n;
    	for(int i=1;i<=n;i++)
    	num[i]-=a;
    	int i=1,j=n;
    	while(num[i]==0&&i<n)i++;
    	while(num[i]==0&&j>1)j--;
    	while(i<j)
    	{
    		num[i+1]+=num[i];
    		num[i]=0;
    		s++;
    		i++;
    		while(num[i]==0&&i<j)i++;
    	}
    	printf("%d",s);
    	return 0;
    }
    

    Pascal :

    var n,i,sum,v,ans:longint;a:array[1..100]of longint; 
    begin
      readln(n); 
      for i:=1 to n do
        begin
          read(a[i]); 
          sum:=sum+a[i]; 
        end; 
      v:=sum div n; 
      for i:=1 to n-1 do
        if a[i]<>v then
          begin
            inc(ans); 
            a[i+1]:=a[i+1]+a[i]-v; 
          end; 
      writeln(ans); 
    end. 
    

    Java :

    import java.util.*;
    public class Main{
      public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int dist = 0,cnt = 0,sum = 0;
        int[] arr = new int[n];
        for(int i = 0; i < n; i ++){
          int value = cin.nextInt();
          sum += value;
          arr[i] = value;
        }
        int average = sum/n;
        for(int i = 0; i < n; i ++){
          if(dist != 0){
            cnt += 1;
          }
          arr[i]+=dist;
          dist = arr[i] - average;
    
        }
        System.out.println(cnt);
      }
    }
    
    • 1

    信息

    ID
    218
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者