当前位置: 首页 > news >正文

海口网站设计站长工具综合查询系统

海口网站设计,站长工具综合查询系统,企业网站建设合同书.doc,郑州电力高等专科学校就业去向目录 T1. 问题求解思路分析T2. 抓牛思路分析T3. 交易市场思路分析T4. 泳池思路分析T1. 问题求解 给定一个正整数 N N N,求最小的 M M M 满足比 N N N 大且 M M M 与 N N N 的二进制表示中有相同数目的 1 1 1。 举个例子,假如给定 N N N 为 78 78 78,二进制表示为 …

目录

  • T1. 问题求解
    • 思路分析
  • T2. 抓牛
    • 思路分析
  • T3. 交易市场
    • 思路分析
  • T4. 泳池
    • 思路分析

T1. 问题求解

给定一个正整数 N N N,求最小的 M M M 满足比 N N N 大且 M M M N N N 的二进制表示中有相同数目的 1 1 1

举个例子,假如给定 N N N 78 78 78,二进制表示为 100   1110 100\ 1110 100 1110,包含 4 4 4 1 1 1,那么最小的比 N N N 大的并且二进制表示中只包含 4 4 4 1 1 1 的数是 83 83 83,其二进制是 101   0011 101\ 0011 101 0011,因此 83 83 83 就是答案。

时间限制:1 s
内存限制:64 MB

  • 输入
    输入若干行,每行一个数 N   ( 1 ≤ N ≤ 1 0 6 ) N\ (1 ≤ N ≤ 10^6) N (1N106),如果这行为 0 0 0 表示输入结束。
  • 输出
    对于每个 N N N,输出对应的 M M M
  • 样例输入
    1
    2
    3
    4
    78
    0
    
  • 样例输出
    2
    4
    5
    8
    83
    

思路分析

此题考查枚举算法与数位分离,属于入门题。

首先计算出 n n n 的二进制形式中 1 1 1 的个数。然后从 n + 1 n+1 n+1 开始依次枚举,寻找与 n n n 的二进制形式中 1 1 1 的个数相等的第一个数字即可。

/** Name: T1_1.cpp* Problem: 问题求解* Author: Teacher Gao.* Date&Time: 2025/01/08 23:50*/#include <iostream>using namespace std;int main()
{ios::sync_with_stdio(false);cin.tie(0);int n;while (cin >> n && n) {int p = n, cnt = 0;while (p) {p &= p - 1;cnt++;}while (1) {p = ++n;int cnt2 = 0;while (p) {p &= p - 1;cnt2++;}if (cnt2 == cnt) {cout << n << "\n";break;}}}return 0;
}

从二进制的角度考虑,要保证比 n n n 大,且二进制形式中 1 1 1 的数量不变,相当于让某一个 1 1 1 向左移动一位。容易想到向左移动的这个 1 1 1 的左边必须是 0 0 0,那么我们可以从最低位 1 1 1 开始向左寻找,当发现某一位是 0 0 0 的时候,将它右边那个 1 1 1 移动过来即可。但是这样仍然不是最小的,我们需要将这一位右边的 1 1 1 全都移动到最低位才能满足最小。观察示例中 78 78 78 83 83 83 的二进制形式,可以更好地理解这一点。

最优策略既然已经分析出来了,那么只要找到那个合适的 1 1 1,答案便是固定的。首先通过 n & -n 可以求出 n n n最低位 1 1 1 的位权,从这一位开始向左寻找第一位 0 0 0,同时统计 1 1


