第一题

代码:
#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>using namespace std;int main() {int x, y;cout << "请输入x的值:" ;cin >> x;if (x < 5) {y = x;}else if (x>=5&&x<15){y = x + 6;}else {y = x - 6;}cout << "y = " << y << endl;return 0;}
第二题
输入一个小写字母,将其转换为大写字母。
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main() {
char c1, c2;
printf("请输入一个小写字母:");
scanf("%c1", &c1);
c2 = c1 + 32;
printf("对应大写字母为:%c\n", c2);
return 0;
}
第三题
求一个 3×3 矩阵主对角线元素之和。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main() {
int A[3][3] = { 0 };
cout << "请输入一个3*3的方阵:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> A[i][j];
}
}
int sum = 0;
sum = A[0][0] + A[1][1] + A[2][2];
cout << "主对角元素之和:" << sum << endl;
}
第四题
求 100~999 之间的水仙花数。所谓水仙花数,是指一个 3 位数,它的每位数 字的立方之和等于该数。例如,因为 153=1+5+3,所以 153 为水仙花数
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int num = 100;
int a = 0, b = 0, c = 0;
cout << "打印水仙花数(100-999):" << endl;
while (num < 1000)
{
a = num % 10; //个位
b = num / 10 % 10; //十位
c = num / 100; //百位
if (a * a * a + b * b * b + c * c * c == num) {
cout << num << endl;
}
num++;
}
}
第五题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int num = 0;
cout << "请输入成绩:" << endl;
cin >> num;
if (num >= 0 && num <= 100)
{
if (num >= 90 && num <= 100)
{
cout << "优秀!!!" << endl;
}
else if (num >= 80 && num <= 89)
{
cout << "良好!!!" << endl;
}
else if (num >= 70 && num <= 79)
{
cout << "中等!!!" << endl;
}
else if (num >= 60 && num <= 69)
{
cout << "合格!!!" << endl;
}
}
else
{
cout << "成绩输入错误!" << endl;
}
return 0;
}
第六题

代码(非递归):
#include<stdio.h>
int fbnq(int n)
{
int a = 1;
int b = 1;
int c = a;
while (n > 2)
{
c = a + b;//从第三个数开始,斐波那契数等于前两个数的和;
a = b;//将前一个数给到a,开始下一次求值
b = c;//将斐波那契数给b,开始下一次求值
n--;//每求一次,n都要减一
}
return c;
}
int main()
{
int n = 0;
scanf_s("%d", &n);
int ret = fbnq(n);
printf("第%d个斐波那契数为%d\n", n, ret);
return 0;
}
第七题

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int num[100],n;
cout << "输入整数的个数为:" ;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
for (int i = 0,j=n-1; i < n/2; i++,j--)
{
int temp = num[j];
num[j] = num[i];
num[i] = temp;
}
for (int i = 0; i < n; i++)
{
cout << num[i] << " ";
}
return 0;
}
第八题
编写一个程序根据输入的三角形的三条边判断是否能组成三角形,如果可以则输出它的面积和三角形类型(等边、等腰、直角三角形)。
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main() {
int a = 0, b = 0, c = 0;
cout << "请输入三条边长:" << endl;
cin >> a >> b >> c;
if (a == b == c)
{
cout << "三角形为等边三角形" << endl;
}
else if (a == b || a == c || b == c)
{
cout << "三角形为等腰三角形" << endl;
}
else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a)
{
cout << "三角形为直角三角形" << endl;
}
else if(a+b>c||a+c>b||b+c>a)
{
cout << "输入错误!!!" << endl;
}
int p, S;
p = (a + b + c) / 2;
S = sqrt(p * (p - a) * (p - b) * (p - c));
cout << "面积为:" << S << endl;
return 0;
}
第九题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int score, min=100, max=0;
cout << "请输入学生成绩:";
while (1)
{
cin >> score;
if (score < 0)
{
break;
}
else
{
if (score < min)
{
min = score;
}
if (score > max)
{
max = score;
}
}
}
cout << "最高得分为:" << max << " 最低得分为:" << min;
return 0;
}
第十题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int func(int F)
{
int C = (F - 32) * 5 / 9;
return C;
}
int main()
{
int F;
cout << "请输入华氏度:" ;
cin >> F;
cout << "对应的摄氏度为:" << func(F);
return 0;
}
第十一题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int num,i=2;
cout << "请输入一个数:" << endl;
cin >> num;
cout << num << "=1";
for (; i <= num;)
{
if (num % i == 0)
{
num = num / i;
cout << "*" << i;
continue;
}
i++;
}
return 0;
}
第十二题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int A[100], n=0,num,s=0;
cout << "请输入一个有序的数组(输入负数结束):" << endl;
while (1)
{
cin >> A[n];
if (A[n] < 0)
{
break;
}
n++;
}
cout << "输入你要插入的数:" << endl;
cin >> num;
for (int i = 0; i < n; i++)
{
if (A[i] < num)
{
s++;
}
}
for (int i = n; i >s; i--)
{
A[i] = A[i - 1];
}
A[s] = num;
cout << "插入后:" << endl;
for (int i = 0; i < n + 1; i++)
{
cout << A[i] << " ";
}
return 0;
}
第十三题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
cout << "在 100~200 之间用 3 除余 2,用 5 除余 3 和用 7 除余 2 的所有整数为:" << endl;
for (int i = 100; i <= 200; i++)
{
if (i % 3 == 2 && i % 5 == 3 && i % 7 == 2)
{
cout << i << " ";
}
}
return 0;
}
第十四题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
int A[20], n=0, m=0, sum = 0;
cout << "输入十名学生成绩:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> A[i];
if (A[i] < 0 || A[i]>100)
{
cout << "输入错误!" << endl;
break;
}
if (A[i] >= 0 && A[i] < 60)//统计不及格人数
{
n++;
}
if (A[i] > 80)
{
m++;
}
sum += A[i];
}
cout << "不及格人数为:" << n << "人" << endl;
cout << "80分以上人数为:" << m << "人" << endl;
cout << "平均分为:" << sum / 10.0;
return 0;
}
第十五题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "请输入一个字符:" << endl;
cin >> c;
if (c >='a' && c <= 'z')
{
cout << "小写字母!" << endl;
}
else if (c >= 'A' && c <= 'Z')
{
cout << "大写字母!" << endl;
}
else
{
cout << "其他字符!" << endl;
}
return 0;
}
效果图:

