函数题
int del ( SeqList * sq, int i)
{
int j; i-- ;
if ( ( i< 0 ) || ( i > sq-> n- 1 ) ) return - 1 ;
else {
for ( j= i+ 1 ; j<= sq-> n- 1 ; j++ ) sq-> a[ j- 1 ] = sq-> a[ j] ;
sq-> n-- ;
return 1 ;
}
}
int Length ( List L ) {
int n= 0 ;
List p = L ;
while ( p!= NULL ) {
n++ ;
p= p-> Next ;
}
return n;
}
List Insert ( List L , ElementType X ) {
List tail= ( List ) malloc ( sizeof ( struct Node ) ) ;
tail-> Data = X ;
tail-> Next = NULL ;
List head= L ;
while ( L -> Next != NULL && L -> Next -> Data <= X ) {
L = L -> Next ;
}
tail-> Next = L -> Next ;
L -> Next = tail;
return head;
}
Node * find_middle ( Node * head)
{
int count = 0 ;
Node * p = head;
if ( p == NULL )
return p;
while ( p-> next != NULL )
{
count++ ;
p = p-> next;
}
p = head;
count = count/ 2 ;
while ( count > 0 )
{
p = p-> next;
count-- ;
}
return p;
}
ElementType Find ( List L , int m ) {
List L1 , L2 ;
L1 = L -> Next ;
L2 = L -> Next ;
int i;
for ( i = 0 ; i < m; i++ ) {
if ( L1 == NULL ) {
return ERROR ;
}
L1 = L1 -> Next ;
}
while ( L1 ) {
L1 = L1 -> Next ;
L2 = L2 -> Next ;
}
return L2 -> Data ;
}
struct ListNode * reverse ( struct ListNode * head )
{
struct ListNode * p, * q;
q= ( struct ListNode * ) malloc ( sizeof ( struct ListNode ) ) ;
if ( head== NULL )
return ;
p= head-> next;
head-> next= NULL ;
while ( p!= NULL ) {
q= p;
p= p-> next;
q-> next= head;
head= q;
}
return head;
}
List Merge ( List L1 , List L2 ) {
List a, c, b, l;
l= ( List ) malloc ( sizeof ( PtrToNode ) ) ;
a= L1 -> Next , b= L2 -> Next , c= l;
while ( a&& b) {
if ( a-> Data > b-> Data ) {
c-> Next = b;
b= b-> Next ;
}
else {
c-> Next = a;
a= a-> Next ;
}
c= c-> Next ;
}
if ( a) c-> Next = a;
if ( b) c-> Next = b;
L1 -> Next = NULL ;
L2 -> Next = NULL ;
return l;
}
bool Push ( Stack S , ElementType X ) {
if ( S -> Top == S -> MaxSize ) {
printf ( "Stack Full\n" ) ;
return false ;
}
S -> Data [ ( S -> Top ) ++ ] = X ;
return true ;
}
ElementType Pop ( Stack S ) {
ElementType res;
if ( S -> Top == 0 ) {
printf ( "Stack Empty\n" ) ;
return ERROR ;
}
res = S -> Data [ -- ( S -> Top ) ] ;
return res;
}
bool AddQ ( Queue Q , ElementType X )
{
if ( Q -> Count == Q -> MaxSize )
{
printf ( "Queue Full\n" ) ;
return false ;
}
Q -> Count ++ ;
Q -> Data [ ( Q -> Front + Q -> Count ) % Q -> MaxSize ] = X ;
return true ;
}
ElementType DeleteQ ( Queue Q )
{
if ( Q -> Count == 0 )
{
printf ( "Queue Empty\n" ) ;
return ERROR ;
}
Q -> Count -- ;
Q -> Front = ( Q -> Front + 1 ) % Q -> MaxSize ;
return Q -> Data [ Q -> Front ] ;
}
int GetHeight ( BinTree BT ) {
if ( BT == NULL )
return 0 ;
int h1= GetHeight ( BT -> Left ) ;
int h2= GetHeight ( BT -> Right ) ;
return h1> h2? h1+ 1 : h2+ 1 ;
}
void PreorderPrintLeaves ( BinTree BT ) {
if ( BT == NULL )
return ;
if ( BT -> Left == NULL && BT -> Right == NULL )
printf ( " %c" , BT -> Data ) ;
PreorderPrintLeaves ( BT -> Left ) ;
PreorderPrintLeaves ( BT -> Right ) ;
}
void InorderTraversal ( BinTree BT ) {
if ( BT ) {
InorderTraversal ( BT -> Left ) ;
printf ( " %c" , BT -> Data ) ;
InorderTraversal ( BT -> Right ) ;
}
}
void PreorderTraversal ( BinTree BT ) {
if ( BT ) {
printf ( " %c" , BT -> Data ) ;
PreorderTraversal ( BT -> Left ) ;
PreorderTraversal ( BT -> Right ) ;
}
}
void PostorderTraversal ( BinTree BT ) {
if ( BT ) {
PostorderTraversal ( BT -> Left ) ;
PostorderTraversal ( BT -> Right ) ;
printf ( " %c" , BT -> Data ) ;
}
}
void LevelorderTraversal ( BinTree BT ) {
BinTree T [ 1000 ] ;
int front= 0 , rear= 0 ;
if ( BT ) {
T [ rear++ ] = BT ;
while ( front!= rear) {
BT = T [ front++ ] ;
printf ( " %c" , BT -> Data ) ;
if ( BT -> Left )
T [ rear++ ] = BT -> Left ;
if ( BT -> Right )
T [ rear++ ] = BT -> Right ;
}
}
}
int NodeCount ( BiTree T )
{
int cnt = 0 ;
if ( T )
{
cnt++ ;
cnt += NodeCount ( T -> lchild) ;
cnt += NodeCount ( T -> rchild) ;
}
return cnt;
}
int NodeCount ( BiTree T )
{
if ( ! T )
return 0 ;
if ( ( T -> lchild && ! T -> rchild) || ( ! T -> lchild && T -> rchild) )
{
return NodeCount ( T -> lchild) + NodeCount ( T -> rchild) + 1 ;
}
return NodeCount ( T -> lchild) + NodeCount ( T -> rchild) ;
}
void degree ( MGraph G )
{
for ( int i = 0 ; i < G . vexnum; i++ )
{
int c = 0 ;
for ( int j = 0 ; j < G . vexnum; j++ )
{
if ( G . arcs[ j] [ i] != 0 )
c++ ;
}
printf ( "%c:%d\n" , G . vexs[ i] , c) ;
}
}
void indegree ( MGraph G )
{
for ( int i = 0 ; i < G . vexnum; i++ )
{
int cnt= 0 ;
for ( int j = 0 ; j < G . vexnum; j++ )
{
if ( G . arcs[ j] [ i] == 1 )
cnt++ ;
}
printf ( "%c:%d\n" , G . vexs[ i] , cnt) ;
}
}
void DFS ( MGraph Graph , Vertex V , void ( * Visit ) ( Vertex ) )
{
Visit ( V ) ;
Visited [ V ] = 1 ;
for ( int i = 0 ; i < Graph -> Nv ; i++ )
{
if ( Graph -> G [ V ] [ i] == 1 )
{
if ( Visited [ i] == false )
{
DFS ( Graph , i, Visit ) ;
}
}
}
}
void DFS ( ALGraph * G , int i)
{
printf ( " %d" , i) ;
visited[ i] = 1 ;
struct ArcNode * w;
for ( w = G -> vertices[ i] . firstarc; w; w = w-> nextarc)
{
if ( visited[ w-> adjvex] == 0 )
DFS ( G , w-> adjvex) ;
}
}
void BFS ( MGraph G , Vertex i)
{
int q[ MaxVertexNum ] , front= 0 , rear= 0 ;
printf ( " %d" , i) ;
visited[ i] = 1 ;
q[ rear++ ] = i;
while ( front!= rear)
{
int now= q[ front++ ] ;
for ( i= 0 ; i< G . vexnum; i++ )
{
if ( G . arcs[ now] [ i] == 1 && visited[ i] == 0 )
{
printf ( " %d" , i) ;
visited[ i] = 1 ;
q[ rear++ ] = i;
}
}
}
}
void BFS ( LGraph Graph , Vertex S , void ( * Visit ) ( Vertex ) ) {
Visit ( S ) ;
Visited [ S ] = true ;
Vertex queue[ 105 ] ;
int front = 0 , rear = 0 ;
queue[ rear++ ] = S ;
PtrToAdjVNode p;
while ( front< rear) {
Vertex x = queue[ front] ;
front++ ;
p = Graph -> G [ x] . FirstEdge;
while ( p) {
Vertex v = p-> AdjV ;
if ( ! Visited [ v] ) {
Visit ( v) ;
Visited [ v] = true ;
queue[ rear++ ] = v;
}
p = p-> Next ;
}
}
}
编程题
#include < stdio. h>
#include < string. h>
void BubbleSort_For_Strings ( char ( * Array ) [ 20 ] , int Line , int Times )
{
int i, j;
for ( i= 0 ; i< Times ; i++ )
{
for ( j= 0 ; j< Line - i- 1 ; j++ )
{
if ( strcmp ( Array + j, Array + j+ 1 ) > 0 )
{
char temp[ 20 ] ;
strcpy ( temp, Array + j) ;
strcpy ( Array + j, Array + j+ 1 ) ;
strcpy ( Array + j+ 1 , temp) ;
}
}
}
}
int main ( )
{
int N , K , i= 0 , j= 0 ;
char a[ 100 ] [ 20 ] = { 0 } ;
scanf ( "%d %d" , & N , & K ) ;
getchar ( ) ;
for ( i= 0 ; i< N ; i++ )
{
j= - 1 ;
do
{
j++ ;
scanf ( "%c" , & a[ i] [ j] ) ;
} while ( a[ i] [ j] != '\n' ) ;
}
BubbleSort_For_Strings ( a, N , K ) ;
for ( i= 0 ; i< N ; i++ )
{
j= - 1 ;
do
{
j++ ;
printf ( "%c" , a[ i] [ j] ) ;
} while ( a[ i] [ j] != '\n' ) ;
}
return 0 ;
}
#include < stdio. h>
#include < stdlib. h>
typedef struct node
{
int data;
struct node * next;
} node;
struct node * createList ( ) ;
int main ( )
{
node * L1 = createList ( ) ;
node * L2 = createList ( ) ;
int cnt = 0 , num = 0 ;
node * p1 = L1 ;
node * p2 = L2 ;
while ( p1!= NULL && p2!= NULL )
{
if ( p1-> data< p2-> data)
{
p1 = p1-> next;
}
else if ( p1-> data == p2-> data)
{
num++ ;
if ( cnt == 0 )
{
cnt = 1 ;
printf ( "%d" , p1-> data) ;
}
else
{
printf ( " %d" , p1-> data) ;
}
p1 = p1-> next;
p2 = p2-> next;
}
else
{
p2 = p2-> next;
}
}
if ( num== 0 )
printf ( "NULL" ) ;
return 0 ;
}
struct node * createList ( )
{
node * head = NULL ;
head = ( node* ) malloc ( sizeof ( node) ) ;
head-> next = NULL ;
node * p = head;
int x;
scanf ( "%d" , & x) ;
while ( x!= - 1 )
{
node * q = NULL ;
q = ( node* ) malloc ( sizeof ( node) ) ;
q-> data = x;
p-> next = q;
p = q;
q-> next = NULL ;
scanf ( "%d" , & x) ;
}
return head-> next;
}
#include < iostream>
#include< stack>
#include< string>
using namespace std;
bool check ( string s) ;
int main ( )
{
string s;
while ( getline ( cin, s) ) {
cout<< ( check ( s) ? "yes" : "no" ) << endl;
}
return 0 ;
}
bool check ( string s) {
stack< char > st;
for ( int i= 0 ; i< s. size ( ) ; i++ ) {
char c= s[ i] ;
if ( c== '(' || c== '[' || c== '{' ) {
st. push ( c) ;
}
else if ( c== ')' || c== ']' || c== '}' ) {
if ( st. empty ( ) ) {
return false ;
}
char x= st. top ( ) ;
st. pop ( ) ;
if ( c== ')' && x!= '(' ||
c== ']' && x!= '[' ||
c== '}' && x!= '{' ) {
return false ;
}
}
}
return st. empty ( ) ;
}
#include < cctype>
#include < iostream>
#include < stack>
#include < string>
using namespace std;
int main ( ) {
string st;
while ( cin >> st) {
stack< string> s;
stack< string> ope;
string temp;
for ( char ct : st) {
if ( isdigit ( ct) ) {
temp = ct;
s. push ( temp) ;
}
else {
double left, right, res;
switch ( ct) {
case '+' :
right = stod ( s. top ( ) ) ;
s. pop ( ) ;
left = stod ( s. top ( ) ) ;
s. pop ( ) ;
res = left + right;
temp = to_string ( res) ;
s. push ( temp) ;
break ;
case '-' :
right = stod ( s. top ( ) ) ;
s. pop ( ) ;
left = stod ( s. top ( ) ) ;
s. pop ( ) ;
res = left - right;
temp = to_string ( res) ;
s. push ( temp) ;
break ;
case '*' :
right = stod ( s. top ( ) ) ;
s. pop ( ) ;
left = stod ( s. top ( ) ) ;
s. pop ( ) ;
res = left * right;
temp = to_string ( res) ;
s. push ( temp) ;
break ;
case '/' :
right = stod ( s. top ( ) ) ;
s. pop ( ) ;
left = stod ( s. top ( ) ) ;
s. pop ( ) ;
res = left / right;
temp = to_string ( res) ;
s. push ( temp) ;
break ;
}
}
}
double out = stod ( s. top ( ) ) ;
printf ( "%.2lf\n" , out) ;
}
return 0 ;
}
#include< stdio. h>
#include< stdlib. h>
struct Queue {
int data;
struct Queue * next;
} * queue;
struct Queue * createQueue ( ) ;
void push ( int num) ;
void pop ( ) ;
int main ( )
{
queue = createQueue ( ) ;
int N , num;
scanf ( "%d" , & N ) ;
for ( int z= 0 ; z< N ; z++ ) {
scanf ( "%d" , & num) ;
if ( num== 0 ) pop ( ) ;
else push ( num) ;
}
putchar ( '\n' ) ;
while ( queue-> data!= 0 ) {
printf ( "%d " , queue-> data) ;
queue-> data = 0 ;
queue = queue-> next;
}
return 0 ;
}
struct Queue * createQueue ( )
{
struct Queue * head, * body;
head = ( struct Queue * ) malloc ( sizeof ( struct Queue ) ) ;
head-> data = 0 ;
body = head;
for ( int z= 1 ; z< 10 ; z++ ) {
body-> next = ( struct Queue * ) malloc ( sizeof ( struct Queue ) ) ;
body = body-> next;
body-> data = 0 ;
}
body-> next = head;
return head;
}
void push ( int num)
{
struct Queue * head = queue;
for ( int z= 0 ; z< 9 ; z++ ) {
if ( head-> data== 0 ) {
head-> data = num;
return ;
}
head = head-> next;
}
printf ( "FULL " ) ;
}
void pop ( )
{
if ( queue-> data== 0 ) printf ( "EMPTY " ) ;
else
{
printf ( "%d " , queue-> data) ;
queue-> data = 0 ;
queue = queue-> next;
}
}
#include< bits/ stdc++ . h>
#define OK 1
#define ERROR 0
#define OVERFLOW - 2
using namespace std;
typedef long long ll;
struct LinkQueue {
int * base;
int front, last;
} ;
void InitQueue ( LinkQueue & q) {
q. base = ( int * ) malloc ( 1007 * sizeof ( int ) ) ;
q. front = q. last = 0 ;
}
void push ( LinkQueue & q, int e) {
q. base[ q. last++ ] = e;
}
void pop ( LinkQueue & q) {
q. front++ ;
}
int GetTop ( LinkQueue & q) {
return q. base[ q. front] ;
}
int EmptyQueue ( LinkQueue & q) {
if ( q. front== q. last) return true ;
return false ;
}
int main ( )
{
int n;
LinkQueue q1, q2;
scanf ( "%d" , & n) ;
InitQueue ( q1) ; InitQueue ( q2) ;
for ( int i= 1 ; i<= n; i++ ) {
int e;
scanf ( "%d" , & e) ;
if ( e& 1 ) {
push ( q1, e) ;
} else push ( q2, e) ;
}
int i= 0 ;
while ( ! EmptyQueue ( q1) || ! EmptyQueue ( q2) ) {
if ( ! EmptyQueue ( q1) ) {
if ( i++ ) printf ( " " ) ;
cout<< GetTop ( q1) ;
pop ( q1) ;
}
if ( ! EmptyQueue ( q1) ) {
if ( i++ ) printf ( " " ) ;
cout<< GetTop ( q1) ;
pop ( q1) ;
}
if ( ! EmptyQueue ( q2) ) {
if ( i++ ) printf ( " " ) ;
cout<< GetTop ( q2) ;
pop ( q2) ;
}
}
}
#include< iostream>
using namespace std;
const int MAX = 305 ;
int main ( ) {
int n, m;
while ( cin >> n >> m) {
if ( n == 0 && m == 0 ) break ;
int arr[ MAX ] = { 0 } ;
for ( int i = 1 ; i <= n; i++ ) arr[ i] = 1 ;
int sum = n, cur = 1 , count = 0 ;
while ( sum > 1 ) {
if ( arr[ cur] == 1 ) {
count++ ;
if ( count == m) {
arr[ cur] = 0 ;
count = 0 ;
sum-- ;
}
}
cur++ ;
cur = ( cur == n + 1 ) ? 1 : cur;
}
for ( int i = 1 ; i <= n; i++ ) {
if ( arr[ i] == 1 ) {
printf ( "%d\n" , i) ;
break ;
}
}
}
return 0 ;
}
#include < stdio. h>
#include < stdlib. h>
typedef struct TNode * Tree ;
struct TNode
{
int data;
struct TNode * Left ;
struct TNode * Right ;
} ;
int flag[ 10 ] ;
struct Node
{
int left, right;
} num[ 10 ] ;
Tree Build ( int node)
{
Tree t = ( Tree ) malloc ( sizeof ( struct TNode ) ) ;
t-> data = node;
t-> Left = t-> Right = NULL ;
if ( num[ node] . left != 100 ) t-> Left = Build ( num[ node] . left) ;
if ( num[ node] . right != 100 ) t-> Right = Build ( num[ node] . right) ;
return t;
}
void Print ( Tree T )
{
if ( T == NULL ) return ;
Tree queue[ 10 ] ;
int left= 0 , right= 0 ;
queue[ right++ ] = T ;
int cnt = 0 ;
while ( left < right)
{
Tree t = queue[ left++ ] ;
if ( t-> Left == NULL && t-> Right == NULL ) {
if ( cnt == 0 ) printf ( "%d" , t-> data) ;
else printf ( " %d" , t-> data) ;
cnt++ ;
}
if ( t-> Left ) queue[ right++ ] = t-> Left ;
if ( t-> Right ) queue[ right++ ] = t-> Right ;
}
}
int main ( )
{
int n;
scanf ( "%d" , & n) ;
getchar ( ) ;
for ( int i= 0 ; i< n; i++ )
{
char x, y;
scanf ( "%c %c" , & x, & y) ;
getchar ( ) ;
if ( x == '-' ) num[ i] . left = 100 ;
else
{
num[ i] . left = x - '0' ;
flag[ x - '0' ] = 1 ;
}
if ( y == '-' ) num[ i] . right = 100 ;
else
{
num[ i] . right = y - '0' ;
flag[ y - '0' ] = 1 ;
}
}
int node;
for ( int i= 0 ; i< n; i++ )
{
if ( flag[ i] == 0 ) node = i;
}
Tree t = Build ( node) ;
Print ( t) ;
return 0 ;
}
#include< iostream>
using namespace std;
typedef struct BiTNode {
char c;
struct BiTNode * lchild;
struct BiTNode * rchild;
} BiTNode , * BiTree ;
BiTree Build ( char * pre, char * in, int n) {
if ( n <= 0 )
return NULL ;
BiTree T = new BiTNode ;
T -> c = * pre;
char * p = in;
while ( p) {
if ( * p == * pre)
break ;
p++ ;
}
int len = p - in;
T -> lchild = Build ( pre + 1 , in, len) ;
T -> rchild = Build ( pre + 1 + len, p+ 1 , n - len - 1 ) ;
return T ;
}
int Hight ( BiTree T ) {
if ( ! T )
return 0 ;
int m = Hight ( T -> lchild) ;
int n = Hight ( T -> rchild) ;
if ( m > n)
return ++ m;
else
return ++ n;
}
int main ( ) {
int n, h;
char pre[ 51 ] , in[ 51 ] ;
BiTree T ;
scanf ( "%d%s%s" , & n, & pre, & in) ;
T = Build ( pre, in, n) ;
h = Hight ( T ) ;
printf ( "%d\n" , h) ;
return 0 ;
}
#include< stdio. h>
int main ( )
{
int n;
scanf ( "%d" , & n) ;
int a[ n] ;
for ( int i= 0 ; i< n; i++ )
{
scanf ( "%d" , & a[ i] ) ;
}
for ( int i= 1 ; i< n; i++ )
{
int min= a[ i] ;
int j= i- 1 ;
for ( j= i- 1 ; j>= 0 ; j-- )
{
if ( min< a[ j] )
{
a[ j+ 1 ] = a[ j] ;
}
else
{
break ;
}
}
a[ j+ 1 ] = min;
for ( int k= 0 ; k< n; k++ )
{
printf ( "%d" , a[ k] ) ;
if ( k< n- 1 )
{
printf ( " " ) ;
}
}
if ( i< n- 1 )
{
printf ( "\n" ) ;
}
}
return 0 ;
}