笔试面试相关记录(9)

(1)最多三次购买股票的机会,每次买入之前需要保证手上没有其他股票,买卖可以在同一天完成,求最大的收益;

输入:

3,8,5,1,2,7,3,8表示每天股票的价格

#include 
#include 
#include 
#include 
using namespace std;

int main() {
    string str;
    while (getline(cin, str)) {
        // 3,8,5,1,2,7,3,8
        int len = str.size();
        vector prices;
        int t = 0;
        for (int i = 0; i < len; i++) {
            if (isdigit(str[i])) {
                t = t*10 + (str[i]-'0');
            } else if (str[i] == ',') {
                prices.push_back(t);
                t = 0;
            }
        }
        prices.push_back(t);
        int n = prices.size();
        // dp表示每天的状态
        // 0 ===>  没有任何买卖股票的记录
        // 1 ===>  第一次买入股票
        // 2 ===>  第一次卖出购票
        // 3 ===>  第二次买入股票
        // 4 ===>  第二次卖出股票
        // 5 ===>  第三次买入股票
        // 6 ===>  第三次卖出股票
        vector> dp(n, vector(7));
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        dp[0][2] = 0;
        dp[0][3] = -prices[0];
        dp[0][4] = 0;
        dp[0][5] = -prices[0];
        dp[0][6] = 0;
        for (int i = 1; i < n; i++) {
            // 当天没有任何买卖股票,这里好像应该是0?
            for (int j = 0; j < 7; j++)
                dp[i][0] = max(dp[i][0], dp[i-1][j]);
            // 该天第一次买入
            dp[i][1] = -prices[i];
            // 该天第一次卖出,从前往后找第一次买入时+当前价格
            for (int j = 0; j <= i; j++)
                dp[i][2] = max(dp[i][2], dp[j][1]+prices[i]);
            // 该天第二次买入,从前往后找第一次卖出时-当前价格
            for (int j = 0; j <= i; j++)
                dp[i][3] = max(dp[i][3], dp[j][2]-prices[i]);
            // 该天第二次卖出,从前往后找第二次买入时+当前价格
            for (int j = 0; j <= i; j++) 
                dp[i][4] = max(dp[i][4], dp[j][3]+prices[i]);
            // 该天第三次买入,从前往后找第二次卖出时+当前价格
            for (int j = 0; j <= i; j++)
                dp[i][5] = max(dp[i][5], dp[j][4]-prices[i]);
            // 该天第三次卖出,从前往后找第三次买入时+当前价格
            for (int j = 0; j <= i; j++)
                dp[i][6] = max(dp[i][6], dp[j][5]+prices[i]);           
        }
        int max_sum = 0;
        // 找到完成三次买卖中的最大值
        for (int i = 0; i < n; i++) {
            max_sum = max(max_sum, dp[i][6]);
        }
        cout << max_sum << endl;
    }
}

(2)输出自然数n以内的所有质数的和的平方根,保留四位小数。

