⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 evaluateroomstfn.cpp

📁 Intel 编程大赛参赛源代码
💻 CPP
字号:
// EvaluateRoomsTFN.cpp : Defines the entry point for the console application.
//

#include <iostream> 
#include <vector> 
#include <time.h>
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
using namespace std; 

vector<string>	TName;
vector<string>	TValue;
int Total;

void InputDataFromFile() 
 { 
	 char tmp[63]; 
	 FILE * fp = fopen("Students.in", "r"); 
	 if (fp==NULL)
	 {
		cerr<<"Can't open Students.in file!\n";   
		system("PAUSE");
		exit(-1);
	 }
	 fscanf(fp, "%d\r\n",&Total); 
	 string st; 
	 for(int i = 0;i < Total; i++) 
	 { 
		 fgets(tmp, 63, fp);	
		 st = string(tmp);
		 TName.push_back(st.substr(0, 10));
		 TValue.push_back(st.substr(11, 50));
	 } 
 }

double EvaluateRooms() 
 { 
	 float rt = 0; 
	 for(int i = 0; i < Total; i += 2) 
	 { 
		 float sum = 0; 
		 for(int j = 0; j < 50; j++) 
		 { 
			 sum += abs(TValue[i][j] - TValue[i+1][j]); 
		 }
		 rt += sum; 
	 } 
	 return (double)rt/200; 
 } 
inline int ComputeTwoPointsValue(int p1,int p2)
{
	float rt=0;
	for(int j = 0; j < 50; j++) 
		 { 
			 rt += abs(TValue[p1][j] - TValue[p2][j]); 
		 }
	return rt;
}


inline int IfLowVaThenAfterChange(int p1,int p2)
{	
	int beforechange=0,afterchange=0;
	int pt1=0,pt2=0; //L point
	if (p1==p2) return 0;
	if ((p1<p2)&&(p1%2==0)&&(abs(p1-p2)==1)) return 0;
	if ((p1>p2)&&(p2%2==0)&&(abs(p1-p2)==1)) return 0;
	if (p1%2==0)
		pt1=p1+1;
	else
		pt1=p1-1;
	if (p2%2==0)
		pt2=p2+1;
	else
		pt2=p2-1;
		beforechange=ComputeTwoPointsValue(p1,pt1)+ComputeTwoPointsValue(p2,pt2);
		afterchange=ComputeTwoPointsValue(p2,pt1)+ComputeTwoPointsValue(p1,pt2);
	return afterchange-beforechange;//
}

void ComputeRoomAssignmentsV2()
{
	srand((unsigned)time(0));
	long s=0,change_sum=1,computetimes=0;
	double StartT=1,EndT=100000;
	srand( (unsigned)time( 0 ) );
	while(EndT>StartT)
	{	
		int d1=GetRandom(0,Total-1);
		int d2=GetRandom(0,Total-1);
			int rt=IfLowVaThenAfterChange(d1,d2);
		if (rt<0)
		{
			swap(TName[d1],TName[d2]);
			swap(TValue[d1],TValue[d2]);
			if (change_sum>1)
				EndT=EndT*(1-(double)1/change_sum);
			s=0;
			change_sum++;
		}
		else
			if (rt!=0)  //if is not 0 then show this time is valiable swap computing but not use it
				s++;
		computetimes++;
		if (s==100000) 
			break;		
	}	
  	printf("The total  change times is %d \nTotal compute times is %d\nNochange times is %d\nEndT is %g\n",change_sum,computetimes,s,EndT);
}

void PrintRoomAssignmentToFile() 
 { 
	 FILE *outf=freopen("Assignments.out", "w", stdout); 
	 for(int i = 0; i < Total; i++) 
		 fprintf(outf,"%s\n",TName[i].c_str());
	 fclose(outf);
}
int main(int argc, char* argv[])
{
	float final,first;
	clock_t start,end;
	InputDataFromFile();
	first=EvaluateRooms();
	printf("The initial Disharmony is %f\n",first);
	start=clock();

	ComputeRoomAssignmentsV2();
	end=clock();
	printf("The time to computer room assignments is %g seconds\n",(double)(end-start)/CLOCKS_PER_SEC);	
	final=EvaluateRooms();
	printf("Final Disharmony is %f\n",final);
	printf("press enter to exit!");
	PrintRoomAssignmentToFile();
	getchar(); 
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -