使用对拍遇到的一些问题

转载的地址:https://blog.csdn.net/wyt734933289/article/details/47400433

题目代码转载:https://www.cnblogs.com/jinglecjy/p/5679639.html

参考了该博主的使用方法,由于需求不同,更改时遇到一些问题。

验证的题为:poj-1017(一个暴力贪心);

一,首先,对拍利用freopen()函数读取txt文件中的数据。

freopen : C/C++重定向 标准输入输出 的库函数,

所谓重定向输出,就是可以把原本只是输出在控制台的字符,输出到你指定的路径文件中。(输入类似,就是从指定的文件中读取,而不是读取在控制台中的输入。重定向函数可以在任何时候开启、关闭。

此处使用时产生一个问题(1):

fclose(stdin);//关闭重定向输入

fclose(stdout);//关闭重定向输出

关闭输入输出流一定要在多组输入输出之外进行,否则程序会卡在那里也不报错。原因未知,可能一个程序只能开启一次。

二,利用随机数来设计数据

产生随机数据的只要与需要对应就可以运行(原来想着会有各种奇怪的bug)。

#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
int main()
{
    freopen("e:\\c++\\对拍\\data.txt","w",stdout);
    srand((unsigned) time(NULL));
    int n=10000;//n多少自己定
    while(n--)
    {
        printf("%d %d %d %d %d %d\n",rand()%100+1,rand()%100+1,rand()%100+1,rand()%100+1,rand()%100+1,rand()%100+1);
        printf("%d\n",n);
    }
    return 0;
}

数据为了方便个人去查,最好输入输出时加一个编号,加的方法很简单,多输出一列数据就行。

x是我用来确定编号用。

	printf("%d %d\n",x,num);

三,对比别人程序和你的产生数据

利用上文的freopen函数产生两个文本out1.txt和out2.txt文件。

对比两个文本数据,利用批处理脚本。图片来自转载博客。

四,代码部分

#include<iostream>
#include<cstdio> 
using namespace std;

int a[6];

int main()
{
	freopen("e:\\c++\\对拍\\data.txt","r",stdin);
	freopen("e:\\c++\\对拍\\out1.txt","w",stdout);
	int num,n;
	int x=100;
	while(x)
	{
		num=0;
		for(int i=0;i<6;i++)
		{
			scanf("%d",&a[i]);
			if(a[i] == 0)
			num++;
		}
		scanf("%d",&x);
		if(num==6)
		break;
		
		num=0;
		n=0;
		for(int i=5;i>=0;i--)
		{
			if(a[i]==0)
			continue;
			else
			switch(i){
				case 5 :
					num+=a[5];//num=93
					break;
				case 4:
					num+=a[i];//num=94
					if(a[0] <= a[4]*11)
					a[0] = 0;
					else
					a[0] -=a[4]*11;//a[0]=86
					break;
				case 3:
					num+=a[3];//num=99
					if(a[1] <= a[3]*5)//15<5*5
					{
						n=5*a[3]-a[1];//n=5*36-5*16-15*4=40
						//cout<<"n:"<<i<<" "<<n<<endl;
						a[1] = 0;
						if(n<a[0])
						{
							a[0]-=n;//a[0]=46;
						}
						else
						{
							a[0]=0;
							n=0;
						}
					}
					else
					{
						a[1]-=a[3]*5;
					}
					break;
				case 2:
					n=a[2]/4;//58/4=14
					a[2]=a[2]%4;//剩下的3*3,可能为0或小于4的数。 
					num+=n;//num=113;
					if(a[2] > 0)//a[2]=2;
					{
						n = 36-a[2]*9;//n的用途改变 n=36-2*9=18
						num++;//num=114
						if(n==27)
						{
							if(a[1] <=5)
							{
								n-=a[1]*2*2;
								a[1]=0;
								if(n>a[0])
								a[0]=0;
								else
								a[0]-=n;
							}
							else
							{
								a[1]-=5;
								n-=5*4;
							}
						}
						if(n==18)
						{
							if(a[1] <=3)
							{
								n-=a[1]*2*2;
								a[1]=0;
								if(n>a[0])
								a[0]=0;
								else
								a[0]-=n;//a[0]=46-18=28;
							}
							else
							{
								a[1]-=3;
								n-=3*4;
							}
						}
						if(n == 9)
						{
							if(a[1] <=1)
							{
								n-=a[1]*2*2;
								a[1]=0;
								if(n>a[0])
								a[0]=0;
								else
								a[0]-=n;
							}
							else
							{
								a[1]-=1;
								n-=1*4;
							}
						}
					}
					break;
				case 1 :
					n=a[1]/9;
					a[1]-=n*9;
					num+=n;
					if(a[1]!=0)
					{
						num++; 
						n=36-a[1]*4;//n切换用途 
						if(n>=a[0])
						a[0]=0;
						else
						a[0]-=n;
					}
					break;
				case 0:
					n=a[0]/36;
					a[0]-=n*36;
					num+=n;
					if(a[0]!=0)
					num++;//num=115;
					break;
			}
		}
		printf("%d %d\n",x,num);
		
	}
	fclose(stdin);//关闭重定向输入
	fclose(stdout);//关闭重定向输出
	return 0;
}
#include<cstdio>
#include<iostream>
using namespace std; 

int MAX_(int a, int b){
    if(a>b) return a;
    else return b;
}

int main(){
	freopen("e:\\c++\\对拍\\data.txt","r",stdin);
	freopen("e:\\c++\\对拍\\out2.txt","w",stdout);
	int x=100;
    int s1, s2, s3, s4, s5, s6;
    while(x){
    	scanf("%d %d %d %d %d %d", &s1, &s2, &s3, &s4, &s5, &s6);
    	scanf("%d",&x);//32 40 42 1 1 4
        int packets = 0;
        packets += s6; // 6*6的产品一个装一箱
        
        packets += s5; // 5*5的产品一个装一箱
        s1 = MAX_(0, s1-11*s5); // 剩余空间用1*1的产品尽量填满
        
        packets += s4; // 4*4的产品一个装一箱
        if(s2<5*s4) s1 = MAX_(0, s1-(5*s4-s2)); // 假如2*2的产品填完之后仍然有空隙,则用1*1填满 
        s2 = MAX_(0, s2-5*s4); // 尽量用2*2的产品填满 
        
        packets += (s3+3)/4; // 3*3的产品四个一箱
        s3 %= 4;            // 假如3*3的箱子不是四的倍数个,则先用2*2填充再用1*1填充 
        if(s3==1){
            if(s2<5) s1 = MAX_(0, s1-(27-4*s2));
            else     s1 = MAX_(0, s1-7);
            s2 = MAX_(0, s2-5);
        } 
        else if(s3==2){
            if(s2<3) s1 = MAX_(0, s1-(18-4*s2));
            else     s1 = MAX_(0, s1-6);
            s2 = MAX_(0, s2-3);
        }
        else if(s3==3){
            if(s2<1) s1 = MAX_(0, s1-(9-4*s2));
            else     s1 = MAX_(0, s1-5);
            s2 = MAX_(0, s2-1);    
        }
        
        packets += (s2+8)/9; // 2*2的产品九个一箱
        s2 %= 9;             // 假如2*2的箱子不是九的倍数个,则用1*1填充
        if(s2) s1 = MAX_(0, s1-(36-4*s2)); 
        
        packets += (s1+35)/36; // 1*1的产品三十六个一箱
        
        printf("%d %d\n",x, packets);
		  
    } 
    
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值