编程题(北理工计算机考研复试上机)2

在这里插入图片描述

#include "pch.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
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;
		}

	}
};

日期加100天后的日期

#include <iostream>
#include <stdio.h>
#include <string>
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 <iostream>
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 <iostream>
#include <cmath>
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;
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值