• 课堂作业二 PAT1025 反转链表


    MyGitHub

    终于~奔溃了无数次后,看到这个结果 ,感动得不要不要的::>_<::

    题目在这里

    题目简述:该题可大致分为 输入链表 -> 链表节点反转 -> 两个步骤

    输入链表:对于每个输入的值都要附带输入 存储该值的空间地址 和 下一个节点的地址,本来想用比较方便→_→但是只会不太会用,只能老老实实地用数组下标做地址名,建立结构体以存储附带数据。
    输入即简化为对于地址为adr的节点,存储进a[adr]中。完成数组输入后,以第一个地址为起点,a[adr].afterstr 为线索,一直找到某个节点使其a[adr].afterstr==-1为止,循环开辟链表空间并输入。

    链表节点反转: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
    "); 
            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
    ", p->theadr,p->num,p->next->theadr);
    
          }
          printf("%05d %d -1
    ", p->theadr, p->num);
          return 0;
        }
    

    顺便,pta两题:

    5-1

        #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
    ", count);
            }
            return 0;
        }
    

    5-2

        #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;
        }
  • 相关阅读:
    白话机器学习
    Intersecting Lines POJ
    Segments POJ
    Toy Storage POJ
    TOYS POJ
    2019CCPC秦皇岛赛区1004 Decimal
    Django 基本使用
    HTML页面布局
    微擎上传视频,音频,图片提示格式不支持
    微擎应用名称图标的修改
  • 原文地址:https://www.cnblogs.com/mingtime/p/5511059.html
Copyright © 2020-2023  润新知