[北大OJ](1002)487-3279

题目描述:

img

题目样本:

img

待优化思路:

  • 创建一条链表将输入的值存放起来
  • 从链表中逐次取出值进行去除Q、Z、以及‘-’字符
  • 从链表中逐次取出值进行字符转换,字母字符转换成题目要求的数字
  • 从链表中逐次取出值进行遍历整个链表,相同的值进行计数然后删除结点
  • 每次计数的值创建一个新的链表串起来,然后进行以输入值进行排序,排序中不改变结点位置,只改变结点的值

待优化代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
    char a[20];
    struct node* next;
};
struct tongji
{
    char name[20];
    int num;
    struct tongji* next;
};

struct node* head, * NEW, * p, * s, * s1;
struct tongji* new1, * Phead, * p1, * s2, * s3;


void newnode();
void bijiao(char[]);

int main()
{
    int a;
    scanf("%d", &a);
    int c = 0;
    head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    Phead = (struct tongji*)malloc(sizeof(struct tongji));
    Phead->next = NULL;
    do
    {
        newnode();
        a--;
    } while (a);
    p = head->next;
    printf("\n");
    while (p != NULL)
    {
        bijiao(p->a);
        p = p->next;
    }

    printf("\n");
    
    int t;
    p = head->next;
    while (p != NULL)
    {
        s1 = p;
        s = p->next;
        t = 1;
        while (s != NULL)
        {
            if (strcmp(p->a, s->a) == 0)
            {
                s1->next = s->next;
                free(s);
                t++;
                s = s1->next;
                continue;
            }
            s1 = s1->next;
            s = s->next;
        }
        if (t > 1)
        {
            new1 = (struct tongji*)malloc(sizeof(struct tongji));
            strcpy(new1->name, p->a);
            new1->num = t;
            new1->next = Phead->next;
            Phead->next = new1;
        }
        p = p->next;
    }
    if (Phead->next == NULL)
    {
        printf("未找到重复数据");
    }
    p1 = Phead->next;
    while (p1 != NULL)
    {
        s2 = p1->next;
        if (s2 != NULL)
        {
            int tempname = atoi(p1->name);
            if (tempname > atoi(s2->name))
            {
                char temp[20];
                strcpy(temp, p1->name);
                int tempnum = p1->num;
                strcpy(p1->name, s2->name);
                p1->num = s2->num;
                strcpy(s2->name, temp);
                s2->num = tempnum;
            }
        }
        p1 = p1->next;
    }
    p1 = Phead->next;
    while (p1 != NULL)
    {
        printf("%c%c%c-%c%c%c%c %d\n", p1->name[0], p1->name[1], p1->name[2], p1->name[3], p1->name[4], p1->name[5], p1->name[6], p1->num);
        p1 = p1->next;
    }

    system("pause");
    return 0;


}
void newnode()
{
    NEW = (struct node*)malloc(sizeof(struct node));
    scanf("%s", NEW->a);
    p = head;
    if (p->next == NULL)
    {
        p->next = NEW;
        NEW->next = NULL;
    }
    else
    {
        while (p->next != NULL)
        {
            p = p->next;
        }
        p->next = NEW;
        NEW->next = NULL;
    }
}
void bijiao(char a[])
{
    int c = strlen(a);
    int i = 0, j = 0;
    for ( i = 0, j = 0; i < c; i++)
    {
    
        if (a[i] == 'Q' || a[i] == 'Z' || a[i] == '-')
        {
            continue;
        }
        a[j] = a[i];
        j++;
    }
    a[j] = '\0';
    for (i = 0, j = 0; i < c; i++)
    {
        if (a[i] >= 'A' && a[i] <= 'C')
        {
            a[i] = '2';
        }
        if (a[i] >= 'D' && a[i] <= 'F')
        {
            a[i] = '3';
        }
        if (a[i] >= 'G' && a[i] <= 'I')
        {
            a[i] = '4';
        }
        if (a[i] >= 'J' && a[i] <= 'L')
        {
            a[i] = '5';
        }
        if (a[i] >= 'M' && a[i] <= 'O')
        {
            a[i] = '6';
        }
        if (a[i] == 'P' || a[i] == 'R' || a[i] == 'S')
        {
            a[i] = '7';
        }
        if (a[i] == 'T' || a[i] == 'U' || a[i] == 'V')
        {
            a[i] = '8';
        }
        if (a[i] == 'W' || a[i] == 'X' || a[i] == 'Y')
        {
            a[i] = '9';
        }
    }
}

VS2019环境下编译正确,裁定错误

本文链接:

https://nullcode.fun/86.html
1 + 1 =
快来做第一个评论的人吧~