程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> USACO Section 3.2 Feed Ratios - 三層for循環就ok了!

USACO Section 3.2 Feed Ratios - 三層for循環就ok了!

編輯:C++入門知識

\

看下數據范圍~~最大才100...稍微想一下...答案求出的所有結果肯定都是小於100的了...那麼三層for循環來暴力找滿足要求的最優解就ok了..注意的是惡心點的..給出的(x,y,z)可能有為0的...所以要特殊處理下...

Program:

/*  
ID: zzyzzy12   
LANG: C++   
TASK: ratios
*/       
#include<iostream>        
#include<istream>    
#include<stdio.h>        
#include<string.h>        
#include<math.h>        
#include<stack>    
#include<algorithm>        
#include<queue>     
#define oo 2000000000    
#define ll long long     
using namespace std;   
struct node 

       int x,y,z; 
}s[4]; 
int i,j,k,ans[4],m; 
void getanswer() 

      ans[0]=oo;  
      for (i=0;i<=100;i++) 
        for (j=0;j<=100;j++) 
           for (k=0;k<=100;k++) 
              if ((!s[0].x || (s[1].x*i+s[2].x*j+s[3].x*k)%s[0].x==0) && (!s[0].y || (s[1].y*i+s[2].y*j+s[3].y*k)%s[0].y==0) && (!s[0].z || (s[1].z*i+s[2].z*j+s[3].z*k)%s[0].z==0)) 
              { 
                    m=0;        
                    if (s[0].x) m=(s[1].x*i+s[2].x*j+s[3].x*k)/s[0].x; 
                    else   
                    {  
                           if (s[1].x*i+s[2].x*j+s[3].x*k) continue; 
                           if (s[0].y) m=(s[1].y*i+s[2].y*j+s[3].y*k)/s[0].y; 
                           else 
                           { 
                                 if (s[1].y*i+s[2].y*j+s[3].y*k) continue; 
                                 if (s[0].z) m=(s[1].z*i+s[2].z*j+s[3].z*k)/s[0].z;    
                           }  
                    }   
                    if (!m) continue;                          
                    if (s[1].y*i+s[2].y*j+s[3].y*k==m*s[0].y) 
                      if (s[1].z*i+s[2].z*j+s[3].z*k==m*s[0].z) 
                        if (ans[0]>m)  
                        { 
                             ans[1]=i; ans[2]=j; ans[3]=k; 
                             ans[0]=m; 
                        } 
              }      

int main()   
{   
      freopen("ratios.in","r",stdin);     
      freopen("ratios.out","w",stdout);    
      for (i=0;i<4;i++) scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z); 
      getanswer(); 
      if (ans[0]==oo) printf("NONE\n"); else 
      printf("%d %d %d %d\n",ans[1],ans[2],ans[3],ans[0]);  
      return 0;      
}   
/* 
ID: zzyzzy12  
LANG: C++  
TASK: ratios
*/     
#include<iostream>     
#include<istream> 
#include<stdio.h>     
#include<string.h>     
#include<math.h>     
#include<stack> 
#include<algorithm>     
#include<queue>  
#define oo 2000000000 
#define ll long long  
using namespace std; 
struct node
{
       int x,y,z;
}s[4];
int i,j,k,ans[4],m;
void getanswer()
{
      ans[0]=oo;
      for (i=0;i<=100;i++)
        for (j=0;j<=100;j++)
           for (k=0;k<=100;k++)
              if ((!s[0].x || (s[1].x*i+s[2].x*j+s[3].x*k)%s[0].x==0) && (!s[0].y || (s[1].y*i+s[2].y*j+s[3].y*k)%s[0].y==0) && (!s[0].z || (s[1].z*i+s[2].z*j+s[3].z*k)%s[0].z==0))
              {
                    m=0;      
                    if (s[0].x) m=(s[1].x*i+s[2].x*j+s[3].x*k)/s[0].x;
                    else 
                    {
                           if (s[1].x*i+s[2].x*j+s[3].x*k) continue;
                           if (s[0].y) m=(s[1].y*i+s[2].y*j+s[3].y*k)/s[0].y;
                           else
                           {
                                 if (s[1].y*i+s[2].y*j+s[3].y*k) continue;
                                 if (s[0].z) m=(s[1].z*i+s[2].z*j+s[3].z*k)/s[0].z;  
                           }
                    } 
                    if (!m) continue;                        
                    if (s[1].y*i+s[2].y*j+s[3].y*k==m*s[0].y)
                      if (s[1].z*i+s[2].z*j+s[3].z*k==m*s[0].z)
                        if (ans[0]>m)
                        {
                             ans[1]=i; ans[2]=j; ans[3]=k;
                             ans[0]=m;
                        }
              }    
}
int main() 

      freopen("ratios.in","r",stdin);   
      freopen("ratios.out","w",stdout);  
      for (i=0;i<4;i++) scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z);
      getanswer();
      if (ans[0]==oo) printf("NONE\n"); else
      printf("%d %d %d %d\n",ans[1],ans[2],ans[3],ans[0]);
      return 0;    

 

 摘自 Jacob's zone

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved