华南农业大学C语言程序设计课后习题(第四章)

华南农业大学C语言程序设计课后习题(第四章)

18042 计算分段函数值

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC;VC

Description 根据如下数学公式,编写程序输入x,计算并输出y的值,保留两位小数

输入格式 输入一个实数x

输出格式 输出函数值

输入样例 0

输出样例 0.00

#include<stdio.h>
int main()
{
 double x,y;
 scanf("%lf",&x);
 if(x<1)
 y=x;
 else if(1<=x&&x<10)
       y=2*x-1;
      else
       y=3*x-11;
 printf("%.2f",y);
 return 0;
}

18043 找出3个数中最大的数

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC;VC

Description 编写程序,由键盘输入3个整数,输出其中最大的数。

输入格式 三个整数,空格分隔

输出格式 最大的数

输入样例 3 6 4

输出样例 6

#include<stdio.h>
int main()
{
 int a,b,c,max;
 scanf("%i%i%i",&a,&b,&c);
 max=a>b?a:b;
 printf("%i",max=max>c?max:c);
 return 0;
}

18044 成绩等级评分

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC;VC

Description 编写程序,由键盘输入一个百分制的整数成绩,要求输出对应的成绩等级。90分以上为A,80到89分为B,70到79分为C,60到69分为D,
60分以下为E。成绩不在0到100之间时输出“error”

输入格式 一个整数成绩

输出格式 输出对应的等级或error

输入样例 99

输出样例 A

#include<stdio.h>
int main()
{
int s;
scanf("%d",&s);
if(100>=s&&s>=90)
printf("A");
else if(s<90&&s>=80)
printf("B");
else if(s>=70&&s<80)
printf("C");
else if(60<=s&&s<70)
printf("D");
else if(0<=s&&s<60)
printf("E");
else
printf("error");
return 0;
}

18045 前一个和后一个字符

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC;VC

Description 编写程序,输入一个数字字符,输出其前一个和后一个的数字字符,如果输入的是0前一个输出
“first”,9后一个则输出“last”,输入的不是数学字符,输出“error”

输入格式 一个字符

输出格式 输出结果

输入样例 0

输出样例 first 1

#include<stdio.h>
int main()
{
 char a,b,t;
 scanf("%c",&t);
 if(t>'0'&&t<'9')
 {
  a=t+1;
  b=t-1;
  printf("%c %c",b,a);
 }
 else if(t=='0')
 printf("first 1");
 else if(t=='9')
 printf("8 last");
 else
 printf("error");
 return 0;
}

18037 20秒后的时间

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC

Description 编写程序,输入三个整数变量hour(小时)、minute(分钟)、second(秒)代表一个时间,
输出该时间20秒以后的时间。

输入格式 一行三个整数,分别代表小时、分钟、秒,中间使用冒号分隔

输出格式 输出一个时间,数字间用冒号分隔
小时、分钟、秒均占两个数字位,不足位用0补足

输入样例 15:30:41

输出样例 15:31:01

#include<stdio.h>
int main()
{
 int h,m,s;
 scanf("%d:%d:%d",&h,&m,&s);
 if(h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)
 {
   if(s+20>=60)
   {
      s=s-40;
      if(m==59)
      {
        if(h==23)
        h=0;
        else
        h++;
        m=0;
      }
      else
      m++;
   }
   else
   s=s+20;
 }
 printf("%02d:%02d:%02d",h,m,s);
 return 0;
}

18041 分期还款(加强版)

时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC;VC

Description 从银行贷款金额为d,准备每月还款额为p,月利率为r。请编写程序输入这三个数值,计算并输出多少个月能够还清贷款,输出时保留1位小数。
如果无法还清,请输出“God”
计算公式如下:

在这里插入图片描述

输入格式 三个数,分别为货款金额、每月还款和月利率,以空格分隔,均为非负数,其中d,p,r>=0

输出格式 需要还款的月份

输入样例 50 50 0.01

输出样例 1.0

#include<stdio.h>
#include<math.h>
int main()
{
 double d,p,r;
 scanf("%lf%lf%lf",&d,&p,&r);
 if(d==0)
 printf("0.0\n");
 else if(d*(1+r)-p-d>1e-3)
 printf("God\n");
 else
 printf("%.1f",log10(p/(p-d*r))/log10(r+1));
 return 0;
}
import numpy as np # 定义参数 n_lags = 31 # 滞后阶数 n_vars = 6 # 变量数量 alpha = 0.05 # 置信水平 # 准备数据 data = np.array([820.95715,819.17877,801.60077,30,26164.9,11351.8], [265.5425,259.05476,257.48619,11.4,12525,4296.5], [696.9681,685.54114,663.32014,47.5,23790.484,8344.8], [4556.1091,440.58995,433.21995,24.6,12931.388,5575.4], [360.08693,353.75386,351.59186,26.9,11944.322,4523], [938.55919,922.25468,894.26468,35.3,27177.893,8287.4], [490.47837,477.35237,385.17474,24.5,14172.1,6650.4], [553.15463,452.35042,425.92277,32.9,16490.17,7795], [740.35759,721.68259,721.68259,15.5,26117.755,7511.7], [1581.99576,1579.50357,1571.23257,65.4,59386.7,15347.2], [1360.91636,1360.20825,1358.11425,66.4,57160.533,8080], [564.06146,560.91611,559.08711,35.2,22361.86,6165.4], [732.17283,727.25063,725.93863,29.7,22177.389,4393.2], [424.12777,424.10579,411.19979,21.6,14691.359,4695.6], [1439.38133,1437.85585,1436.67585,77.3,50123.672,15479], [961.92496,935.21589,931.28189,45.7,28073.9,11273.3], [881.92808,868.65804,832.44504,46.1,27409.15,11224.4], [713.32299,710.75882,707.42682,35.8,24887.111,5164.2], [2657.28891,2599.20299,2515.67859,92,94207.179,19066.4], [420.95033,418.22931,416.80631,25.6,13309.9,7020], [193.92636,193.84936,193.83836,10.9,6133,6139.5], [499.81565,493.73678,485.2468,20.9,13555.897,3412], [951.93942,939.58126,930.049,45.6,27245.608,7752.5], [309.88498,297.05055,295.69055,22.6,11929.038,3903.2], [411.87141,406.63838,389.29638,27.8,12197.085,3834.1], [45.53226,39.24379,55.34631667,7.5,1872.333333,564.3], [532.67282,524.78031,520.89851,24,18041.642,3902], [269.00374,266.96222,211.14422,20.3,7163.069,3515.4], [91.95276,88.77094,85.74583,7.7,1962.8,645.8], [120.60234,116.39872,113.85872,9.8,4227.003,1706.2], [362.98862,350.36495,318.70232,23.7,11615.383,5752.1]) # 计算VAR模型的系数 X = np.zeros((data.shape[0] - n_lags, n_lags * n_vars)) y = np.zeros((data.shape[0] - n_lags, n_vars)) for i in range(n_lags, data.shape[0]): X[i-n_lags, :] = data[i-n_lags:i, :].reshape(1, -1) y[i-n_lags, :] = data[i, :] coefficients = np.linalg.inv(X.T @ X) @ X.T @ y # 计算残差 residuals = y - X @ coefficients # 计算PVAR模型的紧贴矩阵 T = residuals[n_lags:, :] @ residuals[:-n_lags, :].T / (data.shape[0] - n_lags) # 计算PVAR模型的系数 u, s, vh = np.linalg.svd(T) S_inv = np.diag(np.sqrt(s[:n_vars])) @ np.linalg.inv(vh[:n_vars, :]) A = S_inv @ u[:, :n_vars].T @ residuals[n_lags:, :].T # 计算置信区间 t_value = np.abs(np.tinv(alpha/2, data.shape[0]-n_lags-n_vars)) se = np.sqrt((1/(data.shape[0]-n_lags-n_vars)) * (np.sum(residuals[n_lags:, :]**2) / (data.shape[0]-n_lags-n_vars-1))) conf_int = t_value * se print("PVAR模型的系数:\n", A) print("置信区间:[{:.4f}, {:.4f}]".format(A.mean() - conf_int, A.mean() + conf_int))这段代码有什么错误
05-22
这段代码有几个错误: 1.在定义数据的时候,应该用双方括号而不是单方括号,这样才能得到一个二维数组。 2.在计算残差的时候,应该用y - X @ coefficients,而不是y - coefficients @ X。 3.在计算置信区间的时候,应该使用样本标准差而不是总体标准差,所以分母应该是(data.shape[0]-n_lags-n_vars-1)而不是(data.shape[0]-n_lags-n_vars)。 下面是修改后的代码: ```python import numpy as np # 定义参数 n_lags = 31 # 滞后阶数 n_vars = 6 # 变量数量 alpha = 0.05 # 置信水平 # 准备数据 data = np.array([[820.95715,819.17877,801.60077,30,26164.9,11351.8], [265.5425,259.05476,257.48619,11.4,12525,4296.5], [696.9681,685.54114,663.32014,47.5,23790.484,8344.8], [4556.1091,440.58995,433.21995,24.6,12931.388,5575.4], [360.08693,353.75386,351.59186,26.9,11944.322,4523], [938.55919,922.25468,894.26468,35.3,27177.893,8287.4], [490.47837,477.35237,385.17474,24.5,14172.1,6650.4], [553.15463,452.35042,425.92277,32.9,16490.17,7795], [740.35759,721.68259,721.68259,15.5,26117.755,7511.7], [1581.99576,1579.50357,1571.23257,65.4,59386.7,15347.2], [1360.91636,1360.20825,1358.11425,66.4,57160.533,8080], [564.06146,560.91611,559.08711,35.2,22361.86,6165.4], [732.17283,727.25063,725.93863,29.7,22177.389,4393.2], [424.12777,424.10579,411.19979,21.6,14691.359,4695.6], [1439.38133,1437.85585,1436.67585,77.3,50123.672,15479], [961.92496,935.21589,931.28189,45.7,28073.9,11273.3], [881.92808,868.65804,832.44504,46.1,27409.15,11224.4], [713.32299,710.75882,707.42682,35.8,24887.111,5164.2], [2657.28891,2599.20299,2515.67859,92,94207.179,19066.4], [420.95033,418.22931,416.80631,25.6,13309.9,7020], [193.92636,193.84936,193.83836,10.9,6133,6139.5], [499.81565,493.73678,485.2468,20.9,13555.897,3412], [951.93942,939.58126,930.049,45.6,27245.608,7752.5], [309.88498,297.05055,295.69055,22.6,11929.038,3903.2], [411.87141,406.63838,389.29638,27.8,12197.085,3834.1], [45.53226,39.24379,55.34631667,7.5,1872.333333,564.3], [532.67282,524.78031,520.89851,24,18041.642,3902], [269.00374,266.96222,211.14422,20.3,7163.069,3515.4], [91.95276,88.77094,85.74583,7.7,1962.8,645.8], [120.60234,116.39872,113.85872,9.8,4227.003,1706.2], [362.98862,350.36495,318.70232,23.7,11615.383,5752.1]]) # 计算VAR模型的系数 X = np.zeros((data.shape[0] - n_lags, n_lags * n_vars)) y = np.zeros((data.shape[0] - n_lags, n_vars)) for i in range(n_lags, data.shape[0]): X[i-n_lags, :] = data[i-n_lags:i, :].reshape(1, -1) y[i-n_lags, :] = data[i, :] coefficients = np.linalg.inv(X.T @ X) @ X.T @ y # 计算残差 residuals = y - X @ coefficients # 计算PVAR模型的紧贴矩阵 T = residuals[n_lags:, :] @ residuals[:-n_lags, :].T / (data.shape[0] - n_lags) # 计算PVAR模型的系数 u, s, vh = np.linalg.svd(T) S_inv = np.diag(np.sqrt(s[:n_vars])) @ np.linalg.inv(vh[:n_vars, :]) A = S_inv @ u[:, :n_vars].T @ residuals[n_lags:, :].T # 计算置信区间 t_value = np.abs(np.tinv(alpha/2, data.shape[0]-n_lags-n_vars-1)) se = np.sqrt((1/(data.shape[0]-n_lags-n_vars-1)) * (np.sum(residuals[n_lags:, :]**2) / (data.shape[0]-n_lags-n_vars-1))) conf_int = t_value * se print("PVAR模型的系数:\n", A) print("置信区间:[{:.4f}, {:.4f}]".format(A.mean() - conf_int, A.mean() + conf_int)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑾瑜D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值