1.输入n个数进行排序,要求先按奇偶后按从小到大的顺序排序。
# include <iostream>
# include <algorithm>
using namespace std;
bool comp ( int lhs, int rhs) {
if ( lhs % 2 == 1 && rhs % 2 == 1 && lhs < rhs) {
return true ;
}
else if ( lhs % 2 == 1 && rhs % 2 == 0 ) {
return true ;
}
else if ( lhs % 2 == 0 && rhs % 2 == 0 && lhs < rhs) {
return true ;
}
else {
return false ;
}
}
int main ( ) {
int n;
cin >> n;
int arr[ 1000 ] = { 0 } ;
for ( int i = 0 ; i < n; i++ ) {
cin >> arr[ i] ;
}
sort ( arr, arr + n, comp) ;
for ( int i = 0 ; i < n; i++ ) {
cout<< arr[ i] << ' ' ;
}
cout << endl;
}
2.给定两个元素有序(从小到大)的链表,要求将两个链表合并成一个有序(从小到大)链表
# include <iostream>
using namespace std;
typedef struct node {
int data;
struct node * next;
} LNode, * LinkList;
LinkList MergeList ( LinkList L1, LinkList L2) {
LinkList L3= ( LinkList) malloc ( sizeof ( LNode) ) ;
L3-> next = NULL ;
LNode* r3 = L3;
LNode* p1 = L1-> next;
LNode* p2 = L2-> next;
while ( p1 != NULL && p2 != NULL ) {
LNode* s = ( LNode* ) malloc ( sizeof ( LNode) ) ;
if ( p1-> data < p2-> data) {
s = p1;
p1 = p1-> next;
s-> next = r3-> next;
r3-> next = s;
r3 = s;
}
else {
s = p2;
p2 = p2-> next;
s-> next = r3-> next;
r3-> next = s;
r3 = s;
}
}
if ( p1 != NULL )
r3-> next = p1;
if ( p2 != NULL )
r3-> next = p2;
return L3;
}
int main ( ) {
int s1;
cin >> s1;
LinkList L1 = ( LinkList) malloc ( sizeof ( LNode) ) ;
L1-> next = NULL ;
LNode * r1 = L1;
for ( int i = 0 ; i < s1; ++ i) {
LNode * s= ( LNode * ) malloc ( sizeof ( LNode) ) ;
cin >> s-> data;
s-> next = r1-> next;
r1-> next = s;
r1 = s;
}
r1-> next = NULL ;
int s2;
cin >> s2;
LinkList L2 = ( LinkList) malloc ( sizeof ( LNode) ) ;
L2-> next = NULL ;
LNode* r2 = L2;
for ( int i = 0 ; i < s2; ++ i) {
LNode* s = ( LNode* ) malloc ( sizeof ( LNode) ) ;
cin >> s-> data;
s-> next = r2-> next;
r2-> next = s;
r2 = s;
}
r2-> next = NULL ;
LinkList L3= MergeList ( L1, L2) ;
LNode* p = L3;
while ( p-> next != NULL ) {
cout << p-> next-> data << ' ' ;
p = p-> next;
}
}
3.给你一个字符串S,要求你将字符串中出现的所有"gzu"子串删除,输出删除之后的S。
# include <iostream>
using namespace std;
int main ( ) {
string str;
cin >> str;
string s1 = { "gzu" } ;
for ( int i = 0 ; i< str. size ( ) ; i++ ) {
int j = i, t = 0 ;
string s2;
while ( t < 3 ) {
s2 += str[ j] ;
t++ ;
j++ ;
}
if ( s2 == s1) {
str. erase ( i, 3 ) ;
}
s2. clear ( ) ;
}
cout << str << endl;
}
4.击鼓传花
有N个小朋友围成一圈玩击鼓传花游戏,将小朋友编号为1 ~ N,从1 号开始传花,每次传3 个,拿到花的小朋友表演节目后退出,任给N,问最后一个表演的小朋友编号是多少?
例如:5 个小朋友,从1 号开始传花,第一个表演的是3 号,第二个表演的是1 号,第三个表演的是5 号,第四个表演的是2 号,最后一个表演的是4 号。(建议用链表实现)
`
```cpp
# include <iostream>
# include <queue>
using namespace std;
int main ( ) {
int n;
cin >> n;
queue< int > kid;
for ( int i = 0 ; i < n; ++ i) {
kid. push ( i + 1 ) ;
}
int num = 1 ;
while ( ! kid. empty ( ) ) {
int cur = kid. front ( ) ;
kid. pop ( ) ;
if ( num == 3 ) {
num = 1 ;
if ( kid. empty ( ) ) {
cout << cur << endl;
break ;
}
}
else {
num++ ;
kid. push ( cur) ;
}
}
}
6.输入N个整数,输出这个整数两两组合且不重复的所有二元组,要求从小到大输出并且用括号的形式。
# include <iostream>
# include <algorithm>
using namespace std;
int main ( ) {
int n;
cin >> n;
int arr[ 30 ] ;
for ( int i = 0 ; i < n; i++ ) {
cin >> arr[ i] ;
}
sort ( arr, arr + n) ;
for ( int i = 0 ; i < n; ++ i) {
for ( int j = 0 ; j < n; ++ j) {
if ( i != j) {
cout << '(' << arr[ i] << ',' << arr[ j] << ')' << endl;
while ( arr[ j] == arr[ j + 1 ] )
j++ ;
}
}
while ( arr[ i] == arr[ i + 1 ] )
i++ ;
}
}
7.输入一个整数,输出其最长连续因子。
# include <iostream>
# include <queue>
using namespace std;
queue< int > fun ( int num) {
queue< int > yz;
for ( int i = 2 ; i < num; ++ i) {
if ( num % i == 0 ) {
yz. push ( i) ;
continue ;
}
else
break ;
}
return yz;
}
int main ( ) {
int num;
cin >> num;
queue< int > yz = fun ( num) ;
while ( ! yz. empty ( ) ) {
cout << yz. front ( ) << ' ' ;
yz. pop ( ) ;
}
}
8.输入一个整数,判断该整数是否为素数,若是,输出该整数,若否,输出大于该整数的第一个素数。(例如,输入为14,输出17,因为17是大于14的第一个素数)
# include <iostream>
using namespace std;
bool isNum ( unsigned int n) {
if ( n <= 1 )
return false ;
for ( short int i = 2 ; i < n; ++ i) {
if ( n % i == 0 ) {
return false ;
}
}
return true ;
}
int main ( ) {
unsigned int num;
cin >> num;
if ( isNum ( num) ) {
cout << num << endl;
}
else {
unsigned int i = num + 1 ;
while ( true ) {
if ( isNum ( i) ) {
cout << i << endl;
break ;
}
else
i++ ;
}
}
}