第十六题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
double funs(double redius)
{
double S,pi = 3.14;
return redius * redius * pi;
}
double funv(double redius, double h)
{
double V, pi = 3.14;
return redius * redius * pi*h;
}
int main()
{
double r, h;
cout << "请输入圆的半径和周长:" << endl;
cin >> r >> h;
cout << "圆的面积为:" << funs(r) << endl;
cout << "圆的体积为:" << funv(r, h) << endl;
return 0;
}
第十七题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 0, b = 0, c = 0,d=0;
string str;
cout << "请输入一个字符串:" << endl;
getline(cin, str);
int n = str.length();
for (int i = 0; i < n; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
a++;
}
if (str[i] >= 'A' && str[i] <= 'Z')
{
a++;
}
if (str[i] == ' ')
{
b++;
}
if (str[i] >= '0' && str[i] <= '9')
{
c++;
}
}
d = n - a - b - c;
cout << "字母个数为:" << a << "\t空格个数为:" << b << "\t数字个数为:" << c << "\t其他字符数:" << d;
return 0;
}
第十八题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n = 5;
for (int i = 1; i <= 5; i++)
{
for (int j = i; j > 1; j--)
{
cout << j<<" ";
}
for (int k = 0; k < n - i; k++)
{
cout << "1 ";
}
cout << endl;
}
return 0;
}
第十九题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
long func(int n)
{
long sum = 0;
if (n == 1)
{
sum = 12;
}
else
{
sum = func(n - 1) + n * 10 + 2;
}
return sum;
}
int main()
{
int n;
cin >> n;
cout << func(n) << endl;
return 0;
}
第20题
第21题

