一、 编程计算1/1!-1/3!+1/5!-…+(-1)(n+1)/(2n-1)!
#include <iostream>
using namespace std;
int main ( ) {
int n;
cin >> n;
int t = 1 ;
double num = 1 ;
double sum = 0 ;
double temp = num;
while ( n> 0 ) {
sum + = 1 / temp * t;
t = - t;
num++ ;
temp * = num;
num++ ;
temp * = num;
n-- ;
}
cout << sum;
return 0 ;
}
二、 甲乙丙对一次竞赛名次进行预测。
甲:A第1 B第3
乙:C第1 D第4
丙:D第1 B第3 (数据不是原题,记不太清,但解题方法是一样的)
他们都说对了一半,求ABCD正确的名次(ABCD不会出现相同的名次)
#include <iostream>
using namespace std;
int main ( ) {
int A, B, C, D;
for ( A = 1 ; A <= 4 ; A++ ) {
for ( B = 1 ; B <= 4 ; B++ ) {
if ( ( A == 1 && B != 3 ) || ( A != 1 && B == 3 ) && A!= B) {
for ( C = 1 ; C <= 4 ; C++ ) {
if ( A != C && B != C) {
for ( D = 1 ; D <= 4 ; D++ ) {
if ( D!= A&& D!= B&& D!= C&& ( ( C == 1 && D != 4 ) || ( C != 1 && D == 4 ) )
&& ( ( D == 1 && B != 3 ) || ( D != 1 && B == 3 ) ) ) {
cout << "A:" << A << endl;
cout << "B:" << B << endl;
cout << "C:" << C << endl;
cout << "D:" << D << endl;
cout << endl;
}
}
}
}
}
}
}
return 0 ;
}
三、 给定链表节点的定义
struct Node{
int data;
Node *next;
}
请编写一个函数,用递归的方式:对两个有序链表合并成一个有序链表。
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
} ;
void merge ( Node* newList, Node* list1, Node* list2) {
if ( list1 != NULL && list2 != NULL ) {
if ( list1- > data <= list2- > data) {
newList- > next = list1;
newList = newList- > next;
merge ( newList, list1- > next, list2) ;
}
else {
newList- > next = list2;
newList = newList- > next;
merge ( newList, list1, list2- > next) ;
}
}
if ( list1!= NULL && list2== NULL ) {
newList- > next = list1;
newList = newList- > next;
merge ( newList, list1- > next, NULL ) ;
}
if ( list2 != NULL && list1== NULL ) {
newList- > next = list2;
newList = newList- > next;
merge ( newList, NULL , list2- > next) ;
}
}
int main ( ) {
Node* newList = new Node{ NULL , NULL } ;
Node* list1 = new Node{ NULL , NULL } ;
Node* LinkList1 = list1;
cout << "请依次输入链表1的值,当输入-1时结束:" ;
int data;
cin >> data;
while ( data != - 1 ) {
Node* newNode = new Node{ data, NULL } ;
list1- > next = newNode;
list1 = list1- > next;
cin >> data;
}
Node* list2 = new Node{ NULL , NULL } ;
Node* LinkList2 = list2;
cout << "请依次输入链表2的值,当输入-1时结束:" ;
int data2;
cin >> data2;
while ( data2 != - 1 ) {
Node* newNode = new Node{ data2, NULL } ;
list2- > next = newNode;
list2 = list2- > next;
cin >> data2;
}
merge ( newList, LinkList1- > next, LinkList2- > next) ;
Node* p = newList- > next;
for ( ; p!= NULL ; p= p- > next) {
cout << p- > data << " " ;
}
}
请依次输入链表1 的值,当输入- 1 时结束:1 3 4 5 8 - 1
请依次输入链表2 的值,当输入- 1 时结束:3 6 9 10 11 14 - 1
合成后的链表: 1 3 3 4 5 6 8 9 10 11 14
四、 现有一个酒店场景。定义一个客人类Guest。包含成员属性:编号Num、姓名Name、房费Fee、当前酒店入住人数Count。其中编号Num需要程序自动生成。现在要求实现以下Guest的成员函数:构造函数、Show()显示Guest的信息、GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。并定义3个Guest对象来对成员函数进行测试。
#include <iostream>
#include <iostream>
using namespace std;
static int Total = 0 ;
class Guest {
private :
int Num;
string Name;
double Fee;
int Count;
public :
Guest ( ) ;
Guest ( string n, double f, int c) ;
void Show ( ) ;
int getCount ( ) ;
static double getTotalIncome ( Guest* guest, int n) ;
} ;
Guest:: Guest ( ) { }
Guest:: Guest ( string n, double f, int c) {
Num = Total++ ;
Name = n;
Fee = f;
Count = c;
}
void Guest:: Show ( ) {
cout << "Num:" << Num << endl;
cout << "Name:" << Name << endl;
cout << "Fee:" << Fee << endl;
cout << "Count:" << getCount ( ) << endl;
cout << endl;
}
int Guest:: getCount ( ) {
return Count;
}
double Guest:: getTotalIncome ( Guest * guest, int n) {
double sum = 0 ;
int i = 0 ;
for ( int i= 0 ; i< n; i++ ) {
sum + = ( guest+ i) - > Fee;
}
return sum;
}
int main ( ) {
Guest guests[ 3 ] = {
Guest ( "张三" , 999 , 3 ) ,
Guest ( "李四" , 200 , 1 ) ,
Guest ( "王五" , 1200 , 4 )
} ;
for ( int i = 0 ; i < 3 ; i++ ) {
guests[ i] . Show ( ) ;
}
double totalIncome = Guest:: getTotalIncome ( guests, 3 ) ;
cout << "totalIncome:" << totalIncome;
return 0 ;
}
五、现有一抽象类Shape,它拥有一系列虚函数:Input()输入类需要的信息、Show()显示类的信息、Perimeter()计算周长、Area()计算面积。先定义Circle、Square、Triangle来继承Shape并实现其虚函数。要求创建Circle、Square、Triangle的对象,用基类指针指向这些对象,并调用成员函数进行测试。
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public :
virtual void Input ( ) = 0 ;
virtual void Show ( ) = 0 ;
virtual double Perimeter ( ) = 0 ;
virtual double Area ( ) = 0 ;
} ;
class Circle : public Shape {
private :
double radius;
public :
Circle ( ) { }
void Input ( ) {
cout << "请输入圆半径radius:" ;
cin >> radius;
}
void Show ( ) {
cout << "radius:" << radius << endl;
cout << "perimeter" << Perimeter ( ) << endl;
cout << "Area" << Area ( ) << endl;
cout << endl;
}
double Perimeter ( ) {
return 3.14 * 2 * radius;
}
double Area ( ) {
return 3.14 * radius * radius;
}
} ;
class Square : public Shape {
private :
double len;
public :
Square ( ) { }
void Input ( ) {
cout << "请输入正方形边长len:" ;
cin >> len;
}
void Show ( ) {
cout << "len:" << len << endl;
cout << "perimeter:" << Perimeter ( ) << endl;
cout << "area:" << Area ( ) << endl;
cout << endl;
}
double Perimeter ( ) {
return 4 * len;
}
double Area ( ) {
return len * len;
}
} ;
class Triangle : public Shape {
private :
double a, b, c;
public :
Triangle ( ) { }
void Input ( ) {
cout << "请输入三角形边长a,b,c:" ;
cin >> a >> b >> c;
}
void Show ( ) {
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "c:" << c << endl;
cout<< "perimeter:" << Perimeter ( ) << endl;
cout<< "area:" << Area ( ) ;
cout << endl;
}
double Perimeter ( ) {
return a + b + c;
}
double Area ( ) {
double s = ( a + b + c) / 2 ;
return sqrt ( s * ( s - a) * ( s - b) * ( s - c) ) ;
}
} ;
int main ( ) {
Shape* p;
p = new Circle;
p- > Input ( ) ;
p- > Show ( ) ;
p = new Square;
p- > Input ( ) ;
p- > Show ( ) ;
p = new Triangle;
p- > Input ( ) ;
p- > Show ( ) ;
return 0 ;
}