首页 > 试题广场 >

简单错误记录

[编程题]简单错误记录
  • 热度指数:362434 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}在本题中,我们需要处理文件报错信息,其由出错文件的文件路径和错误行号组成。

\hspace{15pt}文件路径的前三个字母为大写字母 \texttt{A-Z} 、冒号 \texttt{ 和反斜杠 \texttt{ ,代表盘符;随后是若干由小写字母构成的字符串,代表文件夹名,彼此使用单个反斜杠间隔。路径的最后一个反斜杠后是文件名。
\hspace{15pt}我们只在乎文件名(即去掉除了文件名以外的全部信息),且至多保留文件名的最后 16 个字符。

\hspace{15pt}随后,我们需要统计相同的报错信息:
\hspace{23pt}\bullet\,如果两条报错信息保留后 16 个字符后的文件名相同,且行号相同,则视为同一个报错;

\hspace{15pt}相同的报错信息以第一次出现的时间为准,至多输出最后 8 条记录。

输入描述:
\hspace{15pt}本题将会给出 1 \leqq T \leqq 100 条报错信息,确切数字未知,您需要一直读入直到文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每条报错信息描述如下:

\hspace{15pt}在一行上先输入一个长度为 1 \leqq {\rm length}(x) \leqq 100 的字符串 x 代表文件路径;随后,在同一行输入一个整数 y \left( 1 \leqq y \leqq 1000 \right) 代表行号。
\hspace{15pt}文件路径的格式如题干所述,保证文件名不为空。


输出描述:
\hspace{15pt}至多八行,每行先输出一个长度为 1 \leqq {\rm length}(s) \leqq 16 的字符串 s ,代表文件名;随后,在同一行输出错误行号、报错次数。
示例1

输入

D:\oblemsinnowcoder 12
D:\nowcoderproblemsinnowcoder 12
D:\nowcoder\problemsinnowcoder 13
D:\oj\problemsinnowcoder 13

输出

oblemsinnowcoder 12 2
oblemsinnowcoder 13 2

说明

\hspace{15pt}在这个样例中,这四条报错信息去除文件路径后,由于文件名长度均超过 16 个字符,故我们只保留最后 16 个字符,得到的文件名均为 \texttt{ 。所以,我们将它们看作同一个文件,按照报错行号划分即可。
示例2

输入

A:\aa 1
B:\b 1
C:\c 1
D:\d 1
E:\e 1
F:\f 1
G:\g 1
H:\h 1
I:\i 1
A:\aa 1

输出

b 1 1
c 1 1
d 1 1
e 1 1
f 1 1
g 1 1
h 1 1
i 1 1

说明

\hspace{15pt}在这个样例中,第一、十条报错信息完全相同,但是我们以其第一次出现的顺序为准,在输出最后 8 条记录时,不包含这一报错。
示例3

输入

D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
E:\je\rzuwnjvnuz 633
C:\km\tgjwpb\gy\atl 637
F:\weioj\hadd\connsh\rwyfvzsopsuiqjnr 647
E:\ns\mfwj\wqkoki\eez 648
D:\cfmwafhhgeyawnool 649
E:\czt\opwip\osnll\c 637
G:\nt\f 633
F:\fop\ywzqaop 631
F:\yay\jc\ywzqaop 631
D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645

输出

rzuwnjvnuz 633 1
atl 637 1
rwyfvzsopsuiqjnr 647 1
eez 648 1
fmwafhhgeyawnool 649 1
c 637 1
f 633 1
ywzqaop 631 2
#include <stdio.h>
#include "string.h"
int main() {
    int a, num[100][2] = {0};
    char str[101] = "\0";
    char tot[100][17] = {"\0"};
    while (scanf("%s %d", str, &a) != EOF) 
    {
        int i = 0;
        for(; i < 16; i++)
        {
            if(str[strlen(str) - 1 - i] != '\\')
                continue;
            else
                break;
        }
        int j = 0;
        while(i-- > 0)
            str[j++] = str[strlen(str) - 1 - i];
        str[j] = '\0';
        int k = 0;
        while(num[k][0] != 0)
        {
            if(strcmp(str, tot[k]) == 0 && a == num[k][0])
            {
                num[k][1]++;
                a = 0;
                break;
            }
            k++;
        }
        if(a != 0) 
        {
            strcpy(tot[k], str);
            num[k][0] = a;
            num[k][1]++;
        }       
    }
    int i = 0;
    for(; i < 100; i++)
    {
        if(num[i][0] != 0)
            continue;
        else
            break;
    }
    int j = i - 8;
    if(j < 0)
    {
        j = 0;
        while(num[j][0] != 0)
        {
            printf("%s", tot[j]);
            printf(" %d ", num[j][0]);
            printf("%d\n", num[j][1]);
            j++;
        }
        return 0;
    }   
    for(int i = 0; i < 8; i++)
    {
        printf("%s", tot[j + i]);
        printf(" %d ", num[j + i][0]);
        printf("%d\n", num[j + i][1]);        
    }
    return 0;
}


发表于 2025-07-09 00:58:50 回复(0)
#include <stdio.h>
#include "string.h"
typedef struct  record {
    char filename[100];
    int codehangshu;
    int shumu;
} errrecord;
int main( )
{
    char str[100]={0},str1[100]={0};
    int a,i,len,j,j1,i1=0,u,u1,fg[500]={0},i3=0,i4,cnt=0,u2;
    errrecord jiru[1000]={0};
    while(scanf("%s%d",str,&a)!=EOF)
    {  
        len=strlen(str);
        jiru[i1].codehangshu=a;
        jiru[i1].shumu=1;
        for(i=0;i<=16;i++)
        {
           if((int)str[len-1-i]==92)
            {
                    for(j=i,j1=0;j>=1;j--)
                    {
                        jiru[i1].filename[j1]=str[len-j];
                        j1++;
                    }
                    goto label;
            }
        }
        if(jiru[i1].filename[0]=='\0')
        {
             for(j=16,j1=0;j>=1;j--)
                    {
                        jiru[i1].filename[j1]=str[len-j];
                        j1++;
                    }
             goto label;      
        }
        label: ;
        i1++;
    }
    for(u=0;u<=i1-2;u++)
    {
        for(u1=u+1;u1<=i1-1;u1++)
        {
            if(strcmp(jiru[u].filename,jiru[u1].filename)==0&&jiru[u].codehangshu==jiru[u1].codehangshu)
            {
                   fg[u1]=1;
                   jiru[u].shumu++;
            }
        }
    }
    for(u=i1-1;u>=0;u--)
    {  
        if(fg[u]!=1)
        {
            cnt++;
            if(cnt==8||u==0)
            {
                u2=u;
                break;
            }
        }
    }
   for(u=u2;u<=i1-1;u++)
    {  
       if(fg[u]==1)
       {
           continue;
       }
       else
       {
        printf("%s %d %d\n",jiru[u].filename,jiru[u].codehangshu,jiru[u].shumu);
        cnt++;
       }
   }
}
发表于 2024-08-08 12:28:56 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct recode 
{
	char str[17];//最后16个字符的文件名
	int num;//行号
	int count;//记错次数
}recode;

void getLastFileName(char path[],char tmp[]) 
{
	char* lastSlash = strrchr(path, '\\'); // 找到最后一个斜杠字符
	char fileName[100] = ""; // 存储文件名,初始化为空字符串

	if (lastSlash != NULL) { // 如果找到了斜杠
		strcpy(fileName, lastSlash+1); // 复制斜杠后面的字符串到fileName
	}
	else {
		strcpy(fileName, path); // 如果没有斜杠,直接复制整个路径到fileName
	}
	int len = strlen(fileName);
	if (len <= 16)
		strcpy(tmp,fileName);
	else
		strcpy(tmp,fileName + len - 16);
}

int main()
{
	recode* list[100] = { 0 };
	char path[100] = { 0 };//路径
	char filename[100] = { 0 };//文件名
	int i = 0;//记录次数
	int j = 0;
	int flag = 0;//重复标志
	int n = 0;//行数
	//初始化字符串
	for (i = 0; i < 100; i++)
	{
		list[i] = (recode*)malloc(sizeof(recode));
		list[i]->num = 0;
		list[i]->count = 0;
		memset(list[i]->str, 0, sizeof(list[i]->str));
	}
	i = 0;
	while (scanf("%s %d", path, &n)==2)//输入:路径和行数分开
	{
		flag = 0;//初始化重复标志
		getLastFileName(path, filename);//记录文件名
		for (j = 0; j < i; j++)
		{
			//重复:文件名相同且错误行数相同
			if (strcmp(filename, list[j]->str) == 0 && list[j]->num ==n)
			{
				list[j]->count++;
				flag = 1;
				break;
			}
		}
		if (flag == 0)//不重复
		{
			strcpy(list[i]->str, filename);
			list[i]->num = n;
			list[i]->count=1;
			i++;//记录次数增加
		}
	}

	//错误记录少于八条则正常输出,否则输出最后八条记录
	if (i < 8)
	{
		for (j = 0; j < i; j++)
		{
			printf("%s %d %d\n", list[j]->str, list[j]->num, list[j]->count);
		}
	}
	else
	{
		for (j = i - 8; j < i; j++)
		{
			printf("%s %d %d\n", list[j]->str, list[j]->num, list[j]->count);
		}
	}

	return 0;
}

编辑于 2023-12-27 11:11:07 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define       N    100
typedef struct record
{
    char str[17];
    char row[4];
    int num;
}record;
int main()
{
    record *note[N],temp;
    char *str[N];
    int i=-1,j,flag1,flag2,m,n,cnt=0,p,q;
    do
    {
        i++;
        str[i]=(char*)malloc(sizeof(char)*100);
        gets(str[i]);
    }while(*str[i]!='\0');
    for(j=0;j<i;j++)
    {
        flag1=0;flag2=0;m=0;p=0;
        while(*(str[j]+m)!='\0')
        {
            if(*(str[j]+m)=='\\')
            {
                flag1=m;
            }
            else if(*(str[j]+m)==' ')
            {
                flag2=m;
                break;
            }
            m++;
        }
        if((flag2-flag1-1)>16)
        {
            n=flag2-16;
            if(j==0)
            {
                note[cnt]=(record*)malloc(sizeof(record));
                while(*(str[j]+n)!=' ')
                {
                    note[cnt]->str[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    note[cnt]->row[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->row[p]='\0';
                note[cnt]->num=1;
                cnt++;
            }
            else if(j>0)
            {
                while(*(str[j]+n)!=' ')
                {
                    temp.str[p++]=*(str[j]+n);
                    n++;
                }
                temp.str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    temp.row[p++]=*(str[j]+n);
                    n++;
                }
                temp.row[p]='\0';
                for(q=0;q<cnt;q++)
                {
                    if(strcmp(note[q]->str,temp.str)==0&&
                       strcmp(note[q]->row,temp.row)==0)
                    {
                        note[q]->num++;
                        break;
                    }
                }
                if(q==cnt)
                {
                    note[cnt]=(record*)malloc(sizeof(record));
                    strcpy(note[cnt]->str,temp.str);
                    strcpy(note[cnt]->row,temp.row);
                    note[cnt]->num=1;
                    cnt++;
                }
            }
        }
        else if((flag2-flag1-1)<=16)
        {
            n=flag1+1;
            if(j==0)
            {
                note[cnt]=(record*)malloc(sizeof(record));
                while(*(str[j]+n)!=' ')
                {
                    note[cnt]->str[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    note[cnt]->row[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->row[p]='\0';
                note[cnt]->num=1;
                cnt++;
            }
            else if(j>0)
            {
                while(*(str[j]+n)!=' ')
                {
                    temp.str[p++]=*(str[j]+n);
                    n++;
                }
                temp.str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    temp.row[p++]=*(str[j]+n);
                    n++;
                }
                temp.row[p]='\0';
                for(q=0;q<cnt;q++)
                {
                    if(strcmp(note[q]->str,temp.str)==0&&
                       strcmp(note[q]->row,temp.row)==0)
                    {
                        note[q]->num++;
                        break;
                    }
                }
                if(q==cnt)
                {
                    note[cnt]=(record*)malloc(sizeof(record));
                    strcpy(note[cnt]->str,temp.str);
                    strcpy(note[cnt]->row,temp.row);
                    note[cnt]->num=1;
                    cnt++;
                }
            }           
        }
    }
    if(cnt<=8)
    {
        for(n=0;n<cnt;n++)
        {
            printf("%s %s %d\n",note[n]->str,note[n]->row,note[n]->num);
        }
    }
    else
    {
        for(n=cnt-8;n<cnt;n++)
        {
            printf("%s %s %d\n",note[n]->str,note[n]->row,note[n]->num);
        }
    }
    return 0;
}

//耗时巨大,虽然代码很low,但一次就通过了

发表于 2022-04-15 19:34:05 回复(0)
可以帮我看看下面这段代码错误在哪吗,与正确代码的区别只在于字符串赋值,我的是 char *name;name= str+l-16;指针赋值。换成    char name[];; strcpy(name, str+l-16);这个就正确了,为什么呢,直接指针赋值的错误在哪呢?


#include<stdio.h>
#include<string.h>
int main()
{
    struct rec
    {
        int row;
        char *s;
        int num;

    };
    struct rec r[100];
    int  q = -1;
    int j=0;
    int line = 0;
    char str[100] ;
    
    while (scanf("%s%d",str,&line) != EOF)
    {
        char *name;
        int k = 0;
        int l = strlen(str);
        int i = 0;
      for(i=l-1;i>=0;i--)
        {
            if(str[i] == '\\')
               break;
        }
        //从i+1 下一位开始复制,还有考虑长度不能超过16
        if(i+1 < l -16)
        {
            name= (str+l-16);
        }else {
         name= (str+i+1);
        }
        
            int flag = 0;
            for (int c = 0; c <= q; c++)
            {
                if (r[c].s==name && r[c].row == line)
                {
                    r[c].num++;
                    flag =1;
                    break;
                }
             
            }
        
   
    if (flag==0)
    {
       q++;
      r[q].s= name;
        r[q].row =line;
        r[q].num=1;
    }
}

    if(q<8)
    {
        for(int i=0;i<=q;i++)
        {
            printf("%s %d %d \n",r[i].s,r[i].row,r[i].num);
        }
    }else {
        for(int i=q-7;i<=q;i++)
        {
            printf("%s %d %d \n",r[i].s,r[i].row,r[i].num);
        }
    }
     
    return 0;
     
    
}
发表于 2022-03-09 19:26:41 回复(0)
按要求来即可。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
    char str[17];
    int row;
    
    int cnt;
}T_rec;

typedef struct{
    T_rec rec[100];
    int pWrite;
}T;

void rec_one(T *p_rec,char *str,int row)
{
    int i,len,flag = 1;
    char *p_e,*p_s;
    char file_name[17];
    
    len = strlen(str);
    
    p_e = str + len - 1;
    
    
    p_s = p_e;
    
    while(*p_s != '\\') p_s--;
    len = strlen(p_s + 1);
    if(len > 16)
    {
        strcpy(file_name, p_s + 1 + len - 16);
    }
    else
    {
        strcpy(file_name, p_s + 1);
    }
    
    for(i = 0;i < p_rec->pWrite; i++)
    {
        if(strcmp(file_name,p_rec->rec[i].str) == 0 && row == p_rec->rec[i].row)
        {
            p_rec->rec[i].cnt++;
            flag = 0;
            break;
        }
    }
    
    if(flag)
    {
        strcpy(p_rec->rec[p_rec->pWrite].str, file_name);
        p_rec->rec[p_rec->pWrite].cnt = 1;
        p_rec->rec[p_rec->pWrite].row = row;
        p_rec->pWrite++;
    }
    
}

int main()
{
    T rec;
    char input[101];
    int row;
    int i,start;
    
    memset((void *)&rec,0,sizeof(rec));
    
    while(scanf("%s %d",input,&row) != EOF)
    {
        rec_one(&rec,input,row);
    }
    
    if(rec.pWrite >= 8)
    {
        start = rec.pWrite - 8;
    }
    else
    {
        start = 0;
    }
    
    for(i = start;i < rec.pWrite; i++)
    {
        printf("%s %d %d\n",rec.rec[i].str,rec.rec[i].row,rec.rec[i].cnt);
    }
    
    return 0;
}


发表于 2021-12-25 11:58:52 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
	char error_name[17];
	int  inumber;
	int error_count;
}ErrorInfo;

ErrorInfo err_info[100] = {0};
int i_errinfo_num = 0;

int main()
{
	char str[100] = {0};
	int iHangNum = 0, i = 0, j = 0;
	while(scanf("%s %d", str, &iHangNum) != EOF)
	{
		int is_find = 0;
		char *ptrTag = strrchr(str, '\\');
		
		if(ptrTag == NULL || ++ptrTag == NULL )
		{
			continue;
		}
		
		int istrLen = strlen(ptrTag);
		// printf("ilen:%d\n", istrLen);
		
		if(istrLen > 16)
		{
			ptrTag += (istrLen - 16);
		}
		
		// printf("ptrTag:%s\n", ptrTag);
		
		for(i = 0; i < i_errinfo_num; i++)
		{
			if(strcmp(ptrTag, err_info[i].error_name) == 0 && iHangNum == err_info[i].inumber)
			{
				err_info[i].error_count++;
				is_find = 1;
				break;
			}
		}
		
		if(is_find == 0)
		{
			strcpy(err_info[i_errinfo_num].error_name, ptrTag);
			err_info[i_errinfo_num].inumber = iHangNum;
			err_info[i_errinfo_num].error_count = 1;
			
			i_errinfo_num++;
		}
	}
	
	// printf("i_errinfo_num:%d\n", i_errinfo_num);
	
	if(i_errinfo_num > 8)
	{
		j = i_errinfo_num - 8;
	}
	
	for(i = j; i < i_errinfo_num; i++)
	{
		printf("%s %d %d\n", err_info[i].error_name, err_info[i].inumber, err_info[i].error_count);
	}	
	
	return 0;
}

发表于 2021-12-06 12:31:38 回复(2)
这题大坑,必须全记录,然后输出最后8条,而不能把前面的删掉。假设删掉了a记录,后面又出现a记录,你会追加,但实际应该算重复的。
发表于 2021-11-17 20:57:57 回复(0)
#include <stdio.h>
#include <string.h>
#define MAX 50
#define OUTMAX 8
#define FILENAMELEN 16

int main(){
    char record_name[MAX][FILENAME_MAX] = {0};
    int record_line[MAX] = {0};
    int record_freq[MAX] = {0};

    int next = 0;
    
    char file[1000];
    int line;

    int total = 0;

    while (scanf("%s %d", file, &line) == 2){
        int file_len = strlen(file);
        int count = 1;
        char recorded_filename[FILENAMELEN + 1];
        recorded_filename[FILENAMELEN] = '\000';
        while (file[file_len-count] != '\\' && count <= FILENAMELEN){
            recorded_filename[FILENAMELEN - count] = file[file_len - count];
            count++;
        }
        for (int i = 0; i < count; i++){
            recorded_filename[i] = recorded_filename[FILENAMELEN + 1 - count + i];
        }


        int exist = 0;
        for (int i = 0; i < MAX; i++){
            if (strcmp(record_name[i], recorded_filename) == 0 && record_line[i] == line){
                record_freq[i]++;
                exist = 1;
                break;
            }
        }
        if (exist == 0){
            next = next % MAX;
            record_freq[next] = 1;
            record_line[next] = line;
            strcpy(record_name[next], recorded_filename);
            next++;
            total++;
        }
        
    }


    total = total > MAX? MAX : total;
    for (int i = total - OUTMAX < 0? 0:total-OUTMAX; i < total; i++){
        int idx = (next + i) % total; 
        printf("%s %d %d\n", record_name[idx], record_line[idx], record_freq[idx]);
    }
}


发表于 2021-08-25 14:30:01 回复(0)