Posted in 面试题 onJanuary 04, 2012
答案:
#i nclude
#i nclude
using namespace std;
void Order(vector& data) //bubble sort
{
int count = data.size() ;
int tag = false ; // 设置是否需要继续冒泡的标志位
for ( int i = 0 ; i {
for ( int j = 0 ; j {
if ( data[j] > data[j+1])
{
tag = true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}
void main( void )
{
vectordata;
ifstream in(“c:\\data.txt”);
if ( !in)
{
cout exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close(); //关闭输入文件流
Order(data);
ofstream out(“c:\\result.txt”);
if ( !out)
{
cout exit(1);
}
for ( i = 0 ; i out out.close(); //关闭输出文件流
}
#i nclude
#i nclude
using namespace std;
void Order(vector
{
int count = data.size() ;
int tag = false ; // 设置是否需要继续冒泡的标志位
for ( int i = 0 ; i {
for ( int j = 0 ; j {
if ( data[j] > data[j+1])
{
tag = true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}
void main( void )
{
vector
ifstream in(“c:\\data.txt”);
if ( !in)
{
cout exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close(); //关闭输入文件流
Order(data);
ofstream out(“c:\\result.txt”);
if ( !out)
{
cout exit(1);
}
for ( i = 0 ; i out out.close(); //关闭输出文件流
}
文件中有一组整数,要求排序后输出到另一个文件中
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@