McCree来师带上任只为三件事公平!公平!还是公平! 学校一共有 � n个学院,每个学院都有一定的经费。McCree希望能找到一个非负整数 � d,各个学院的经费要么加上 � d,要么减去 � d,要么不做改变。最终McCree希望找到最小的 � d使得各个学院的经费都相同。 题目描述 输入格式 第一行为一个正整数 � n ( 1 ≤ � ≤ 100 1≤n≤100 ),表示有 � n个学院。 第二行有 � n 个整数 � 1 , � 2 , … , � � a 1 ,a 2 ,…,a n ( 1 ≤ � � ≤ 100 1≤a i ≤100 ) — 表示各个学院的经费 。 输出格式 输出一行一个整数 � d。 如果 � d不存在,输出-1。c
时间: 2024-01-16 18:04:25 浏览: 75
以下是Python实现代码:
```python
n = int(input())
a = list(map(int, input().split()))
avg = sum(a) / n # 计算平均数
if avg % 1 != 0: # 平均数不是整数,无解
print(-1)
else:
diff = 0 # 统计差值
for x in a:
diff += abs(x - avg)
print(int(diff/2)) # 最小差值为差值的一半
```
具体思路已在代码中注释。
相关问题
用c语言编写程序:从键盘上任给一个郑州大学学生的学号,输出该学号相关的信息。(例如:输入202201011150,输出:2022级,机械与动力工程学院,11班,50号),上载拍照图片、视频:0分。 要求: (1)有输入,对输入的学号判断,不是12位的数字符号重新输入学号(当输入0,结束程序运行),如果是12位数字符号,分别输出:级,学院,班级,班内序号. (2)通过提示信息“是否继续输入学生的学号(y/n)”,回答“y”,继续输入;回答“n”,结束程序运行。 说明: (1)级,限定在'2022','2021','2020','2019','2018'当中,否则重新输入学号 (2)匹配不上给定的学院代码,给出相应提示信息。学院代码如下(可以自行添加)
```c
#include <stdio.h>
#include <string.h>
int main() {
char student_id[13]; // 声明一个长度为13的字符数组,用来存储学号
char *level; // 声明一个指针变量,用来存储年级
char *college; // 声明一个指针变量,用来存储学院
char *class; // 声明一个指针变量,用来存储班级
char *number; // 声明一个指针变量,用来存储班内序号
int is_valid = 0; // 判断学号是否合法的标志变量
// 学院代码表
char college_codes[5][20] = {
{ "电气与自动化工程学院" },
{ "能源与环境学院" },
{ "计算机学院" },
{ "机械与动力工程学院" },
{ "信息工程学院" }
};
while (1) {
printf("请输入一个12位的郑州大学学生学号(输入0结束程序):");
scanf("%s", student_id);
if (strcmp(student_id, "0") == 0) { // 输入0结束程序
break;
}
if (strlen(student_id) != 12) { // 判断学号是否合法
printf("您输入的学号不是12位数字符号,请重新输入!\n");
continue;
}
// 判断年级
switch (student_id[0]) {
case '2':
if (student_id[1] == '0') {
level = "2020级";
} else if (student_id[1] == '1') {
level = "2021级";
} else if (student_id[1] == '2') {
level = "2022级";
} else {
printf("您输入的学号年级不正确,请重新输入!\n");
continue;
}
break;
case '1':
if (student_id[1] == '9') {
level = "2019级";
} else if (student_id[1] == '8') {
level = "2018级";
} else {
printf("您输入的学号年级不正确,请重新输入!\n");
continue;
}
break;
default:
printf("您输入的学号年级不正确,请重新输入!\n");
continue;
}
// 判断学院
switch (student_id[2]) {
case '1':
college = college_codes[3];
break;
case '2':
college = college_codes[2];
break;
case '3':
college = college_codes[4];
break;
case '4':
college = college_codes[1];
break;
case '5':
college = college_codes[0];
break;
default:
printf("对不起,没有找到您输入的学院代码所对应的学院,请重新输入!\n");
continue;
}
// 判断班级
switch (student_id[9]) {
case '1':
class = "1班";
break;
case '2':
class = "2班";
break;
default:
printf("您输入的班级不正确,请重新输入!\n");
continue;
}
// 判断班内序号
switch (student_id[10]) {
case '0':
number = "10号";
break;
case '1':
number = "11号";
break;
case '2':
number = "12号";
break;
case '3':
number = "13号";
break;
case '4':
number = "14号";
break;
case '5':
number = "15号";
break;
case '6':
number = "16号";
break;
case '7':
number = "17号";
break;
case '8':
number = "18号";
break;
case '9':
number = "19号";
break;
default:
printf("您输入的班内序号不正确,请重新输入!\n");
continue;
}
// 输出学生信息
printf("%s,%s,%s,%s\n", level, college, class, number);
// 询问是否继续输入
char choice;
while (1) {
printf("是否继续输入学生的学号(y/n):");
scanf(" %c", &choice); // 注意要在前面加一个空格,避免与上一个输入的回车符冲突
if (choice == 'y' || choice == 'n') {
break;
}
}
if (choice == 'n') {
break;
}
}
return 0;
}
```
在一个圆上任取三个点分别为p、q、s,求三角形pqs为锐角三角形的概率(有书写过程
求三角形pqs为锐角三角形的概率可以按照如下步骤:1. 将圆心O视为原点,令P(x1,y1), Q(x2,y2), S(x3,y3)。2. 计算三点的距离:d1 = sqrt((x1-x2)^2 + (y1-y2)^2),d2 = sqrt((x1-x3)^2 + (y1-y3)^2),d3 = sqrt((x3-x2)^2 + (y3-y2)^2)。3. 计算锐角三角形的概率:P = (1 - (d1^2 + d2^2 - d3^2)/2d1d2)^2。4. 当P > 0时,三角形PQS为锐角三角形,概率为P;当P < 0时,三角形PQS为钝角三角形,概率为0。
阅读全文
相关推荐








