Magic Squares

在一组环绕布置的相同水箱中,通过调整阀门的开启状态,找到最少的阀门开启次数,以使所有水箱内的水量相等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Magic Squares

Description

You have n identical water tanks arranged in a circle on the ground. Each tank is connected with two neighbors by a pipe with a valve. All valves are initially closed, so water in each tank cannot move. But if all the valves are opened, water can flow freely among all the tanks. After some time, all the water levels will eventually equalize.
However, in many cases, only few valves need to be opened to equalize the water levels. For example, if the initial water levels are 10, 3, 5, 4, 1, 1, only 3 valves need to be opened (one valve between level 3 and level 5, another valve between level 1 and level 1, and the final one between level 1 and level 10).
Your task is to find the minimal number of valves to be opened, to equalize all water levels in each tank.

Input

The first line contains t (1<=t<=20), the number of test cases followed. Each test case begins with one integer n(3<=n<=200), followed by n non-negative integers not greater than 100, the initial water levels.

Output

For each test case, print the minimal number of valves to be opened.

Sample Input

3
6 10 3 5 4 1 1
4 4 4 3 3
8 2 1 1 2 2 1 1 6

Sample Output

3
2
5


题意:n个相同的水箱装有不同的量水 连成一个环,相邻两个tank之间有一个阀门,求最少打开几个开关,使得每个水箱的水量相同。

思路:n个开关,最坏的情况打开n-1个即可。至少可以有一个开关close。枚举一个开关关闭。然后求最少的连通方案。

直接上代码:

/*the code author is sunquan*/
/*2013-6-2 18:00*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
{
    int t,n;
    int a[200];
    int total;//the sum of water
    int ans;//answer
    cin>>t;
    while(t--)
    {
       cin>>n;
       total=0;
       for(int i=0;i<n;i++)
       {
          cin>>a[i];
          total+=a[i];
       }
       ans=n-1;//the max number of valves to be opened
       //enum the position of bump
       for(int first=0;first<n;first++)
       {
          int sum,counter,open;
          sum=counter=open=0;
          for(int j=0; j<n;j++)
          {
             sum+=a[(first+j)%n];
             counter++;
             if(sum*n==total*counter)
               counter=sum=0;
             else
               open++;
          }
          ans = ans<open?ans:open;
       }
         cout<<ans<<endl;
    }
    return 0;
}


### MATLAB `magic` 函数的使用方法 `magic` 是 MATLAB 提供的一个内置函数,用于生成魔方矩阵(Magic Square)。魔方矩阵是一个 n×n 的正方形网格,其中每一行、列以及两条对角线上所有数之和均相等。这种特性使得它在数值分析和算法测试中非常有用。 #### 基本语法 以下是 `magic` 函数的基本调用方式: ```matlab M = magic(n); ``` - 参数 `n` 表示要生成的魔方矩阵的大小(即行列数),必须为整数。 - 如果输入参数不是标量或者小于等于零,则会抛出错误。 #### 特性说明 由 `magic` 创建出来的矩阵具有如下特点[^1]: - 所有元素均为不同的正整数; - 各行各列及其主次对角线上的数字总和相同;此共同值被称为“幻数”。 #### 示例代码展示 下面给出几个具体的例子来演示如何应用该命令: ##### 单一魔方阵构建实例 ```matlab % 构建一个3阶魔方矩阵 A = magic(3); disp(A); ``` 运行上述脚本将会得到以下输出结果: ``` 8 1 6 3 5 7 4 9 2 ``` 可以看到每行、列还有两个斜方向加起来都是相同的数值——这里是15。 ##### 结合绘图功能的应用场景 我们还可以利用之前提到过的subplot技巧把不同维度下的魔法平方画出来做对比研究。比如这样写程序片段就可以实现这一目标了: ```matlab figure; % 新开一张空白图表窗口 for i=1:4 ax(i)=subplot(2,2,i); % 定义四个子区域位置布局情况 imagesc(magic(round((i+1)*pi))); colormap gray; end colorbar ;title('Different Magic Squares'); ``` 这里通过循环机制分别展示了基于圆周率近似值得到的不同规模版本的神奇数组图案效果,并统一设置了灰度颜色映射方案以便观察更加直观清晰明了。 #### 注意事项 需要注意的是当指定尺寸过小时可能无法形成标准意义上的魔术方格结构,例如尝试执行`magic(1)`只会返回单独一个单元格包含数字'1';而如果请求负数或非整型作为输入则会被拒绝接受并提示相应的异常信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值