题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

#include <stdio.h>
#include<string.h>
struct err {
    char arr[20];
    int h;
    int count;
};
int main() {
    //思路
    //1.创建std存放文件名,行数,出现次数 结构体
    struct err std1[101];
    memset(std1, 0, sizeof(std1));
    int i = 0;
    int j = 0;
    char std2[101] = { 0 };
    int num2 = 0;
    int n = 0;
    while (scanf("%s %d", std2, &num2) != EOF) {
        int len = strlen(std2);
        for (j = 0; j < 16; j++) { //16个有效字符
            if (std2[len - j - 1] == '\\') { //识别倒数第一个斜杠
                break;
            }
            else {
                std1[i].arr[j] = std2[len - j -
                    1];//有效字符以逆序的形式存放在std1中
            }
        }
        std1[i].h = num2;//存放行数
        std1[i].count = 1;//存放出现次数
        i++;
        n++;//共有几组数据
    }
    int n2 = n;
    //2.操作std,对重复出现错误记录在第一次出现的位置++并且将当前位置置空;
    for (i = 0; i < n; i++) {
        if (std1[i].arr[0] == 0)
            continue;
        for (j = 1 + i; j < n; j++) {
            if (strcmp(std1[i].arr, std1[j].arr) == 0 && std1[i].h == std1[j].h) {
                memset(&std1[j], 0, sizeof(std1[j]));
                std1[i].count++;
                n2--;
            }
        }
    }
    //3.输出:循环队列实现输出;
    int front = 0;
    int rear = 0;
    struct err result[9];
    memset(result, 0, sizeof(result));
    i = 0;
    while (i < n)
    {
        if ((std1[i].count) != 0)
        {
            if ((rear + 1) % 9 != front)
            {
                memcpy(&result[rear], &std1[i], sizeof(std1[0]));
                rear=(rear+1)%9;
            }
            else
            {
                front = (front + 1) % 9;
                memcpy(&result[rear], &std1[i], sizeof(std1[0]));
                rear = (rear + 1) % 9;
            }
        }
            i++;
    }
    while (1)
    {
        if (front == rear)
        {
            break;
        }
        else
        {
            int len = strlen(result[front].arr);
            for (i = 0; i < len; i++) 
            {
                printf("%c", result[front].arr[len - 1 - i]);
            }
            printf(" %d", result[front].h);
            printf(" %d\n", result[front].count);
            front = (front + 1) % 9;
        }
    }
    return 0;
}

全部评论

相关推荐

07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务