zoukankan      html  css  js  c++  java
  • 寻找黑客

    题目链接

    程序

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

    运行结果

  • 相关阅读:
    7. v-bind 绑定Class操作 【对象语法】
    7。 V-bind 绑定
    【离散化】
    【洛谷 1576】最小花费
    【洛谷 1078】文化之旅
    【POJ 2115】CLooooops
    【洛谷 1516】青蛙的约会
    【UOJ 270】电厂计划
    【UOJ 92】有向图的强联通分量
    【POJ 2186】Popular Cows
  • 原文地址:https://www.cnblogs.com/fjlinww/p/10909212.html
Copyright © 2011-2022 走看看