POJ 2709 贪心算法 测试数据生成程序
2014-11-18 10:38
218 查看
#include<iostream> using namespace std; //*****************算法变量************************// const int Max=20; int InputNum;//输入N值; int InputArray[Max];//输入的颜料数目 int GrayNum;//灰色颜料的数量 int ColorNum;//需要的颜料盒数 //完成排序 inline void Swap(int Array[],int i,int j) { int temp=Array[i]; Array[i]=Array[j]; Array[j]=temp; } void Sort(int Array[],int Size) { int i,j; for(i=1;i<Size;i++) { for(j=Size-1;j>=i;j--) { if(Array[j]>Array[j-1]) Swap(Array,j,j-1); } } } int main() { while(true) { ColorNum=0;//谨记每次都要初始化 scanf("%d",&InputNum); if(InputNum==0) { return 0;} for(int i=0;i<InputNum;i++) { scanf("%d",&InputArray[i]);} scanf("%d",&GrayNum); Sort(InputArray,InputNum); //从所需其他颜色的最大值计算份数 if(InputArray[0]!=0)//要考虑到都是0的情况 { ColorNum=(InputArray[0]+49)/50;} for(int i=0;i<InputNum;i++) {InputArray[i]=50*ColorNum-InputArray[i];} //调制灰色;按1ml递增来调制 for(int i=1;i<=GrayNum;i++) { Sort(InputArray,InputNum); if(InputArray[2]==0)//如果颜料数量第三大已为0,那就加颜料 { ColorNum++; for(int i=0;i<InputNum;i++) InputArray[i]+=50; } InputArray[0]--; InputArray[1]--; InputArray[2]--; } printf("%d\n",ColorNum); } return 0; }
AC过的代码:
</pre><pre name="code" class="cpp">132K 0ms
</pre><pre name="code" class="cpp">容易出错的地方:
</pre><pre name="code" class="cpp">1.调制灰色颜料必须按1ML来调制
2.
ColorNum=0;//谨记每次都要初始化
!!!!每次循环一定要记得对结果进行初始化,不然就会Wrong
算法思路:先根据纯色颜料中的最大者求出至少需要的颜料盒数,然后用剩余的颜料来配gray,配的时候要1ml 1ml的配gray颜料,通过不断重新从大到小排序,每次从前三种颜料去1ml配出1mlgray颜料,配之前判断下第3种颜料是否还有剩余,没有就加1个颜料盒,以此类推,到gray颜料配足够为止。要注意判断一定要紧跟排序之后,防止减完第三种颜料为0误判,因为此时第4种颜料可能还剩余1ml
经验:
1.可以自己写测试数据到txt文件中,将测试数据输入到AC过的代码,和自己的代码进行对比。写的测试数据必须根据题意来写
本题的测试数据生成程序如下:
#include<iostream> #include<fstream> using namespace std; inline int Random(int x) //通过取余取得指定范围的随机数,0~x-1 { return (rand()%x); } int main() { int N,k,G; ofstream fout("ceshi.txt"); //生成100组测试数据 for(int i=1;i<=100;i++) { do{ N=Random(12);} while(N<3); fout<<N<<" "; for(int j=1;j<=N;j++) { k=Random(1001); fout<<k<<" "; } G=Random(1001); fout<<G<<" "; fout<<"\n"; } fout<<0<<" "; fout.close(); }
#define Random(x) (rand() % x) //通过取余取得指定范围的随机数
生成指定范围随机数的方法。
利用测试文件进行测试的源代码如下
#include"stdafx.h" #include <stdlib.h> #include <stdio.h> #include<fstream> using namespace std; //*****************算法变量************************// const int Max=20; int InputNum;//输入N值; int InputArray[Max]; int GrayNum;//灰色颜料的数量 int ColorNum;//需要的颜料盒数 inline void Swap(int Array[],int i,int j) { int temp=Array[i]; Array[i]=Array[j]; Array[j]=temp; } void Sort(int Array[],int Size) { int i,j; for(i=1;i<Size;i++) { for(j=Size-1;j>=i;j--) { if(Array[j]>Array[j-1]) Swap(Array,j,j-1); } } } int main() { fstream read("ceshi.txt"); while(true) { ColorNum=0; /*scanf("%d",&InputNum);*/ read>>InputNum; if(InputNum==0) { return 0;} for(int i=0;i<InputNum;i++) { /*scanf("%d",&InputArray[i]);*/ read>>InputArray[i]; } /*scanf("%d",&GrayNum);*/ read>>GrayNum; Sort(InputArray,InputNum); //从所需其他颜色的最大值计算份数 if(InputArray[0]!=0) { ColorNum=(InputArray[0]+49)/50;} for(int i=0;i<InputNum;i++) {InputArray[i]=50*ColorNum-InputArray[i];} //调制灰色 for(int i=1;i<=GrayNum;i++) { Sort(InputArray,InputNum); if(InputArray[2]==0) { ColorNum++; for(int i=0;i<InputNum;i++) InputArray[i]+=50; } InputArray[0]--; InputArray[1]--; InputArray[2]--; } printf("%d\n",ColorNum); } return 0; }
相关文章推荐
- ASP.NET权限组件,生成10万条测试数据检测程序的大数据性能改进
- shell 抓取网页解析网页 自动读取poj测试数据——V2生成 pku poj acm编程模版
- ASP.NET权限组件,生成10万条测试数据检测程序的大数据性能改进
- 生成随机测试数据的文件的程序
- 主函数main()中由数字和字母组成的字符串s为测试数据,程序 主要提取字符串s中由字符'$'分隔的不同八进制数形式的字符串, 将八进制数形式字符子串所对应的字符生成新的字符串
- 一个生成COBOL测试数据的小程序——解释数据中的数据类型
- 一个生成COBOL测试数据的小程序——分析COBOL中数据声明的语句
- 自动生成测试数据加强版
- ABAP动态生成经典应用之Table数据Upload 程序
- 数据库测试数据自动生成工具
- 用TCL编写了一个生成密码字典的小测试程序,共享一下
- 测试程序通过excel读取测试数据达到数据与程序的分离
- [SAP]ABAP动态生成经典应用之Table数据Upload 程序
- mysql 动态生成测试数据
- Sync Service for ADO.NET 程序调试 - 使用Tracing生成LOG(数据同步的日志) [DEVICE篇]
- 生成测试数据
- Oracle批量生成测试数据方法
- 通过C#程序生成数据库的实体类,根据SqlServer存储过程生成数据操作类
- 使用Sql生成测试数据
- 使用Sql生成测试数据(转贴)