请问如何编程
输入一个数,如果是1295-1344,1381-1420,1570-1574则输出“张三”,否则输出“无此人”;如果是1345-1380,1575-1590,6001-6030,6114-6135则输出“李四”,否则输出“有此人”,请问如何编程?
……否则输出“无此人”……否则输出“有此人”
------ 既非张三又非李四时,到底输出什么?
#include <string> #include <vector> #include <utility> #include <algorithm> const std::string& foo( unsigned id ) { static const std::string nobody = "无此人"; static const std::vector<std::pair<unsigned,std::string>> buf = { {1295,"无此人"} , {1345,"张三" } , {1381,"李四"} , {1421,"张三" } , {1570,"无此人"} , {1575,"张三" } , {1591,"李四"} , {6001,"无此人"} , {6031,"李四"} , {6114,"无此人"} , {6136,"李四"} }; auto itor = std::upper_bound( std::begin(buf), std::end(buf), id , [](unsigned id,const auto& value){return id<value.first;} ); return itor!=std::end(buf) ? itor->second : nobody; } #include <cassert> int main(int argc, char **argv) { assert( foo(1294)=="无此人" ); assert( foo(1295)=="张三" ); assert( foo(1344)=="张三" ); assert( foo(1345)=="李四" ); assert( foo(1380)=="李四" ); assert( foo(1381)=="张三" ); assert( foo(1420)=="张三" ); assert( foo(1421)=="无此人" ); assert( foo(1569)=="无此人" ); assert( foo(1570)=="张三" ); assert( foo(1574)=="张三" ); assert( foo(1575)=="李四" ); assert( foo(1590)=="李四" ); assert( foo(1591)=="无此人" ); assert( foo(6000)=="无此人" ); assert( foo(6001)=="李四" ); assert( foo(6030)=="李四" ); assert( foo(6031)=="无此人" ); assert( foo(6113)=="无此人" ); assert( foo(6114)=="李四" ); assert( foo(6135)=="李四" ); assert( foo(6136)=="无此人" ); }