OJ 题目来源:https://turingjudge.com
作业->编程基础练习营4->1026统计学生信息(使用动态链表完成)
主要若我将结构体中 score 的类型改为整数就内存溢出,改为浮点数就错误,只能用字符串才可以;想不通为什么这样会溢出,浮点数还直接0分了;可否有大神能指点一二?非常感谢
MLE代码:
#include <iostream>
#include <string.h>
using namespace std;
struct Node {
char sex;
int age;
int score;
struct Node *pre;
struct Node *nxt;
char number[30];
char name[50];
char address[50];
};
struct Node *head;
struct Node *tail;
void inputNode(struct Node t);
void outputNode(struct Node *n);
int main() {
head = new(struct Node);
head->pre = NULL;
head->nxt = NULL;
cin >> head->number;
if (strcmp( head->number, "end")==0 ) {
cout << endl;
return 0;
}
cin >> head->name >> head->sex >> head->age >> head->score >> head->address;
tail = head;
while (1) {
struct Node t;
cin >> t.number;
if (strcmp(t.number, "end") == 0) { break; }
cin >> t.name >> t.sex >> t.age >> t.score >> t.address;
inputNode(t);
}
struct Node *t = tail;
while (t != NULL) {
outputNode(t);
t = t->pre;
}
return 0;
}
void outputNode(struct Node *n) {
cout << n->number << ' ' << n->name << ' ' << n->sex << ' ' << n->age << ' ' << n->score << ' ' << n->address << endl;
}
void inputNode(struct Node t) {
struct Node *n = new(struct Node);
strcpy(n->number, t.number);
strcpy(n->name, t.name);
n->sex=t.sex;
n->age=t.age;
n->score=t.score;
strcpy(n->address, t.address);
tail->nxt = n;
n->nxt = NULL;
n->pre = tail;
tail = n;
}
综上,啥破题目
ps:
建议:1.倒序输出可以学习一下,链表的头插法,这样只需要单链表就可以了