• 寻找黑客


    题目链接

    程序

    #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
    ");
    		exit(0);
    	}
    	else if(argc > 1)
    	{
    		if((fp1=fopen(argv[1],"r")) == NULL){
    			printf("error, cannot open %s
    ",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
    ",HackNum);
    	for(i=0; i<j; i++)
    	{
    		if(send[i].Newlength >= SUMLEN)
    		{
    			fprintf(fp2,"%s
    ",send[i].name);
    		}
    	}
    	fclose(fp1);
    	fclose(fp2);
    	return 0;
    }
    
    

    运行结果

  • 相关阅读:
    Javascript的实例化与继承:请停止使用new关键字
    关于Unicode和URL encoding入门的一切以及注意事项
    Javascript: 从prototype漫谈到继承(2)
    你真的了解setTimeout和setInterval吗?
    javascript同步分页
    jquery ui dialog去除第一个文本框焦点问题
    指定范围内的随机数
    Mac下的mysql初始密码如何重置?
    进度条插件
    浏览器事件以及事件代理
  • 原文地址:https://www.cnblogs.com/fjlinww/p/10909212.html
Copyright © 2020-2023  润新知