bool check(int n) {
    for (int i = 2; i <= int(sqrt(n)); i++) {
        if (n%i == 0) { 
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    while (cin >> n) {
        int sum = 0;
        for (int i = 2; i < n; i++) {
            if (check(i)) {
                sum += i;
            }
        }
        printf("%.4f", sqrt(sum));
    }
}

(3)找出id='1001'的客户在11月份和12月份的总的消费数目

在线数据库测试:DB Fiddle - SQL Database Playground (db-fiddle.com)

drop table if exists AccoInfo;
CREATE TABLE `AccoInfo` (
  `id` varchar(10)  NOT NULL,
  `name` varchar(10)  NOT NULL,
  `balance` int NOT NULL,
  PRIMARY KEY (`id`)
);

insert into AccoInfo(id, name, balance) values 
('1001','hundsun', 100),
('1002','hundsun', 100);

drop table if exists AccoCurrent_202211;
CREATE TABLE `AccoCurrent_202211` (
  `id` varchar(10) NOT NULL,
  `amount` int NOT NULL,
  `time` varchar(11) NOT NULL
);

insert into AccoCurrent_202211 values
('1001', 100, '20221101'), 
('1001', 200, '20221102'), 
('1002', 100, '20221101'), 
('1002', 200, '20221102'); 

drop table if exists AccoCurrent_202212;
CREATE TABLE `AccoCurrent_202212` (
  `id` varchar(10) NOT NULL,
  `amount` int NOT NULL,
  `time` varchar(11) NOT NULL
);

insert into AccoCurrent_202212 values
('1001', 100, '20221201'),
('1001', 200, '20221202'),
('1002', 100, '20221201'),
('1002', 200, '20221202');
SELECT id, SUM(amount) AS 'sum(amount)'
FROM (
    SELECT id, amount FROM AccoCurrent_202211
    UNION ALL
    SELECT id, amount FROM AccoCurrent_202212
) combined
WHERE id = '1001';

(4)从下面表中找出没有用户欠款记录的客户名和用户名称

CREATE TABLE `cust_info` (
  `NO` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '客户号',
  `NAME` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '姓名',
  `ID` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '身份证号',
  `BIRTH` int DEFAULT NULL COMMENT '出生年月',
  `COUNTRY` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '国籍'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('001', '张三', '330111XXXXX1', 19900101, '中国');
INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('002', '李四', '330111XXXXX2', 20000201, '中国');
INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('003', '王五', '330111XXXXX3', 20000301, '中国');
INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('004', '赵六', '330111XXXXX4', 19930401, '美国');

CREATE TABLE `cust_assets` (
  `NO` varchar(100) DEFAULT NULL COMMENT '客户号',
  `DEPOSIT` decimal(10,2) DEFAULT NULL COMMENT '存款金额',
  `CREDIT` decimal(10,2) DEFAULT NULL COMMENT '信用卡金额'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO cust_assets (`NO`, DEPOSIT, CREDIT) VALUES('001', 10001.0, -100.0);
INSERT INTO cust_assets (`NO`, DEPOSIT, CREDIT) VALUES('002', 1000.0, -2000.0);
INSERT INTO cust_assets (`NO`, DEPOSIT, CREDIT) VALUES('003', 0.0, -5000.0);
SELECT ci.NO, ci.NAME
FROM cust_info ci
LEFT JOIN cust_assets ca ON ca.NO = ci.NO
WHERE ca.CREDIT is NULL;

(5)从下面表中选出工人平均成绩大于85的工人的名称和平均成绩

DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee (
Eno int NOT NULL, 
Ename varchar(255) DEFAULT NULL,
Esex varchar(255) NOT NULL, 
Eage int DEFAULT NULL,
Edept varchar(255) DEFAULT NULL, 
PRIMARY KEY (Eno)
);
INSERT INTO Employee VALUES ('1', '张三', '男', '32', '部门A');
INSERT INTO Employee VALUES ('2', '李四', '男', '26', '部门B');
INSERT INTO Employee VALUES ('3', '王五', '男', '28', '部门A');
INSERT INTO Employee VALUES ('4', '赵七', '男', '29', '部门A');
INSERT INTO Employee VALUES ('5', '周六', '女', '30', '部门C');
DROP TABLE IF EXISTS Course;
CREATE TABLE Course (
CNo varchar(255) NOT NULL,
Cname varchar(255) DEFAULT NULL, 
Ccredit int DEFAULT NULL,
PRIMARY KEY (CNo)
);
INSERT INTO Course VALUES ('001', '数据库', '10');
INSERT INTO Course VALUES ('002', '计算机基础', '10');
INSERT INTO Course VALUES ('003', '金融基础', '20');
INSERT INTO Course VALUES ('004', '投资基础', '10');
DROP TABLE IF EXISTS SC;
CREATE TABLE SC (
Eno int NOT NULL,
CNo varchar(255) DEFAULT NULL,
Grade int DEFAULT NULL,  PRIMARY KEY (Eno)
) ;
INSERT INTO SC VALUES ('1', '001', '60');
INSERT INTO SC VALUES ('2', '001', '80');
INSERT INTO SC VALUES ('3', '001', '86');
INSERT INTO SC VALUES ('4', '001', '88');
INSERT INTO SC VALUES ('5', '001', '86');
SELECT E.Ename, AVG(SC.Grade) AS AverageGrade
FROM Employee E
JOIN SC ON E.Eno = SC.Eno
GROUP BY E.Eno, E.Ename
HAVING AVG(SC.Grade) > 85;

(6)事务隔离级别

笔试面试相关记录(9)_第1张图片

你可能感兴趣的:(面试,算法,c++)