题目85-五键键盘(c++版)
#include <string> #include <vector> #include <iostream> #include <memory> #include <cstring> #include <sstream> std::string title = "有一个特殊的五键键盘\n" "上面有A、Ctrl-C、Ctrl-X、Ctrl-V、Ctrl-A\n" "A键在屏幕上输出一个字母A\n" "Ctrl-C将当前所选的字母复制到剪贴板\n" "Ctrl-X将当前选择的字母复制到剪贴板并清空所选择的字母\n" "Ctrl-V将当前剪贴板的字母输出到屏幕\n" "Ctrl-A选择当前屏幕中所有字母\n" "注意:\n" "1. 剪贴板初始为空\n" "2. 新的内容复制到剪贴板会覆盖原有内容\n" "3. 当屏幕中没有字母时,Ctrl-A无效\n" "4. 当没有选择字母时Ctrl-C、Ctrl-X无效\n" "5. 当有字母被选择时A和Ctrl-V这两个输出功能的键,\n" "会先清空所选的字母再进行输出\n" "\n" " 给定一系列键盘输入,\n" " 输出最终屏幕上字母的数量\n" "\n" " 输入描述:\n" "输入为一行\n" "为简化解析用数字12345分别代替A、Ctrl-C、Ctrl-X、Ctrl-V、Ctrl-A的输入\n" "数字用空格分割\n" "\n" " 输出描述:\n" "输出一个数字为屏幕上字母的总数量\n" "\n" " 示例一:\n" "输入:\n" "1 1 1\n" "输出:\n" "3\n" "\n" " 示例二:\n" "输入:\n" "1 1 5 1 5 2 4 4\n" "输出:\n" "2"; void Help() { std::cout << title << std::endl; ; } int main(int argc, char const *argv[]) { if (argc > 1 && strncmp(argv[1], "-h", 2) == 0) { Help(); } // get the numbers int n; std::string str; std::vector<int> numbers; std::getline(std::cin, str); std::stringstream sstr(str); while (sstr >> n) { numbers.push_back(n); } // calc screen words int current_words = 0, last_words = 0; // 当前多少个字,上一次多少个字 int current_control = 0, last_control = 0; //当前输入的控制,上一次输入的控制 int have_words = 0; //剪贴板里有多少个字 bool select_all=false; for (auto &n : numbers) { // std::cout << n << std::endl; switch (n) { case 1: { if (last_control == 5) { current_words = 1; } else { current_words++; } last_control = 1; } break; case 2: { if (last_control == 5) { have_words = current_words; } last_control = 2; } break; case 3: { if (last_control == 5) { have_words = current_words; current_words = 0; } last_control = 3; } break; case 4: { if (last_control == 5&nbs***bsp;select_all) { current_words = have_words; select_all=false; } else { current_words += have_words; } last_control = 4; } break; case 5: { last_control = 5; select_all=true; } break; default: break; } } std::cout << current_words << std::endl; return 0; }
#华为机考#