#include <stdio.h> struct staff { char name[10]; int a[3]; }staff[10]; int main() { int N;scanf("%d",&N); struct staff staff[10]; int i,sum[N]; for(i=0;i<N;i++) { scanf("%s %d %d %d",staff[i].name,&staff[i].a[0],&staff[i].a[1],&staff[i].a[2]); } for(i=0;i<N;i++) { sum[i]=0; sum[i]=staff[i].a[0]+staff[i].a[1]-staff[i].a[2]; printf("%s %.2f\n",staff[i].name,(float)sum[i]); } return 0; }请问这个程序为什么在pta里是错的
时间: 2023-06-17 17:09:12 浏览: 30
这个程序在 PTA 上会出错,是因为题目中要求输出的是保留一位小数的浮点数,但是你在输出的时候使用了 `%.2f` 这个格式化字符串,这会输出保留两位小数的浮点数。
正确的输出格式应该是 `%.1f`,即保留一位小数,你可以将代码中的 `printf` 语句修改为下面这样:
```c
printf("%s %.1f\n",staff[i].name,(float)sum[i]);
```
这样就可以通过 PTA 的测试了。
相关问题
#include<stdio.h> #include<stdlib.h> #include<string.h> #define LEN sizeof(struct staff) struct staff { int num; char name[20]; char station[10]; int score; struct staff *next; }; int main() { struct staff *h,*p,*q; int num; char s[10]; h=(struct staff*)malloc(LEN); h->next=NULL;//建立带头结点的空链表 scanf("%d",&num); q=h; while(num!=0) { p=(struct stu*)malloc(LEN); p->num=num; scanf("%s %s %d",p->name,p->station,&p->score); p->next=NULL; q->next=p; q=p; scanf("%d",&num); }//输入数据到0截止 scanf("%s",s); p=h->next; while(p!=NULL) { if(strcmp(p->station,s)!=0) { printf("%d %s %s %d\n",p->num,p->name,p->station,p->score); p=p->next;} } } 修改程序
这段代码存在一些问题,包括变量类型不匹配、指针类型错误、结构体成员变量的访问方式错误等,请参考下面的修改建议:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct staff {
int num;
char name[20];
char station[10];
int score;
struct staff* next;
};
int main() {
struct staff* head, * p, * q;
int num, score;
char name[20], station[10], s[10];
head = (struct staff*)malloc(sizeof(struct staff)); // 建立带头结点的空链表
head->next = NULL;
scanf("%d", &num);
q = head;
while (num != 0) {
p = (struct staff*)malloc(sizeof(struct staff));
p->num = num;
scanf("%s %s %d", p->name, p->station, &score);
p->score = score;
p->next = NULL;
q->next = p;
q = p;
scanf("%d", &num);
} // 输入数据到0截止
scanf("%s", s);
p = head->next;
while (p != NULL) {
if (strcmp(p->station, s) != 0) {
printf("%d %s %s %d\n", p->num, p->name, p->station, p->score);
}
p = p->next;
}
// 释放内存
p = head;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
return 0;
}
```
主要的修改包括:
1. 在输入员工信息时,将 `p->score` 的地址传入 `scanf` 函数,以便正确读取员工分数。
2. 在输入员工信息时,将 `name` 和 `station` 数组作为参数传入 `scanf` 函数,以便正确读取员工姓名和职位。
3. 在 `main` 函数中添加释放内存的代码,以防止内存泄漏。
#include <stdio.h> struct staff { int num; char name[15]; char sex; struct date {int year; int month; int day; }birthday; float salary; char address[30]; }person1={00003,"WuHong","M",{1998,8,20},2480,"清城名苑5-3-801"}; int main() { printf("编号:%d\n姓名:%s\n性别:%c\n出生日期:%d%d%d\n工资:%f\n地址:%s", person1.num,person1.name,person1.sex,person1.birthday.year,person1.birthday.month,person1.birthday.day, person1.salary,person1.address); }有什么错
代码中有一个问题,即结构体内的嵌套结构体 `date` 应该在结构体 `staff` 外面定义。这是因为 `struct date` 是 `struct staff` 中的一个成员,而不是一个独立的结构体。
以下是修改后的代码:
```
#include <stdio.h>
struct date {
int year;
int month;
int day;
};
struct staff {
int num;
char name[15];
char sex;
struct date birthday;
float salary;
char address[30];
} person1 = {00003, "WuHong", "M", {1998, 8, 20}, 2480, "清城名苑5-3-801"};
int main() {
printf("编号:%d\n姓名:%s\n性别:%c\n出生日期:%d%d%d\n工资:%f\n地址:%s",
person1.num, person1.name, person1.sex,
person1.birthday.year, person1.birthday.month, person1.birthday.day,
person1.salary, person1.address);
return 0;
}
```
除此之外,代码还需要添加 `return 0;` 语句以表示程序正常结束。