回 帖 发 新 帖 刷新版面

主题:一道ACM题,我为什么AC不了?测试的例子都是对的啊

对于幸存的灾民来说,最急待解决的显然是温饱问题,救灾部队一边在组织人员全力打通交通,一边在组织采购粮食。现在假设下拨了一定数量的救灾经费要去市场采购大米(散装)。如果市场有m种大米,各种大米的单价和重量已知,请问,为了满足更多灾民的需求,最多能采购多少重量的大米呢?

Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0<n<=1000,0<m<=1000),分别表示经费的金额和大米的种类,然后是m行数据,每行包含2个整数p和h(1<=p<=25,1<=h<=100),分别表示单价和对应大米的重量。 
Output
对于每组测试数据,请输出能够购买大米的最多重量(你可以假设经费买不光所有的大米)。
每个实例的输出占一行,保留2位小数。 
Sample Input
1
7 2
3 3
4 4

Sample Output
2.33

Author
lcy 


我的代码:
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;

using std::setprecision;


struct node
{
    int p;
    int h;
};

node array[1000];


//Store the sorted nodes into array
void sort(node* pointer, int size)
{
    int min = pointer[0].p;
    int s = 0;
    
    
    for(int i = 0;i<size;++i)
    {
     
        for(int j = 0;j<size;++j)
                if(pointer[j].p<=min)
                        {
                        
                        
                                 min = pointer[j].p;
                                          s = j; 
                        }
        pointer[i].p = 26;
        array[i].p = min;
        array[i].h = pointer[s].h;
       
        
        min = pointer[0].p;
        s = 0;
        
        
        
    
    }
    
 

}


int main()
{
    int C;
    int n, m;
    node* ptr;
    
    
    int i, j, k,l;
    int a, b;
    int result = 0;
    
    //Format the output1
    
    cout<<fixed<<setprecision(2);
    
    cin>>C;
    for(l = 0;l<C;++l)
    {
        cin>>n>>m;
        
        ptr = new node[m];
        for(j = 0;j<m;++j)
            cin>>ptr[j].p>>ptr[j].h;
               
       
                
        
        //To sort it
           
        
       sort(ptr, m);
       
        //Iterator
        a = n;
        
        for(i=0;i<m;++i)
        {
                if(a>array[i].p*array[i].h)
                                a-=array[i].p*array[i].h;
                else
                {
                               b = i - 1;
                               break;
                
                }
                                
                                
        }
        
        
        
        for(k = 0;k<=b;++k)
                result+=array[k].h;
        
        
                
    
                
        cout<<result + static_cast<double>(a)/array[b + 1].p<<endl;
       
        delete ptr;
        result = 0;
        
               
                       
                
    }
    
   system("PAUSE");
    
    return 0;
    
}


[code=c]
请填写代码
[/code][code=c]
请填写代码
[/code]

回复列表 (共1个回复)

沙发

你的代码太长了,看看我的代码合不合你的要求?

#include<iostream.h>
int main(){

    int time,Money,FoodType;
    int t,f;
    float fa,fb,fc,fab,want=999999.00;

    cin>>time;
    for(t=0;t<time;t++){
        cin>>Money>>FoodType;
        for(f=0;f<FoodType;f++){
            cin>>fa>>fb;
            fab=fa/fb;
            if(want>fab)want=fab,fc=fa;
        }
        cout<<(float)Money/(float)fc<<endl;
    }
        
    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册