2023年江西财经大学831真题答案,个人制作非官方答案仅供参考,有误可以私信我一起讨论。
程序设计题
5.1
#include<stdio.h>
int main(){
int x;
double y,dj=8.58,hdzk=8.18;
scanf("%d %lf",&x,&y);
if(x==3){
hdzk-=0.2;
}
//满减优惠减去的钱
printf("活动折扣优惠需要付%.3lf\n",y*hdzk);
printf("满减优惠优惠需要付%.3lf\n",
((y*8.58)-((int)(y*8.58)/200)*20));
if(((y*8.58)-((int)(y*8.58)/200)*20)>y*hdzk){
printf("应该选择活动折扣,最优惠需要付%.3lf\n",y*hdzk);
}else{
printf("应该选择满减优惠,最优惠需要付%.3lf\n",
((y*8.58)-((y*8.58)/200)*20));
}
return 0;
}
5.2
#include<stdio.h>
#include<string.h>
int main(){
char str[81],str1[45],str2[45],k=0,l=0;
scanf("%s",&str);
for(int i=0;i<strlen(str);i++){
if(i%2){
str1[k++]=str[i];
}else{
str2[l++]=str[i];
}
}
int j=0;
for(int i=0;i<k;i++){
str[j++]=str1[i];
}
for(int i=0;i<k;i++){
str[j++]=str2[i];
}
printf("%s",str);
return 0;
}
5.3
#include<stdio.h>
int main(){
int n,max=-1,ans,flag=0;
int a[100];
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<n;j++){
if(a[i]==a[j]){
count++;
}
}
if(count>n/2){
ans=a[i];
flag=1;
}
}
if(flag)printf("%d",ans);
else printf("don't exist");
return 0;
}
5.4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct data{
char t[20];
double s;
};
void input(char *ts,double temp){
FILE *fp;
data dd;
strcpy(dd.t,ts);
dd.s=temp;
if((fp=fopen("dailytemp.bin","a+b"))==NULL){
printf("can't open the file!");
exit(0);
}
fwrite(&dd,sizeof(dd),1,fp);
fclose(fp);
}
void report(){
FILE *fp;
data dd;
if((fp=fopen("dailytemp.bin","rb"))==NULL){
printf("can't open the file!");
exit(0);
}
int k=0;
double a[10005];
while(fread(&dd,sizeof(dd),1,fp)){
a[k++]=dd.s;
}
for(int i=0;i<5-1;i++){//对温度进行排序
for(int j=i;j<5-i-1;i++){
if(a[i]>a[i]+1){
double temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("温度 人数\n----------\n");
int count=1;
for(int i=0;i<k;i++){//输出人数,如果一个温度输出完了,人数重新置为1
if(a[i]!=a[i+1]){
printf("%.1lf %d\n",a[i],count);
count=1;
}else count++;
}
printf("----------\n共%d人",k);
fclose(fp);
}
int main(){
char ss[20];
double temp;
for(int i=1;i<=5;i++){
scanf("%s",&ss);
scanf("%lf",&temp);
input(ss,temp);
}
report();
}
/*
13:33:23 36.5
13:35:23 32.5
13:36:23 36.5
13:37:23 33.5
13:31:23 37.5
*/
5.5
#include<stdio.h>
#include<math.h>
#define N 11
#define K 3
typedef struct{
float x;
float y;
}Point;
void getmean(int center[N]);
float getdistance(Point point1,Point point2);
void cluster();
float geterror();
Point point[N]={{2.0,10.0},{2.0,5.0},{8.0,4.0},{5.0,8.0},
{7.0,5.0},{6.0,4.0},{1.0,2.0},{4.0,9.0},
{7.0,3.0},{1.0,3.0},{3.0,9.0}};//N个样本点
Point mean[K]; //K个簇中心
int center[N]; //N个样本所在的簇
float getdistance(Point point1,Point point2){//计算两点之间距离
float s=(point2.x-point1.x)*(point2.x-point1.x)+
(point2.y-point1.y)*(point2.y-point1.y);
float d=pow(s,0.5);
return d;
}
void cluster(){//聚类
for(int i=0;i<N;i++){
float min=1e5;
for(int j=0;j<K;j++){
if(getdistance(mean[j],point[i])<min){
min=getdistance(mean[j],point[i]);
center[i]=j;//计算每个样本到K个簇中心的距离,并将其归属到距离最小的簇中心所对应的簇中
}
}
}
getmean(center);
}
void getmean(int center[N]){//计算簇中心
for(int i=0;i<K;i++){//针对每个簇,重新计算它的簇中心的位置
float x=0,y=0;
int count=0;
for(int j=0;j<N;j++){
if(center[j]==i){//检查数据集每个簇是否属于当前簇
x+=point[j].x;//相加用来计算算数平均
y+=point[j].y;
count++;
}
}
mean[i].x=x/count;//采用算数平均重新计算簇中心之和
mean[i].y=y/count;
}
}
float geterror(){//计算所有点与其簇中心的距离之和
float distancesum=0;
for(int i=0;i<K;i++){//针对每个簇,计算他与其簇中心的距离之和
float x=0,y=0;
int count=0;
for(int j=0;j<N;j++){
if(center[j]==i){//检查数据集每个簇是否属于当前簇
distancesum+=getdistance(mean[i],point[j]);
}
}
}
return distancesum;
}
int main(){
int number=0;
float temp1,temp2;
mean[0].x=point[0].x;
mean[0].y=point[0].y;
mean[1].x=point[3].x;
mean[1].y=point[3].y;
mean[2].x=point[6].x;
mean[2].y=point[6].y;
cluster();
number++;
temp1=geterror();
printf("the error1 is:%f\n",temp1);
getmean(center);
cluster();
number++;
temp2=geterror();
printf("the error2 is:%f\n",temp2);
while(number<1000&&fabs(temp1-temp2)>1e-5){
temp1=temp2;
getmean(center);
cluster();
temp2=geterror();
number++;
printf("the error%d is:%f\n",number,temp2);
}
printf("the total number of cluster is:%d\n",number);
return 0;
}