题目链接
程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXREQS 15000
#define SUMLEN 1500
typedef struct Requests
{
char sender[10]; //sender name, all recivers are the same
int length; //the length of each request
}Reqs;
typedef struct Senders
{
char name[10]; //sender name
int Newlength; //the total length of each sender
}Send;
//check if str[] is in the send[], if yes return its procession, if no return -1
int IsRepeat(char str[], Send send[])
{
int i = 0;
int index = -1;
for(i=0; i<MAXREQS; i++)
{
if (strcmp(str, send[i].name) == 0)
{
index = i;
break;
}
}
return index;
}
int main(int argc, char *argv[])
{
FILE *fp1,*fp2;
Reqs reqs[MAXREQS] = {{},{},{},{},{},{},{},{}}; //max num we can process is MAXREQS
Send send[MAXREQS] = {{},{},{},{},{},{},{},{}};
int i=0,j=0,index=0,HackNum=0;
if(argc == 1){
printf("error, have not entered file name\n");
exit(0);
}
else if(argc > 1)
{
if((fp1=fopen(argv[1],"r")) == NULL){
printf("error, cannot open %s\n",argv[1]);
exit(0);
}
if(argc == 2)
fp2 = stdout;
else
fp2=fopen(argv[2],"w");
}
i=0;j=0;
while(fscanf(fp1,"%s%*s%d",reqs[i].sender,&reqs[i].length) != EOF)
{
index = IsRepeat(reqs[i].sender, send);
if (index == -1)
{
strcpy(send[j].name,reqs[i].sender);
send[j].Newlength = reqs[i].length;
j++;
}
else
{
//update the repeat value
send[index].Newlength = send[index].Newlength + reqs[i].length;
}
i++;
}
//find the hackers who's length is more than SUMLEN
for(i=0; i<j; i++)
{
if(send[i].Newlength >= SUMLEN)
{
HackNum++;
}
}
fprintf(fp2,"%d\n",HackNum);
for(i=0; i<j; i++)
{
if(send[i].Newlength >= SUMLEN)
{
fprintf(fp2,"%s\n",send[i].name);
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
运行结果
原文地址:https://www.cnblogs.com/fjlinww/p/10909212.html
时间: 2024-11-09 07:11:09