代码:
#include<iostream>
#include<stdlib.h>
using namespace std;
int NextTime(int year, int month, int date, int hour, int minute, int second)
{
int Days = 0;
//首先判断输入日期格式是否正确
if (year < 0 || month < 1 || month>12 || date < 1 || date>31 || hour < 0 || hour>23 || minute < 0 || minute>59 || second < 0 || second>59)
{
cout << "格式输入错误!!!" << endl;
}
second += 1;
if (second == 60)
{
second = 0;
minute += 1;
if (minute == 60)
{
minute = 0;
hour += 1;
if (hour == 24)
{
hour = 0;
date += 1;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Days = 31;
break;
case 2:
if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0)
{
Days = 29;
}
else {
Days = 28;
}
break;
default:
Days = 30;
break;
}
if (date > Days)
{
date = 1;
month += 1;
}
if (month > 12)
{
month = 1;
year += 1;
}
}
}
}
cout << "下一秒是" << year << "年" << month << "月" << date << "日" << hour << "时" << minute << "分" << second << "秒" << endl;
return 1;
}
int main()
{
int YEAR, MONTH, DATE, HOUR, MINUTE, SECOND;
cout << "请输入年份&月份&天数&小时&分钟&秒数" << endl;
cin >> YEAR >> MONTH >> DATE >> HOUR >> MINUTE >> SECOND;
cout << "您输入的是" << YEAR << "年" << MONTH << "月" << DATE << "日" << HOUR << "时" << MINUTE << "分" << SECOND << "秒" << endl;
NextTime(YEAR, MONTH, DATE, HOUR, MINUTE, SECOND);
system("pause");
return 0;
}
第22题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a, b;
cout << "请输入两个整数:" << endl;
cin >> a >> b;
cout << "交换前:" << "a = " << a << " b = " << b << endl;
a = b - a;
b = b - a;
a = a + b;
cout << "交换后:" << "a = " << a << " b = " << b << endl;
return 0;
}
第23题

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int i = 0, j, count = 0;
getline(cin, str);
while (str[i++] != '\0')
{
if (str[i] == ' ')
{
i++;
while (str[i] == ' ')//空格连续
{
i++;
}
count++;
}
}
printf("单词数为: % d\n", count + 1);
}

第24题

代码:
/* 八皇后问题 */
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int ack[10][10];
char que[10][10];
int cnt = 0;
void init(int n);//初始化
void print(int n);//数组的打印
void copy(int a[10][10], int b[10][10], int n);//把a数组拷贝到b数组
void dfs(int k, int n);//核心递归算法
void update_ack(int x, int y, int n);//更新ack数组
int main()
{
int n=8;
printf("8皇后问题\n");
init(n);
dfs(0, n);
printf("8皇后排列组合个数 = %d\n", cnt);
return 0;
}
void init(int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
ack[i][j] = 0;
que[i][j] = '.';
}
}
void print(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%c ", que[i][j]);
printf("\n");
}
printf("\n");
}
void dfs(int k, int n) {
// k==n 表示找到一组解 打印
if (k == n) {
print(n);
cnt++;
return;
}
for (int i = 0; i < n; i++) {
// 在一层循环判断是否可以放置皇后,如果可以进行以下操作(按列):
if (ack[k][i] == 0) {
// 1. 备份attack数组(备份后,最后恢复)
int tmp[10][10];
copy(ack, tmp, n);
// 2. 标记该位置为Q(2. 存储位置)`queen[k][i]='Q'` (仅改变一个地方,最后直接改回来)
que[k][i] = 'Q';
// 3. 上一步放置了皇后,所以更新attack数组
update_ack(k, i, n);
// 4. 递归试探k+1行的皇后位置(按行)
dfs(k + 1, n);
// 5. 递归完成后,恢复attack数组和 `queen[k][i]='.' `
copy(tmp, ack, n);
que[k][i] = '.';
}
}
}
void copy(int a[10][10], int b[10][10], int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
b[i][j] = a[i][j];
}
void update_ack(int x, int y, int n) {
int dx[] = { -1,-1,-1,0, 1, 1, 1, 0 };
int dy[] = { -1,0, 1, 1, 1, 0,-1,-1 };
ack[x][y] = 1;
for (int i = 1; i <= n - 1; i++)//从1延伸到n-1的距离
for (int j = 0; j < 8; j++) {//从八个方向延伸
int nx = x + i * dx[j];
int ny = y + i * dy[j];
if (nx >= 0 && nx < n && ny >= 0 && ny < n)
ack[nx][ny] = 1;
}
}
第25题

