daily practice

Problem description
Given n nn integers a 1, a 2,..., a n a_{1}, a_{2}, \cdots, a_{n}a1,a2​,⋯,an​
Find the sum of them multiplied by two and then added together, that is

S = a 1 ⋅ a 2 + a 1 ⋅ a 3 + ⋯ + a 1 ⋅ a n + a 2 ⋅ a 3 + ⋯ + a n − 2 ⋅ a n − 1 + a n − 2 ⋅ a n + a n − 1 ⋅ a n S=a_{1}  \cdot a_{2}+a_{1} \cdot a_{3}+\cdots+a_{1} \cdot a_{n}+a_{2} \cdot a_{3}+\cdots+a_{n-2} \cdot a_{n-1}+a_{n-2} \cdot  a_{n}+a_{n-1} \cdot a_{n}
S=a1⋅a2​+a1​⋅a3+⋯+a1⋅an+a2⋅a3+⋯+an−2⋅an−1+an−2⋅an+an−1⋅an
Input format
The first line of input contains an integer n nn.

The second line contains n nn integers a 1, a 2,... a n a_{1}, a_{2}, \cdots a_{n}a1,a2
​,⋯an.

Output format
Outputs an integer S SS representing the desired sum. Please use the appropriate data type for the calculation.

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

typedef long long LL;
const int N = 100010, mod = 1000000009;
int a[N];
int main()
{
    int n, k;
    cin >> n >> k;
    for (int i = 0;i < n;i++) scanf("%d", &a[i]);
    sort(a, a + n);
    LL res = 1;
    int l = 0, r = n - 1;
    int sign = 1;

    if (k % 2)
    {
        res = a[r];
        r--;
        k--;
        if (res < 0) sign = -1;
    }
    while (k)
    {
        LL x = (LL)a[l] * a[l + 1], y = (LL)a[r] * a[r - 1];
        if (x * sign > y * sign)
        {
            res = x % mod * res % mod;
            l += 2;
        }
        else
        {
            res = x % mod * res % mod;
            res -= 2;
        }
        k -= 2;
    }
    cout << res << endl;
    return 0;
}

可以考虑使用 fgets() 函数来读取字符串,这样可以避免使用 scanf() 函数时可能出现的缓冲区溢出问题。同时,为了确保读取的数据类型正确,可以在读取前先清空输入缓冲区。 改进后的代码如下: ``` #include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 100 struct Student { int id; char name[20]; float daily_score; float practice_score; float final_score; float total_score; }; int main() { struct Student stu[MAX_STUDENT_NUM]; int n, i; printf("Please enter the number of students: "); scanf("%d", &n); // 清空输入缓冲区 while (getchar() != '\n'); for (i = 0; i < n; i++) { printf("Please enter the ID, name, daily score, practice score and final score of student %d: ", i + 1); // 读取字符串 fgets(stu[i].name, 20, stdin); // 去掉字符串末尾的换行符 stu[i].name[strcspn(stu[i].name, "\n")] = '\0'; // 读取其他数据类型 scanf("%d %f %f %f", &stu[i].id, &stu[i].daily_score, &stu[i].practice_score, &stu[i].final_score); // 计算总评成绩 stu[i].total_score = stu[i].daily_score * 0.3 + stu[i].practice_score * 0.3 + stu[i].final_score * 0.4; } // 输出学生信息 printf("\n"); printf("ID\tName\t\tDaily score\tPractice score\tFinal score\tTotal score\n"); for (i = 0; i < n; i++) { printf("%d\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", stu[i].id, stu[i].name, stu[i].daily_score, stu[i].practice_score, stu[i].final_score, stu[i].total_score); } return 0; } ``` 这段代码可以正确读取字符串和其他数据类型,并计算每位学生的总评成绩。同时,输出格式也进行了一定的调整,使得输出更加清晰易读。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值