zoukankan      html  css  js  c++  java
  • usaco1.1.2 Greedy Gift Givers 题解

    【算法】模拟  【难度】☆☆☆☆☆
    考察字符串处理。这道题可以边读入边处理。
    注意审题,题目中没有说送礼者和受礼者的姓名和前面的名单中的名字顺序一样。关键在于字符串的匹配。
    字符串的匹配可以用一个strcmp函数的。。我忘了这个函数就自己写了一个。。
    View Code
     1 /*
    2 ID: wsc5001
    3 LANG: C
    4 TASK: gift1
    5 */
    6 #include<stdio.h>
    7 #include<stdlib.h>
    8 char pname[10][15]={'\0'};
    9 int n;
    10 int zhaoren(char shuru[])
    11 {
    12 int i,j,k,t,flag;
    13 for(i=0;i<n;i++)
    14 {
    15 flag=1;
    16 t=0;
    17 while ((!(shuru[t]=='\0'&&pname[i][t]=='\0'))&&t<15)
    18 {
    19 if (shuru[t]!=pname[i][t])
    20 flag=0;
    21 t++;
    22 }
    23 if (flag==1)
    24 return i;
    25 }
    26 return -1;
    27 }
    28 int main()
    29 {
    30 FILE *fin,*fout;
    31 fin=fopen("gift1.in","r");
    32 fout=fopen("gift1.out","w");
    33 char tempchar,thisp1[15],thisp2[15];
    34 int pmoney[10]={0},pyuan[10]={0};
    35 int i,j,t,m,p,q,w;
    36 fscanf(fin,"%d%c",&n,&tempchar);
    37 //读入名单
    38 for (i=0;i<n;i++)
    39 {
    40 j=0;
    41 while (1)
    42 {
    43 fscanf(fin,"%c",&tempchar);
    44 if (tempchar!='\n')
    45 {
    46 pname[i][j]=tempchar;
    47 j++;
    48 }
    49 else
    50 break;
    51 }
    52 }
    53 ///////
    54 for (i=0;i<n;i++)
    55 {
    56 fscanf(fin,"%s%d%d",thisp1,&p,&q);
    57 t=zhaoren(thisp1);
    58 {
    59 if(q!=0)
    60 {
    61 pyuan[t]=(p/q)*q;
    62 for (j=0;j<q;j++)
    63 {
    64 fscanf(fin,"%s",thisp2);
    65 w=zhaoren(thisp2);
    66 pmoney[w]+=p/q;
    67 }
    68 }
    69 }
    70 }
    71 for (i=0;i<n;i++)
    72 {
    73 fprintf(fout,"%s %d\n",pname[i],pmoney[i]-pyuan[i]);
    74 }
    75 fclose(fin);
    76 fclose(fout);
    77 //system("pause");
    78 }

  • 相关阅读:
    .net Application的目录
    (转载).NET中RabbitMQ的使用
    (转载)RabbitMQ消息队列应用
    说说JSON和JSONP
    SQL Server中的事务与锁
    StackExchange.Redis Client(转载)
    正则语法的查询,这是纯转载的,为了方便查询
    Regex的性能问题
    解决json日期格式问题的办法
    BenchmarkDotNet(性能测试)
  • 原文地址:https://www.cnblogs.com/loveidea/p/2416920.html
Copyright © 2011-2022 走看看