#include <stdio.h>
void compare(char *pt1,char *pt2)
{
char *a=pt1,*b=pt2; // 记录指针变量初值
while(pt1<a+11) // 指针指到第12个元素
{
pt1++;
}
while(pt2<b+3) // 指针指到第4个元素
{
pt2++; // 当前为4
}
while(pt2<b+16) // 结束条件是指到第17个元素
{
*pt1++=*pt2++; // a的第12个元素之后开始用b中5-17代替
}
*pt1 = '\0';
}
int main(){
char a[1024]="My name is Li jilin.", b[1024]="Mr.Zhang Haoling is very happy.";
compare(a,b);
puts(a);
return 0;
}
第26题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
int main()
{
double sum = 0;
for (int i = 2; i <= 20;)
{
sum += 1.0 / i;
i += 2;
}
cout << "1/2+1/4+1/6+1/8+…+1/20 = " << sum << endl;
}
第27题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
typedef struct LNode
{
int data;
struct LNode* next;
}LNode;
LNode* FindLink(LNode* head, int X) //查找
{
LNode* chick = head->next;
while (chick != NULL)
{
if (chick->data == X)
return chick;
else
chick = chick->next;
}
return NULL;
}
void DeleteLink(LNode* head, int X)
{
LNode* p;
LNode* q;
p = FindLink(head, X);
q = p->next;
p->data = q->data;
p->next = q->next;
free(q);
}
int main()
{
int num = 6, n;
LNode* head, * tail, * p, * q,*k;
head = (LNode*)malloc(sizeof(LNode));
tail = head;
cout << "请输入一个整数链表(6个数):" << endl;
while (num--)
{
p = (LNode*)malloc(sizeof(LNode));
cin >> n;
p->data = n;
tail->next = p;
tail = p;
}
tail->next = NULL;
q = head->next;
cout << "链表删除前:" << endl;
for (int i = 0; i < 6; i++)
{
if (i == 5)
{
cout << q->data;
}
else
{
cout << q->data << "->";
}
q = q->next;
}
cout << endl;
int A;
cout << "请输入要删除的数:" << endl;
cin >> A;
DeleteLink(head, A);
k = head->next;
cout << "链表删除后:" << endl;
for (int i = 0; i < 5; i++)
{
if (i == 4)
{
cout << k->data;
}
else
{
cout << k->data << "->";
}
k = k->next;
}
return 0;
}
第28题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
void BubbleSort(int a[], int len)
{
int i, j, temp;
for (j = 0; j < len - 1; j++)
{
for (i = 0; i < len - 1 - j; i++)
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
int main()
{
int A[12],temp;
cout << "请输入10个整数:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> A[i];
}
cout << "冒泡排序后:" << endl;
/*for (int i = 0; i < 10-1; i++)
{
for (int j = 0; i < 9 - i; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}*/
BubbleSort(A, 10);
for (int i = 0; i < 10; i++)
{
cout << A[i] << " ";
}
return 0;
}
第29题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
int func(int n)
{
if (n == 3)
{
return 1;
}
if (n == 2)
{
return 2;
}
if (n == 1)
{
return 4;
}
if (n == 0)
{
return 8;
}
}
int main()
{
string str;
int n = 0, sum1 = 0,sum2=0,sum3=0,sum4=0;
cout << "请输入一串二进制数字(16): "<< endl;
getline(cin, str);
for (int i = 3; i >=0; i--)
{
if (str[i] == '1')
{
sum1 += func(i);
}
}
for (int i = 7; i >=4; i--)
{
if (str[i] == '1')
{
sum2 += func(i-4);
}
}
for (int i = 11; i >=8; i--)
{
if (str[i] == '1')
{
sum3 += func(i-8);
}
}
for (int i = 15; i >=12; i--)
{
if (str[i] == '1')
{
sum4 += func(i-12);
}
}
cout << sum1 <<" " << sum2 <<" "<< sum3 <<" " << sum4 << endl;
cout << "转化为十进制和:" << sum1 +sum2 + sum3 + sum4 << endl;
return 0;
}
第30题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
void YSF(int n, int k, int m)
{
int end = n + 1;
int* A = (int*)malloc(sizeof(int) * end);
//i为每个人的编号
//flag为每个人报出的数字
//count为已经已经出列的人数
int i = 0, flag = 1, count = 0;
for (; i <= n; i++)
{
A[i] = 0;
}
//从k开始叫号
i = k;
while (count != n - 1)
{
if (i == end) i = 1;
if (A[i] != 1)
{
if (flag > m) flag = 1;
if (flag == m)
{
cout << i << " ";
A[i] = 1;
count++;
}
flag++;
}
i++;
}
for (int i = 1; i <= n; i++)
{
if (A[i] == 0) cout << i;
}
}
int main()
{
int n, k, m;
cout << "请输入n(人数),k(起始),m(间隔):";
cin >> n >> k >> m;
cout << "游戏开始前:" << endl;
for (int i = 1; i <= n; i++)
{
cout << i << " ";
}
cout << endl;
cout << "出列顺序:" << endl;
YSF(n, k, m);
return 0;
}
第31题

代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
char head[100];
int len1;
String(string s,int len)
{
len1 = len;
for (int i = 0; i < len; i++)
{
head[i] = s[i];
}
}
void print(char head[])
{
for (int i = 0; i < len1; i++)
{
cout << head[i];
}
cout << endl;
}
void reverse(char head[])
{
for (int i = 0,j=len1-1; i < len1 / 2; i++,j--)
{
char temp = head[i];
head[i] = head[j];
head[j] = head[i];
}
}
};
int main()
{
string s;
cout << "请输入字符串:";
getline(cin, s);
//int len = sizeof(s) / sizeof(s[0]);
int len = s.length();
String S = String(s,len);
cout << "逆置前:" << endl;
S.print(S.head);
S.reverse(S.head);
cout << "逆置后:" << endl;
S.print(S.head);
return 0;
}
第32题

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
class Box
{
public:
int C, H, K;
Box(int c, int h, int k)
{
C = c;
H = h;
K = k;
}
int funs()
{
return 2 * C * H + 2 * C * K + 2 * H * K;
}
int funv()
{
return C * H * K;
}
};
int main()
{
int c, h, k;
cout << "请输入盒子的长高宽:";
cin >> c >> h >> k;
cout << endl;
Box b=Box(c, h, k);
cout << "表面积为:" << b.funs() << " 体积为: " << b.funv();
return 0;
}
第33题

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
class Tree
{
public:
int ages;
void grow(int years)
{
ages = years;
}
void ShowAge()
{
cout << "这棵树的年龄是" << ages << endl;
}
};
int main()
{
Tree t;
int year;
cout << "请给树输入一个年龄:" << endl;
cin >> year;
t.grow(year);
t.ShowAge();
return 0;
}
第34题

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
class Student {
friend void func(Student &s);
public:
void getscore(int score)
{
this->score = score;
}
private:
int score;
};
void func(Student &s)
{
int score = s.score;
if (score >= 90)
{
cout << "优"<<endl;
}
else if(score>=80&&score<90)
{
cout << "良" << endl;
}
else if (score >= 70 && score < 80)
{
cout << "中" << endl;
}
else if (score >= 60 && score < 70)
{
cout << "及格" << endl;
}
else if (score < 60)
{
cout << "不及格" << endl;
}
}
int main() {
int score;
Student S;
cout << "请输入学生的分数:" << endl;
cin >> score;
S.getscore(score);
func(S);
return 0;
}
第35题