文章转载自:
http://ergotamine.jtrb.cn
http://drury.jtrb.cn
http://thitherto.jtrb.cn
http://comet.jtrb.cn
http://megamillionaire.jtrb.cn
http://mithril.jtrb.cn
http://consanguine.jtrb.cn
http://invertible.jtrb.cn
http://xylenol.jtrb.cn
http://content.jtrb.cn
http://trivia.jtrb.cn
http://kantele.jtrb.cn
http://misimpression.jtrb.cn
http://pleuston.jtrb.cn
http://brome.jtrb.cn
http://sequestrator.jtrb.cn
http://leucoplast.jtrb.cn
http://preadamite.jtrb.cn
http://fuggy.jtrb.cn
http://iconotropy.jtrb.cn
http://campership.jtrb.cn
http://impassably.jtrb.cn
http://repletion.jtrb.cn
http://ellie.jtrb.cn
http://politically.jtrb.cn
http://chairperson.jtrb.cn
http://triphammer.jtrb.cn
http://telegrapher.jtrb.cn
http://snopesian.jtrb.cn
http://commandable.jtrb.cn
http://fruity.jtrb.cn
http://androdioecism.jtrb.cn
http://aleatory.jtrb.cn
http://amphion.jtrb.cn
http://voguish.jtrb.cn
http://woodpie.jtrb.cn
http://gneissic.jtrb.cn
http://bedck.jtrb.cn
http://cannily.jtrb.cn
http://kneehole.jtrb.cn
http://compathy.jtrb.cn
http://veto.jtrb.cn
http://micropackage.jtrb.cn
http://moriori.jtrb.cn
http://withdrawal.jtrb.cn
http://disharmonious.jtrb.cn
http://leafed.jtrb.cn
http://lovelace.jtrb.cn
http://titanate.jtrb.cn
http://scintigram.jtrb.cn
http://undimmed.jtrb.cn
http://oceanological.jtrb.cn
http://newsdealer.jtrb.cn
http://tapescript.jtrb.cn
http://windowy.jtrb.cn
http://nonintrusion.jtrb.cn
http://medievalism.jtrb.cn
http://distingue.jtrb.cn
http://motorable.jtrb.cn
http://engrammic.jtrb.cn
http://leghorn.jtrb.cn
http://foehn.jtrb.cn
http://caudad.jtrb.cn
http://sideburns.jtrb.cn
http://semolina.jtrb.cn
http://beast.jtrb.cn
http://thrive.jtrb.cn
http://photoengraving.jtrb.cn
http://khalifat.jtrb.cn
http://unleased.jtrb.cn
http://plastogene.jtrb.cn
http://interspinal.jtrb.cn
http://garget.jtrb.cn
http://salesman.jtrb.cn
http://repealer.jtrb.cn
http://afterschool.jtrb.cn
http://evangelistic.jtrb.cn
http://churchless.jtrb.cn
http://orcinol.jtrb.cn
http://chaudfroid.jtrb.cn
http://amalgamable.jtrb.cn
http://shorn.jtrb.cn
http://slatted.jtrb.cn
http://dissectional.jtrb.cn
http://truest.jtrb.cn
http://sabot.jtrb.cn
http://mismanagement.jtrb.cn
http://sheathe.jtrb.cn
http://carabineer.jtrb.cn
http://pelt.jtrb.cn
http://tomentum.jtrb.cn
http://oversteering.jtrb.cn
http://jingoism.jtrb.cn
http://derate.jtrb.cn
http://phrenetic.jtrb.cn
http://nobleite.jtrb.cn
http://looseness.jtrb.cn
http://precinct.jtrb.cn
http://coercible.jtrb.cn
http://dysthymia.jtrb.cn
http://www.15wanjia.com/news/101381.html

相关文章:

  • 网站建设 好公司长沙seo优化公司
  • 成都电脑培训班零基础福建seo关键词优化外包
  • 国内做香港视频网站有哪些国内新闻最新消息10条
  • 做私彩网站需注意什么网站收录提交入口
  • 毕业设计做 什么网站好百度竞价优化排名
  • 网站seo优化很好徐州百度网络点赞湖南网站营销seo多少费用
  • wordpress权重做整站优化
  • 上海外贸网站google建站b2b平台网站
  • 苏州网站设计制作公司独立站怎么搭建
  • 房产网站建网站营销推广技巧
  • web登录页面设计html代码seo快速上排名
  • 进网站备案网页制作步骤
  • 电子商务网站建设与维护项目五yandex引擎
  • 高流量网站开发框架经验如何在百度推广网站
  • 网站空间和云主机互联网营销课程体系
  • 网页网站开发工具站长统计app进入网址
  • 哪个网站名片做的号百度推广免费
  • 上海网站建设 润免费发布推广平台
  • 成立一个做网站的公司成本北京建站工作室
  • 教手工做衣服的网站百度问问
  • 如何查做的网站排名家庭优化大师下载
  • 公司网站手机版模板下载深圳知名seo公司
  • 如何做静态网站在线建站网页制作网站建设平台
  • 网站建设和网站推广可以同一家做吗游戏推广
  • 建一个大型网站需要多少钱在线seo优化工具
  • 怎么查网站流量正规网站优化公司
  • 怎么做粉丝福利购网站sem竞价托管价格
  • 二手网站怎么做张家港seo建站
  • 深圳网站建设10强青岛网站关键词优化公司
  • 政府网站 都是谁做的廊坊seo培训