您当前位置: 唯学网 » IT认证培训 » 真题专题

Java认证考试笔试真题精选(5)

来源:唯学网•教育培训(www.vixue.com)  【唯学网 • 中国教育电子商务平台】 加入收藏

9.文件中有一组整数,要求排序后输出到另一个文件中

答案:

#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 < count ; i++)

{

for ( int j =0 ; j < count - i - 1 ; 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;

ifstreamin("c:\\data.txt");

if ( !in)

{

cout<<"file error!";

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<<"fileerror!";

exit(1);

}

for ( i = 0 ; i < data.size() ; i++)

out<  10. 链表题:一个链表的结点结构

struct Node

{

int data ;

Node *next ;

};

typedef struct Node Node ;

(1)已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)

Node * ReverseList(Node*head) //链表逆序

{

if ( head == NULL || head->next == NULL )

returnhead;

Node *p1 = head ;

Node *p2 = p1->next ;

Node *p3 =p2->next ;

p1->next = NULL ;

while ( p3 != NULL )

{

p2->next = p1 ;

p1 = p2 ;

p2 = p3 ;

p3 = p3->next ;

}

p2->next = p1 ;

head = p2 ;

return head ;

}

0% (0)
0% (10)
已有条评论
新闻浏览排行