Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请大牛指点 难道这么做不对?

Posted by zxxb at 2008-12-12 18:52:14 on Problem 2257
先按照应得得钱从达到小排序 遇到一个钱数大于0的 找一个钱数为负的,把钱全给他,这样从前到后重复n-1次 肯定最后都是0 难道不对吗
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct
{
  char name[30];
  int mon;
}M;
M m[1011];

int cmp(M a,M b)
{
    return a.mon>b.mon;
}

int main()
{
    int n,k,t;
    while(scanf("%d%d",&n,&k)!=EOF)
    {   
        getchar();
        if(n==0&&k==0){ break;}
        for(int i=0;i<n;i++)
        {
           scanf("%s",m[i].name);
           m[i].mon=0;
        }
        for(int i=0;i<k;i++)
        {
          char s1[30],s2[30];
          int mon;
          scanf("%s %s %d",s1,s2,&mon);
          getchar();
          int flag1=0,flag2=0;
          for(int j=0;j<n;j++)
          {
             if(strcmp(m[j].name,s1)==0)
             {
                m[j].mon-=mon;
                flag1=1;
             }
             if(strcmp(m[j].name,s2)==0)
             {
                m[j].mon+=mon;
                flag2=1;
             }
             if(flag1&&flag2)
               break;
          }
        }
        sort(m,m+n,cmp);
        t++;
        printf("Case #%d\n",t);
        for(int j=0;j<n-1;j++)
        {    
             if(m[j].mon>0)
             {
                 for(int i=j+1;i<n;i++)
                 {
                     if(m[i].mon<0)
                     {
                        m[i].mon+=m[j].mon;
                        printf("%s %s %d\n",m[j].name,m[i].name,m[j].mon);
                        m[j].mon=0;
                        break;
                     }
                 }
             }
        }
         printf("\n");
        }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator