码迷,mamicode.com
首页 > 其他好文 > 详细

寻找黑客

时间:2019-05-22 23:55:57      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:int   png   ssi   enter   string   mamicode   send   out   each   

题目链接

程序

#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;
}

运行结果

技术图片

技术图片

寻找黑客

标签:int   png   ssi   enter   string   mamicode   send   out   each   

原文地址:https://www.cnblogs.com/fjlinww/p/10909212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!