#include<iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(int r, int i) :real(r), imag(r) {};
friend Complex operator+(Complex& c1, Complex& c2);
void display();
};
void Complex::display()
{
cout << "real=" << real << endl;
cout << "imag=" << imag << endl;
}
Complex operator+(Complex& c1, Complex& c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
Complex c1(3, 4);
Complex c2(5, 8);
Complex c3;
c3 = c1 + c2;
c3.display();
return 0;
}
第36题

#include <iostream>
using namespace std;
class MyTime
{
public:
MyTime(int h = 0, int m = 0, int s = 0) :h(h), m(m), s(s) {}
friend istream& operator>>(istream&, MyTime&);
friend ostream& operator<<(ostream&, MyTime&);
MyTime operator++();
private:
int h, m, s;
};
istream& operator>>(istream& is, MyTime& t) {
is >> t.h >> t.m >> t.s;
return is;
}
ostream& operator<<(ostream& os, MyTime& t) {
os << t.h << ":" << t.m << ":" << t.s;
return os;
}
MyTime MyTime::operator++() {
s += 1;
if (s == 60) {
s = 0;
m += 1;
if (m == 60) {
m = 0;
h += 1;
if (h == 24)h = 0;
}
}
return MyTime(h, m, s);
}
/* 请在这里填写答案 */
int main()
{
MyTime t1, t2(23, 59, 59), t3;
cin >> t3;
++t1;
cout << t1 << endl;
++t2;
cout << t2 << endl;
++t3;
cout << t3 << endl;
return 0;
}
第37题

