• 笔试面试相关记录(9)


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

    输入:

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

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. int main() {
    7. string str;
    8. while (getline(cin, str)) {
    9. // 3,8,5,1,2,7,3,8
    10. int len = str.size();
    11. vector<int> prices;
    12. int t = 0;
    13. for (int i = 0; i < len; i++) {
    14. if (isdigit(str[i])) {
    15. t = t*10 + (str[i]-'0');
    16. } else if (str[i] == ',') {
    17. prices.push_back(t);
    18. t = 0;
    19. }
    20. }
    21. prices.push_back(t);
    22. int n = prices.size();
    23. // dp表示每天的状态
    24. // 0 ===> 没有任何买卖股票的记录
    25. // 1 ===> 第一次买入股票
    26. // 2 ===> 第一次卖出购票
    27. // 3 ===> 第二次买入股票
    28. // 4 ===> 第二次卖出股票
    29. // 5 ===> 第三次买入股票
    30. // 6 ===> 第三次卖出股票
    31. vectorint>> dp(n, vector<int>(7));
    32. dp[0][0] = 0;
    33. dp[0][1] = -prices[0];
    34. dp[0][2] = 0;
    35. dp[0][3] = -prices[0];
    36. dp[0][4] = 0;
    37. dp[0][5] = -prices[0];
    38. dp[0][6] = 0;
    39. for (int i = 1; i < n; i++) {
    40. // 当天没有任何买卖股票,这里好像应该是0?
    41. for (int j = 0; j < 7; j++)
    42. dp[i][0] = max(dp[i][0], dp[i-1][j]);
    43. // 该天第一次买入
    44. dp[i][1] = -prices[i];
    45. // 该天第一次卖出,从前往后找第一次买入时+当前价格
    46. for (int j = 0; j <= i; j++)
    47. dp[i][2] = max(dp[i][2], dp[j][1]+prices[i]);
    48. // 该天第二次买入,从前往后找第一次卖出时-当前价格
    49. for (int j = 0; j <= i; j++)
    50. dp[i][3] = max(dp[i][3], dp[j][2]-prices[i]);
    51. // 该天第二次卖出,从前往后找第二次买入时+当前价格
    52. for (int j = 0; j <= i; j++)
    53. dp[i][4] = max(dp[i][4], dp[j][3]+prices[i]);
    54. // 该天第三次买入,从前往后找第二次卖出时+当前价格
    55. for (int j = 0; j <= i; j++)
    56. dp[i][5] = max(dp[i][5], dp[j][4]-prices[i]);
    57. // 该天第三次卖出,从前往后找第三次买入时+当前价格
    58. for (int j = 0; j <= i; j++)
    59. dp[i][6] = max(dp[i][6], dp[j][5]+prices[i]);
    60. }
    61. int max_sum = 0;
    62. // 找到完成三次买卖中的最大值
    63. for (int i = 0; i < n; i++) {
    64. max_sum = max(max_sum, dp[i][6]);
    65. }
    66. cout << max_sum << endl;
    67. }
    68. }

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

    1. bool check(int n) {
    2. for (int i = 2; i <= int(sqrt(n)); i++) {
    3. if (n%i == 0) {
    4. return false;
    5. }
    6. }
    7. return true;
    8. }
    9. int main() {
    10. int n;
    11. while (cin >> n) {
    12. int sum = 0;
    13. for (int i = 2; i < n; i++) {
    14. if (check(i)) {
    15. sum += i;
    16. }
    17. }
    18. printf("%.4f", sqrt(sum));
    19. }
    20. }

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

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

    1. drop table if exists AccoInfo;
    2. CREATE TABLE `AccoInfo` (
    3. `id` varchar(10) NOT NULL,
    4. `name` varchar(10) NOT NULL,
    5. `balance` int NOT NULL,
    6. PRIMARY KEY (`id`)
    7. );
    8. insert into AccoInfo(id, name, balance) values
    9. ('1001','hundsun', 100),
    10. ('1002','hundsun', 100);
    11. drop table if exists AccoCurrent_202211;
    12. CREATE TABLE `AccoCurrent_202211` (
    13. `id` varchar(10) NOT NULL,
    14. `amount` int NOT NULL,
    15. `time` varchar(11) NOT NULL
    16. );
    17. insert into AccoCurrent_202211 values
    18. ('1001', 100, '20221101'),
    19. ('1001', 200, '20221102'),
    20. ('1002', 100, '20221101'),
    21. ('1002', 200, '20221102');
    22. drop table if exists AccoCurrent_202212;
    23. CREATE TABLE `AccoCurrent_202212` (
    24. `id` varchar(10) NOT NULL,
    25. `amount` int NOT NULL,
    26. `time` varchar(11) NOT NULL
    27. );
    28. insert into AccoCurrent_202212 values
    29. ('1001', 100, '20221201'),
    30. ('1001', 200, '20221202'),
    31. ('1002', 100, '20221201'),
    32. ('1002', 200, '20221202');
    1. SELECT id, SUM(amount) AS 'sum(amount)'
    2. FROM (
    3. SELECT id, amount FROM AccoCurrent_202211
    4. UNION ALL
    5. SELECT id, amount FROM AccoCurrent_202212
    6. ) combined
    7. WHERE id = '1001';

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

    1. CREATE TABLE `cust_info` (
    2. `NO` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '客户号',
    3. `NAME` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '姓名',
    4. `ID` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '身份证号',
    5. `BIRTH` int DEFAULT NULL COMMENT '出生年月',
    6. `COUNTRY` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '国籍'
    7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    8. INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('001', '张三', '330111XXXXX1', 19900101, '中国');
    9. INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('002', '李四', '330111XXXXX2', 20000201, '中国');
    10. INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('003', '王五', '330111XXXXX3', 20000301, '中国');
    11. INSERT INTO cust_info (`NO`, NAME, ID, BIRTH, COUNTRY) VALUES('004', '赵六', '330111XXXXX4', 19930401, '美国');
    12. CREATE TABLE `cust_assets` (
    13. `NO` varchar(100) DEFAULT NULL COMMENT '客户号',
    14. `DEPOSIT` decimal(10,2) DEFAULT NULL COMMENT '存款金额',
    15. `CREDIT` decimal(10,2) DEFAULT NULL COMMENT '信用卡金额'
    16. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    17. INSERT INTO cust_assets (`NO`, DEPOSIT, CREDIT) VALUES('001', 10001.0, -100.0);
    18. INSERT INTO cust_assets (`NO`, DEPOSIT, CREDIT) VALUES('002', 1000.0, -2000.0);
    19. INSERT INTO cust_assets (`NO`, DEPOSIT, CREDIT) VALUES('003', 0.0, -5000.0);
    1. SELECT ci.NO, ci.NAME
    2. FROM cust_info ci
    3. LEFT JOIN cust_assets ca ON ca.NO = ci.NO
    4. WHERE ca.CREDIT is NULL;

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

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

    (6)事务隔离级别

  • 相关阅读:
    MySQL作业一
    【思维构造】Element Extermination—CF1375C
    MFC|按钮的自绘
    clusterprolifer go kegg msigdbr 富集分析应该使用哪个数据集,GO?KEGG?Hallmark?
    录屏怎么把声音录进去?简单的方法来了!
    L1-020 帅到没朋友(Python3)
    I Chiitoitsu
    【GCC】关于 -finput-charset= 和 -fexec-charset= 选项
    矩阵求导数
    【智能家居】5、主流程设计以及外设框架编写与测试
  • 原文地址:https://blog.csdn.net/wj617906617/article/details/133818148