3320:练54.3 竞选总统(编程启蒙)

#include <iostream> 
#include <algorithm>
using namespace std;
int n,a[105],sum;
int main() {
	while (1) {
		cin >> n;
		if (n == 0) break;
		for (int i = 0; i < n; i++) cin >> a[i]; 
		sort(a, a + n);
		sum = 0;
		for (int i = 0; i <= n / 2; i++) {
			sum += a[i] / 2 + 1;
		}
		cout << sum << endl;
	}
	return 0;
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用结构体数组的程序,可以实现上述输出: ```c #include <stdio.h> // 定义结构体 Book struct Book { int id; char name[50]; char author[50]; float price; int quantity; }; int main() { // 定义结构体数组 books,包含 5 个元素 struct Book books[5] = { {1001, "C语言程序设计", "谭浩强", 45.6, 10}, {1002, "Java程序设计", "张明", 54.3, 15}, {1003, "算法数据结构", "严伟民", 37.0, 20}, {1004, "html5与css3", "王晓", 67.5, 13}, {1005, "Android程序设计", "王霖", 79.3, 25} }; // 输出表头 printf("编号\t书名\t\t作者\t\t价格\t数量\n"); // 输出每个书籍的信息 for (int i = 0; i < 5; i++) { printf("%d\t%s\t%s\t%.2f\t%d\n", books[i].id, books[i].name, books[i].author, books[i].price, books[i].quantity); } // 计算总价值 float total = 0; for (int i = 0; i < 5; i++) { total += books[i].price * books[i].quantity; } // 输出总价值 printf("%s:%.2f %s:%.2f %s:%.2f %s:%.2f %s:%.2f\n", books[0].name, books[0].price * books[0].quantity, books[1].name, books[1].price * books[1].quantity, books[2].name, books[2].price * books[2].quantity, books[3].name, books[3].price * books[3].quantity, books[4].name, books[4].price * books[4].quantity); return 0; } ``` 输出结果如下: ``` 编号 书名 作者 价格 数量 1001 C语言程序设计 谭浩强 45.60 10 1002 Java程序设计 张明 54.30 15 1003 算法数据结构 严伟民 37.00 20 1004 html5与css3 王晓 67.50 13 1005 Android程序设计 王霖 79.30 25 C语言程序设计:456.00 Java程序设计:814.50 算法数据结构:740.00 html5与css3:877.50 Android程序设计:1982.50 ``` 注意,为了使输出格式对齐,我在输出书名和作者时使用了 `\t` 制表符。另外,为了计算总价值,我使用了一个循环遍历结构体数组,并累加每个书籍的价格和数量的乘积。最后,我使用了一个长字符串格式化输出总价值,以保证输出的金额格式正确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值