#include <iostream>
#include<cstring>
#include<math.h>
using namespace std;
class Triangle
{
private:
double a,b,c,s;
public:
Triangle();
Triangle(double a1,double b1,double c1);
friend double operator + (Triangle &s1,Triangle &s2);
};
Triangle::Triangle(double a1,double b1,double c1)
{
a=a1;
b=b1;
c=c1;
double p=(double)(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
}
double operator + (Triangle &s1,Triangle &s2)
{
return s1.s+s2.s;
}
int main()
{
Triangle s1(3,4,5),s2(3,4,5);
cout<<s1+s2;
return 0;
}
第38题

//使用函数重载的方法定义两个重名函数,
//分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。
#include<iostream>
using namespace std;
template<typename T>
T add(T a, T b)
{
T c;
c = a + b;
return c;
}
int main()
{
int a, b, c;
float x, y, z;
cout << "请输入两个整型数:" << endl;
cin >> a >> b;
cout << "请输入两个浮点数:" << endl;
cin >> x >> y;
c = add(a, b);
z = add(x, y);
cout << "整型数之和是:" << c << endl;
cout << "浮点数之和是:" << z << endl;
return 0;
}
第39题

#include <iostream>
using namespace std;
//基类 shape
class Shape {
public:
Shape() {}
~Shape() {}
virtual float getArea() {
return -1;
}
};
//派生类 圆形
class Circle :public Shape {
public:
Circle(float radius):itsRadius(radius){}
~Circle(){}
float getArea() {
return 3.14 * itsRadius * itsRadius;
}
private:
float itsRadius;
};
//派生类 长方形
class Rectangle :public Shape {
public:
Rectangle(float len, float width) :itsLength(len), itsWidth(width) {};
~Rectangle(){}
virtual float getArea() {
return itsLength * itsWidth;
}
virtual float getLength() {
return itsLength;
}
virtual float getWidth() {
return itsWidth;
}
private:
float itsWidth;
float itsLength;
};
//正方形
class Square :public Rectangle {
public:
Square(float len);
~Square() {}
};
Square::Square(float len) :Rectangle(len, len) {
}
int main() {
Shape* sp;
sp = new Circle(5);
cout << "圆形的面积:" << sp->getArea() << endl;
delete sp;
sp = new Rectangle(4, 6);
cout << "长方形的面积:" << sp->getArea() << endl;
delete sp;
sp = new Square(5);
cout << "正方形的面积:" << sp->getArea() << endl;
delete sp;
return 0;
}
第40题

#include <iostream>
using namespace std;
class Counter
{
private:
int sum;
public:
Counter(int ins);
void increase();
void decrease();
int output();
};
Counter::Counter(int ins)
{
sum=ins;
}
void Counter::increase()
{
sum++;
}
void Counter::decrease()
{
sum--;
}
int Counter::output()
{
return sum;
}
int main()
{
int a;
cout<<"请输入计数器初值:";
cin>>a;
Counter b(a);
b.increase();
b.decrease();
b.increase();
b.increase();
cout<<"当前的值为:"<<b.output()<<endl;
return 0;
}
第41题

#include <iostream>
#include <string>
using namespace std;
class Mammel {
private:
string name;
int weight;
public:
Mammel() {
cout << "正在默认构造Mammel对象。" << endl;
}
Mammel(string name, int weight) {
this->name = name;
this->weight = weight;
}
~Mammel();
virtual void speak();
};
void Mammel::speak() {
cout << "Mammel" << endl;
}
Mammel::~Mammel() {
cout << "正在析构Mammel对象。" << endl;
}
class Dog :public Mammel {
private:
int age;
public:
Dog() {
cout << "正在默认构造Dog对象。" << endl;
}
Dog(string name, int weight, int age);
~Dog();
void speak();
};
Dog::Dog(string name, int weight, int age) :Mammel(name, weight), age(age) {
cout << "正在构造Dog对象。" << endl;
}
Dog::~Dog() {
cout << "正在析构Dog对象。" << endl;
}
void Dog::speak() {
cout << "Dog." << endl;
}
int main() {
Dog mydog("haha", 20, 2);
mydog.speak(); //Dog
Mammel* animal1 = &mydog;
(*animal1).speak(); //Dog
Mammel& animal2 = mydog;
animal2.speak(); //Dog
Mammel animal3 = mydog;
animal3.speak(); //Mammel
//通过以上测试我们可以看出派生类转化为基类的三种方法:赋值、指针、引用
//只能通过指针,引用来访问虚函数
return 0;
}
第42题

#include<iostream>
using namespace std;
class base
{
public:
base(double x,double y)
{
a=x;
b=y;
}
virtual void area()=0;
protected:
double a;
double b;
};
class Triangle:public base
{
public:
Triangle(double x,double y):base(x,y)
{};
void area()
{
cout<<"三角形面积为:"<<0.5*a*b<<endl;
}
};
class Circular:public base
{
public:
Circular(double x):base(x,x)
{};
void area()
{
cout<<"圆的面积为:"<<3.14*a*a<<endl;
}
};
class Square:public base
{
public:
Square(double x):base(x,x)
{};
void area()
{
cout<<"正方形面积为:"<<a*a<<endl;
}
};
int main()
{
base *ptr;
Triangle ob1(5,4);
ptr=&ob1;
ptr->area();
Circular ob2(2);
ptr=&ob2;
ptr->area();
Square ob3(3);
ptr=&ob3;
ptr->area();
system("pause");
return 0;
}
第43题

#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
std::string line;
//一次读入一整行
//while (getline(std::cin,line))
//std::cout << line << std::endl;
//一次只读入一个词
while (std::cin >> line)
std::cout << line << std::endl;
system("pause");
return 0;
}
第44题

