[NA]Lab3 循环线性方程组

本文介绍了CYLL发现的一个商业广告——买一赠二的午餐优惠,但其实背后隐藏着复杂的成本计算。任务是根据菜单上的价格找出每道菜的真实成本。题目涉及一个循环线性方程组,可以通过Gauss-Seidel迭代法解决,给出简洁的AC代码实现。
摘要由CSDN通过智能技术生成

任务概述

题目描述

One day, CYLL found an interesting piece of commercial from newspaper: the Cyber-restaurant was offering a kind of “Lunch Special” which was said that one could “buy one get two for free”. That is, if you buy one of the dishes on their menu, denoted by d i d_i di with price p i p_i pi​​, you may get the two neighboring dishes d ​ i − 1 d_{​i−1} di1​​ and d ​ i + 1 d_{​i+1} di+1​​ for free! If you pick up d 1 d_1 d1​​, then you may get d 2 d_2 d2​​ and the last one d ​ n d_​n dn​​ for free, and if you choose the last one d n d_n dn, you may get d ​ n − 1 d_{​n−1} dn1 and d 1 d_1 d1 for free.

However, after investigation CYLL realized that there was no free lunch at all. The price p i p_i pi of the i i i-th dish was actually calculated by adding up TWICE the cost c i c_i ci of the dish and HALF of the costs of the two “free” dishes. Now given all the prices on the menu, you are asked to help CYLL find the cost of each of the dishes.

Sample program of judge:

#include <stdio.h>

#define Max_size 10000 /* max number of dishes */

void Price( int n, double p[] );

int main()
{
   
    int n, i;
    double p[Max_size];

    scanf("%d", &n);
    for (i=0; i<n; i++) 
        scanf("%lf", &p[i]);
    Price(n, p);
    for (i=0; i<n; i++)
        printf("%.2f ", p[i]);
    printf("\n");

    return 0;
}

/* Your function will be put here */

Sample Input:

12 23.64 17.39 12.77 16.62 10.67 14.85 12.68 26.90 28.30 15.59 37.99 23.18

Sample Output:

9.20 5.58 3.24 7.00 1.99 6.36 2.25 10.01 11.52 0.50 17.65 4.88 

算法思路

很简单的一道题,就是解一个线性方程组,而且还是严格对角占优的,所以方法很多。
哦,先把方程组写出来:
{ 2.0 c 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值