#include "pch.h"
#include
#include
#include
#include
using namespace std;
struct letter {
char val;
int sum;
letter():val('#'),sum(0){}
};
int main()
{
string str;
vector<letter> L;
letter t;
int i = 0;
cout << "请输入:";
while (cin >> str) {
for (i = 0; i < L.size() && str[0] != L[i].val; i++);
if (i == L.size()) {
t.val = str[0];
t.sum = 1;
L.push_back(t);
}
else {
L[i].sum++;
}
}
for (i = 0; i < L.size(); i++) {
cout << L[i].val << " " << L[i].sum << endl;
}
return 0;
}
#include "pch.h"
#include
#include
#include
#include
#include
using namespace std;
int main()
{
stack<char> s;
vector<char> queue;
string input;
int i = 0;
string str("(aaaa(bbbb(cccc,dddd),eeee(ffff)))");
while (cin >> input) {
while (!s.empty())
s.pop();
queue.clear();
for (i = 0; i < str.size() && str[i] != input[0]; i++);
for (; i >= 0; i--) {
if (str[i] == '(') {
if (!s.empty()) {
s.pop();
}
else {
if (i == 0)
continue;
i--;
queue.push_back(str[i]);
}
}
else if (str[i] == ')') {
s.push(')');
}
}
for (i = queue.size() - 1; i >= 0; i--) {
cout << queue[i] << queue[i] << queue[i] << queue[i];
cout << '>';
}
cout << input;
cout << endl;
}
return 0;
}
先输入一组数,然后输入其分组,按照分组统计出现次数并输出。
例如,输入数据3,2,3,8,8,2,3
输入对应分组 1,2,3,2,1,3,1
输出:
1={2=0,3=2,8=1}
2={2=1,3=0,8=1}
3={2=1,3=1,8=0}
class Solution {
public:
void fun() {
int a = 0;
char c = '0';
vector<int> x;
vector<int> y;
int i = 0, j = 0;
map<int, map<int, int>> M;
//输入
while (cin>>a) {
c = getchar(); x.push_back(a); if (c == '\n') break;
}
while (cin >> a) {
c = getchar(); y.push_back(a); if (c == '\n') break;
}
//计算
for (i = 0; i < y.size(); i++) {
for (j = 0; j < x.size(); j++) {
M[y[i]][x[j]] = 0;
}
}
for (i = 0, j = 0; i < y.size(); i++, j++) {
M[y[i]][x[j]]++;
}
//输出
map<int, map<int, int>>::iterator it;
map<int, int>::iterator it1;
for (it = M.begin(); it != M.end(); it++) {
cout << it->first << "={";
for (it1 = it->second.begin(); it1 != it->second.end(); it1++) {
if (it1 != it->second.begin()) {
cout << ',';
}
cout << it1->first << '=' << it1->second;
}
cout << '}' << endl;
}
}
};
#include
#include
#include
using namespace std;
bool leap_year(int year);
void date(string date);
int main()
{
date("2019-1-12");
date("2018-12-1");
date("2019-12-21");
date("2020-1-19");
date("2020-12-13");
return 0;
}
void date(string date) {//"2019-1-12"
int y = 0, m = 0, d = 0;
int months_len[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int days1 = 0;
int i = 0;
int sum = 0;
sscanf_s(date.c_str(), "%d-%d-%d", &y, &m, &d);
sum = 0;
while (sum < 100) {
d++;
sum++;
if (m == 2 && leap_year(y)) {
months_len[2] = 29;
}
else {
months_len[2] = 28;
}
if(d > months_len[m]) {
d = 1;
m++;
if (m > 12) {
m = 1;
y++;
}
}
}
cout << y << '-' << m << '-' << d << '\n';
}
bool leap_year(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
一个小球,从高为H的地方下落,下落弹地之后弹起高度为下落时的一半,比如第一次弹起高度为H/2,如此反复,计算从小球H高度下落到n次弹地往返的总路程。
要求:(1)用递归的方法实现。(2)输入H和n,输出结果。(3)注意程序的健壮性。(4)可以用C/C++实现。
#include
using namespace std;
double f(double n, double h)
{
if (n == 1.0)
return h + (h / 2);
else {
return h + (h / 2) + f(n - 1, h / 2);
}
}
int main()
{
double h, n;
cout << "请输入高度:" << endl;
while (cin >> h)
{
if (h == 0)
break;
cout << "请输入次数:" << endl;
cin >> n;
cout << "总路程为:" << f(n, h) << endl;
cout << "请输入高度:" << endl;
}
return 0;
}
创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要加入自己的成员变量或成员函数。
要求:(1)输入两个点的坐标,输出两个点之间的距离。(2)重载运算符为“-”。
创建一个CTriangle类,需要用到第二题中创建的类,即用3点来代表一个三角形,输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。可以根据需要加入自己的成员变量或成员函数。
要求:(1)输入三个点的坐标,输出周长并给出是否是直角三角形的信息。(2)注意程序的健壮性。
#include
#include
using namespace std;
class CPoint {
public:
CPoint(){}
CPoint(double a,double b):x(a),y(b){}
double operator-(CPoint A) {
return sqrt(pow(x - A.x, 2) + pow(y - A.y, 2));
}
private:
double x = 0;
double y = 0;
};
class CTriangle {
public:
CTriangle(double x1,double y1, double x2, double y2, double x3, double y3):a(x1,y1),b(x2,y2),c(x3,y3){}
double C() {
return (a - b) + (a - c) + (b - c);
}
bool Z() {
double x = 0.0, y = 0.0, z = 0.0;
double u = 0.0;
x = a - b;
y = a - c;
z = b - c;
if (x >= y && x >= z) {
return pow(y, 2) + pow(z, 2) == pow(x, 2);
}
else if (y >= x && y >= z) {
return pow(x, 2) + pow(z, 2) == pow(y, 2);
}
else if (z >= x && z >= y) {
return pow(x, 2) + pow(y, 2) == pow(z, 2);
}
}
private:
CPoint a;
CPoint b;
CPoint c;
};
int main()
{
double x1 = 0;
double y1 = 0;
double x2 = 0;
double y2 = 0;
double x3 = 0;
double y3 = 0;
x1 = 1; y1 = 1;
x2 = 4; y2 = 1;
x3 = 1; y3 = 5;
CTriangle T(x1, y1, x2, y2, x3, y3);
cout << T.C() << '\n';
cout << T.Z() << '\n';
return 0;
}