答:
string类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字符)之前所有的空白字符,然后读取字符直到再次遇到空白字符,读取终止(该空白字符仍留在输入流中)
getline 函数对空白字符处理:不忽略行开头的空白字符,读取字符直至遇到换行符,读取终止并丢弃换行符(换行符从输入流中去掉但并不存储在string对象中)
第45题
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
std::string s1, s2;
while (std::cin >> s1){
//字符串直接相加
//s2 = s2 + s1;
//字符串间用空格连接
if (s2.size() == 0)
s2 = s2 + s1;
else
s2 = s2 + " " + s1;
}
std::cout << s2 << std::endl;
system("pause");
return 0;
}
第46题

#include <iostream>
#include <string>
using namespace std;
void main()
{
string mystring("pipi555#@lzy");
int num = mystring.size();
for (int i=0 ; i<num ; i++)
{
if (!ispunct(mystring[i]))
cout << mystring[i];
}
}
第47题

#include <iostream>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<int> ivec(10,5);
std::vector<int>::iterator iter;
for (iter = ivec.begin(); iter != ivec.end(); iter++){
*iter *= 2;
std::cout << *iter << " ";
}
std::cout << std::endl;
system("pause");
return 0;
}
第48题

#include <iostream>
#include <string>
void main()
{
//比较两个string对象是否相等
std::string str1 = "hello";
std::string str2 = "hello";
if (str1 == str2)
std::cout << "Equal!" << std::endl;
else
std::cout << "Not equal!" << std::endl;
//比较两个C风格字符串内容是否相等
char ca1[] = "hello";
char ca2[] = "hello";
std::cout << strcmp(ca1, ca2) << std::endl;
system("pause");
}
第49题

答:
知识点1:几种顺序容器的特点
vector:可变大小,支持快速随机访问,尾部插入数据很快
deque:双端队列。支持快速随机访问,头部插入数据很快
list:双向链表。支持双向顺序访问,在其任何位置插入删除数据都很快
array:固定大小数组,不能改变大小。(注意这里与普通的内置数组类型是不一样的)
string:与vector类似,专用于保存字符。
知识点2:在通常情况下,使用vector是非常好的选择。且新版本的标准库容器比旧版本快很多。C++程序应该使用标准库容器,而不是原始的数据结构如:内置数组。
1,vector更好,vector支持随机访问,操作性更大
2,list更好,因为只针对删除的话链表的删除的效率还是十分之高的
3,vector更好,vector进行排序的效率较其他两种效率高
第50题

vector<int>ans;//空
vector<int>ans(10);//10个值,每个值为0
vector<int>ans={1,2,3,4};//1,2,3,4
vector<int>ans(10,1);//10个值,每个值为1
vector<int>ans(ans1);//拷贝ans1中的元素
vector<int>ans(ans1.begin()+1,ans1.end());//初始化为两个迭代器指定范围的元素
第51题

