?? huichang.cpp
字號:
#include<fstream.h>
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
/*
int Partition(int a[], int low, int high )//劃分
{
int i,j;
int x = a[low];
i = low;
j = high;
while(i<j)
{
while(i<j&&x < a[j])
{
j = j - 1;
}
if(i<j)
{
a[i] = a[j];
i=i+1;
}
while(i<j&&x >= a[i])
{
i = i + 1;
}
if(i<j)
{
a[j] = a[i];
j=j-1;
}
}
a[i] = x;
return i;
}
void QuickSort(int a[], int low, int high)
{
int Position;
if(low < high)
{
Position = Partition(a,low,high);
QuickSort(a, low, Position-1);
QuickSort(a, Position+1, high);
}
}
*/
//交換函數
void swap1(int *a,int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
//選擇排序
void sort1(int *a,int b[],int as)
{
int i,j,index;
int elem;
for(i=0;i<as-1;i++){
elem=*(a+i);
index=i;
for(j=i+1;j<as;j++){
if(*(a+j)<elem){
elem=*(a+j);
index=j;
}
else if((*(a+j)==elem) && (b[j]<b[i])){//開始時間相同,比較結束時間
elem=*(a+j);
index=j;
}
}
swap1(a+i,a+index);
swap1(&b[i],&b[index]);
}
}
//會場安排函數
int schedule(int a[],int b[],int s,int e)
{
int n=0;
int i=s+1;
if ( a[s]>-1 )
{
n=1;
for(;i<=e;i++)
{
if ( a[i]>=b[s] ) //有一個活動結束,新活動可在已空閑的會場進行。
{
s++;
}
else
{
n++; //要增開一會場
}
}
}
return n;
}
void main( )
{
ifstream fin("input.txt");
ofstream fout("output.txt");
int n;
fin>>n; //讀入會場數
int *st=new int[n]; //開始時間
int *et=new int[n]; //結束時間
for(int i=0;i<n;i++)
{
fin>>st[i]>>et[i]; //讀入開始和結束時間
}
cout<<"排序前---------------"<<endl<<"開始時間"<<endl;
for( i=0;i<n;i++)
cout<<" "<<st[i];
cout<<endl<<"結束時間"<<endl;
for( i=0;i<n;i++)
cout<<" "<<et[i];
sort1(st,et,n);
//QuickSort(st,0,n-1);
//QuickSort(et,0,n-1);
cout<<endl<<endl<<"排序后---------------"<<endl<<"開始時間"<<endl;
for( i=0;i<n;i++)
cout<<" "<<st[i];
cout<<endl<<"結束時間"<<endl;
for( i=0;i<n;i++)
cout<<" "<<et[i];
fout<< schedule(st,et,0,n-1) <<endl; //把所需會場數寫入文件
delete []st;
delete []et;
cout<<endl<<"--------------所需會場數是-----------------------"<<"\t"<<endl;
ifstream fin1("output.txt");
fin1>>n; //將所需會場數讀出
cout<<n<<endl;
fin.close(); //關閉文件流
fout.close();
fin1.close();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -