MyGitHub
终于~奔溃了无数次后,看到这个结果 ,感动得不要不要的::>_<::
题目在这里
题目简述:该题可大致分为 输入链表 -> 链表节点反转 -> 两个步骤
输入链表:对于每个输入的值都要附带输入 存储该值的空间地址 和 下一个节点的地址,本来想用
比较方便→_→但是只会
链表节点反转:head->1->2->3->4->5->..->tail 我的方法是,三个指针 temp 、 q和p ,temp作为头指针,*p指向待反转的指针的前一位,假若k=4,从4开始 temp->4->1->2->3->5 , temp->4->3->1->2->5... temp->4->3->2->1->5,然后temp初始化,以1作为头指针的位置,重复上述循环。
链表输出:打代码时,输出一直忘了更新一下,输出应该为当前节点的下一个节点地址→_→ 并且输出的截止点不应该为p ->afterstr==-1 而是p ->next!= NULL (坑了一脸的血泪)
...然后就好了
注意:输入的节点可能不在链表上,因此要加一个计数器 count~
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct MyStruct //建立节点相关信息数据结构
{
int afterstr;
int theadr;
int num;
MyStruct *next;
};
struct list //创建链表
{
int afterstr;
int num;
}a[100001];
int main()
{
int count = 0, times;
int adr, n, k;
int i, j;
int theastr, afterstr, num;
scanf("%d %d %d", &adr, &n, &k); //输入头结点地址
if (adr == -1)
{
printf("-1\n");
return 0;
}
for (i = 0; i < n; i++)
{
scanf("%d %d %d", &theastr, &num, &afterstr); //存入数组中
a[theastr].afterstr = afterstr;
a[theastr].num = num;
}
MyStruct *head, *pnew, *tail, *p, *q, *temp;
head = (MyStruct*)malloc(sizeof(MyStruct));
head->next = NULL;
tail = head;
for (theastr = adr; theastr != -1; theastr = a[theastr].afterstr) //存入连表中
{
pnew = (MyStruct*)malloc(sizeof(MyStruct)); //开辟新节点
pnew->num = a[theastr].num;
pnew->afterstr = a[theastr].afterstr;
pnew->theadr = theastr;
tail->next = pnew;
tail = pnew;
count += 1;
}
times = count / k;
p = head->next;
temp = head;
for (i = 0; i < times; i++) //链表反转
{
for (j = 0; j < k - 1; j++)
{
q =p->next;
p->next = q->next;
q->next =temp->next;
temp->next = q;
}
temp = p; //重置头指针地址
if (p->next == NULL)break;
p = p->next;
}
for (p = head->next; p ->next!= NULL; p = p->next) //打印链表
{
printf("%05d %d %05d\n", p->theadr,p->num,p->next->theadr);
}
printf("%05d %d -1\n", p->theadr, p->num);
return 0;
}
顺便,pta两题:
#include<stdio.h>
class Date
{
public:
int year;
int month;
int day;
};
int main()
{
int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
Date data;
int count;
while (scanf("%d%d%d",&data.year,&data.month,&data.day)!=EOF)
{
count = 0;
if (data.year == 0 && data.month == 0 && data.day == 0)
break;
if (data.year % 4 == 0 )
{
if (data.year % 100 == 0)
{
if (data.year % 400 == 0)
{
monthday[2] = 29;
}
}
else
{
monthday[2] = 29;
}
}
for (int i = 0; i < data.month; i++)
{
count += monthday[i];
}
count += data.day;
printf("%d\n", count);
}
return 0;
}
#include<stdio.h>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 1000000
class dormistry {
public:
string name;
int height;
int weight;
};
int dors[MAX] = { 0 };
dormistry dor[MAX];
int dornum[MAX];
int main()
{
int i,j=0,n;
string names;
int nums;
int heights;
int weights;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
cin >> nums >> names >> heights >> weights;
getchar();
//scanf_s("%d %s %d %d", &nums,&names, &heights, &weights);
if (dors[nums] == 0)
{
dors[nums] = 1;
dor[nums].name = names;
dor[nums].height = heights;
dor[nums].weight = weights;
dornum[j++] = nums;
}
else if (dors[nums] == 1)
{
if (heights > dor[nums].height)
{
dor[nums].name = names;
dor[nums].height = heights;
dor[nums].weight = weights;
}
}
}
sort(dornum, dornum + j );
for (i = 0; i < j; i++)
{
printf("%06d ",dornum[i]);
cout<<dor[dornum[i]].name<< " " << dor[dornum[i]].height<< " " << dor[dornum[i]].weight<<endl;
}
return 0;
}