知识点:容器之间的拷贝,容器的类型和其中元素的类型都必须相同
利用迭代器进行拷贝,只需要其元素的范围,利用的是迭代器范围的对应元素进行初始化
本题可以使用迭代器来进行初始化
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
using namespace std;
int main(int argc, char**argv)
{
//拷贝使用的list和vector
list<int> list1(7,8);
vector<int> vec2 (5,2);
//创建的目标容器
vector<double> vec1(list1.begin(),list1.end());
vector<double> vec3(vec2.begin(),vec2.end());
//验证正确性
vector<double>::iterator it1 = vec1.begin();
vector<double>::iterator it2 = vec3.begin();
for (it1;it1 != vec1.end();it1++)
{
cout<<*it1<<endl;
}
for (it2;it2 != vec3.end();it2++)
{
cout<<*it2<<endl;
}
return 0;
}
第52题

知识点:由于char*类型不同于string但是可以转换成stirng所以可以利用范围赋值方式
// 容器类型不同, 不能直接赋值
// 元素类型相容, 可以采用范围辅助
list<const char*> lst = {"hello", "world"};
vector<string> vec;
vec.assign(lst.begin(), lst.end());
第53题

#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main()
{
// 输入并保存元素
deque<string> deq;
string str;
while(cin >> str) {
deq.push_back(str);
}
// 通过常量迭代器打印元素
for(auto iter = deq.cbegin(); iter != deq.cend(); ++iter) {
cout << (*iter) << endl;
}
system("pause");
return 0;
}
第54题

#include <iostream>
#include <list>
#include <deque>
using namespace std;
int main()
{
// 初始化容器
list<int> lst = {0, 1, 2, 3, 4};
deque<int> odd, even;
// 通过迭代器拷贝
auto iter = lst.cbegin();
auto end = lst.cend();
while(iter != end) {
int num = *iter;
if(num % 2 == 1) // 等价于 if(num & 1) 查看最低位, 1:奇数, 0:偶数
odd.push_back(num);
else
even.push_back(num);
++iter;
}
// 等价于
// for (auto i : lst)
// (i & 0x1 ? odd : even).push_back(i);
// 打印结果验证
for(auto o : odd) { cout << o << " "; }
cout << endl;
for(auto e : even) { cout << e << " "; }
cout << endl;
system("pause");
return 0;
}
第55题

思路:由于已知至少读取 100 个字符,故可用 reverse 函数预先为 string分配 100 个字符的内存空间,然后再逐个读取字符,并用 push_back 末尾追加到 string 中。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void input_string(string s)
{
s.reserve(100); // 预分配 100 个元素的空间
char c;
while(cin >> c)
s.push_back(c);
}
int main()
{
string str;
input_string(str);
cout << str << endl;
system("pause");
return 0;
}
第56题

#include <iostream>
#include <string>
using namespace std;
string fix_prefix_and_suffix(string &name, const string &prefix, const string &suffix) // 注意形参类型
{
// name = prefix + name + suffix;
name.insert(name.begin(), 1, ' '); // 注意区别
name.insert(name.begin(), prefix.begin(), prefix.end()); // 不能只写个 prefix
name.append(" "); // 注意区别
name.append(suffix.begin(), suffix.end()); // 不能只写个 suffix
return name;
}
int main()
{
string s = "Wang";
cout << fix_prefix_and_suffix(s, "Dear", "x") << endl;
system("pause");
return 0;
}
第57题

//map是表示哈希表,vector相当于数组
//list适用于链表,vector适用于数组类,deque是双端队列,map是哈希表,set是集合
#include<iostream>
#include<algorithm>
#include<map>
#include<unordered_map>
using namespace std;
unordered_map<string, vector<string>>mp;
void add_family(string name,vector<string>son) {
mp[name] = son;
}
void add_son(string family, string son_name) {
mp[family].emplace_back(son_name);
}
int main() {
string str;
vector<string>name = { "jone","job" };
add_family("mr", name);
add_son("mr", "hello");
return 0;
}
第58题

//map是表示哈希表,vector相当于数组
//list适用于链表,vector适用于数组类,deque是双端队列,map是哈希表,set是集合
#include<iostream>
#include<algorithm>
#include<map>
#include<unordered_map>
using namespace std;
unordered_map<string, vector<string>>mp;
void add(vector<int>& ans,int num) {
if (ans.empty()) {
ans.emplace_back(num);
return;
}
else
for (auto curr : ans) {
if (curr == num)return;
}
ans.emplace_back(num);
}
int main() {
int num;
vector<int>ans;
while (cin>>num) {
add(ans, num);
}
for (auto& num : ans)cout << num << " ";
return 0;
}
**set的效率更高**
第59题
第60题






