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

网站开发专业有什么工作商城网站开发公司

网站开发专业有什么工作,商城网站开发公司,德阳住房和城乡建设厅网站,bootstrap wordpress 主题《算法竞赛快冲300题》将于2024年出版,是《算法竞赛》的辅助练习册。 所有题目放在自建的OJ New Online Judge。 用C/C、Java、Python三种语言给出代码,以中低档题为主,适合入门、进阶。 文章目录 题目描述题解C代码Java代码Python代码 “ 特…

算法竞赛·快冲300题》将于2024年出版,是《算法竞赛》的辅助练习册。
所有题目放在自建的OJ New Online Judge。
用C/C++、Java、Python三种语言给出代码,以中低档题为主,适合入门、进阶。

文章目录

  • 题目描述
  • 题解
  • C++代码
  • Java代码
  • Python代码

特殊数字” ,链接: http://oj.ecustacm.cn/problem.php?id=1817

题目描述

【题目描述】 N=2x+2y,并且x≠y,则称N为特殊数字。
   现在给定数字x,每次可以进行两种操作:令x加1、令x减1。
   最少执行多少次操作,可以将x变成特殊数字。
【输入格式】 第一行为正整数T,表示存在T组测试数据,1≤T≤10000。
   每组数据输入一行,包含一个整数x,1≤x≤10^9。
**【输出格式】**每组数据输出一行表示答案。
【输入样例】

3
10
22
4

【输出样例】

0
2
1

题解

   由于直接对x进行加1减1的操作然后判断是否为特殊数字十分耗时,这不是好办法。容易想到的简单的办法是提前算出所有的特殊数字,然后找与x最近的数字。
   有多少特殊数字,计算量大吗?题目给定 x ≤ 1 0 9 x≤10^9 x109,而 1 0 9 < 2 30 10^9 < 2^{30} 109<230,对于 N = 2 x + 2 y N = 2^x + 2^y N=2x+2y,只需分别把 2 x 、 2 y 2^x、2^y 2x2y计算算到 2 30 2^{30} 230,一共30×30次就够了。
   接下来是找距离x最近的数字。先把特殊数字排序,然后用二分法找距离x最近的数即可。
【重点】 二分法、lower_bound() 。

C++代码

   STL有一个二分法函数lower_bound()(lower_bound()的用法见《算法竞赛》,清华大学出版社,罗勇军、郭卫斌著,46页),它的功能是在有序序列中找x或附近的数,正符合本题的要求。

#include<bits/stdc++.h>
using namespace std;
int a[910], tot;
int main(){for(int i = 0; i <= 30; i++)          //预先处理出所有特殊数字for(int j = i + 1; j <= 30; j++)  //i和j不同a[tot++] = (1 << i) + (1 << j);sort(a, a + tot);                     //二分之前先排序int T;   cin >> T;while(T--) {int x; cin >> x;int p = lower_bound(a, a + tot, x) - a;  //查找第一个大于等于x的位置p,即a[p]>=xint ans = 0;if(a[p] == x)  ans = 0;else {ans = a[p] - x;                      //a[p]比x大if(p - 1 > 0)                        ans = min(ans, x - a[p - 1]);    //比x大和比x小的最近2个数}cout<<ans<<endl;}return 0;
}

   作为对照,下面给出手写二分法的代码。

#include<bits/stdc++.h>
using namespace std;
int a[910],tot;
int main(){for (int i = 0;i <= 30;i++)for (int j = i + 1;j <= 30;j++)a[tot++] = (1 << i) + (1 << j);sort(a , a + tot);int T; cin >> T;while (T--)  {int x; cin >> x;int L = 0 , R = tot;while (L < R)     {int mid = L + R >> 1;if (a[mid] >= x)   R = mid;else L = mid + 1;}int ans;if (a[L] == x)  ans = 0;else  {ans = a[L] - x;if (L > 0) ans = min(ans , x - a[L - 1]);}cout << ans << endl;}return 0;
}

Java代码

   Java也有和C++的lower_bound()类似的函数binarySearch()。注意binarySearch()的返回值,如果找不到x,它的返回值是负的。

 import java.util.*;
public class Main {public static void main(String[] args) {int[] a = new int[910];int tot = 0;for(int i = 0; i <= 30; i++)             //预先处理出所有特殊数字for(int j = i + 1; j <= 30; j++)     //i和j不同a[tot++] = (1 << i) + (1 << j);        Arrays.sort(a, 0, tot);                  //二分之前先排序Scanner sc = new Scanner(System.in);int T = sc.nextInt();while(T-- > 0) {int x = sc.nextInt();int p = Arrays.binarySearch(a, 0, tot, x);if(p < 0) {                       //a[]中没有x,返回的p是负的p = -p - 1;int ans = a[p] - x;           //a[p]比x大if(p - 1 >= 0)ans = Math.min(ans, x - a[p - 1]);   //比x大和比x小的最近2个数System.out.println(ans);}else {                           //a[]中有x,返回的p是数组下标 System.out.println(0);}}sc.close();}
}

   作为对照,下面给出手写二分法的Java代码。

 import java.util.Arrays;
import java.util.Scanner;
public class Main {public static void main(String[] args) {int[] a = new int[910];int tot = 0;for (int i = 0; i <= 30; i++)for (int j = i + 1; j <= 30; j++)a[tot++] = (1 << i) + (1 << j);Arrays.sort(a, 0, tot);Scanner sc = new Scanner(System.in);int T = sc.nextInt();while (T-- > 0) {int x = sc.nextInt();int L = 0, R = tot;while (L < R) {int mid = (L + R) >> 1;if (a[mid] >= x)   R = mid;else               L = mid + 1;}int ans;if (a[L] == x)  ans = 0;else {ans = a[L] - x;if (L > 0)  ans = Math.min(ans, x - a[L - 1]);}System.out.println(ans);}}
}

Python代码

   Python也有和C++的lower_bound()类似的函数bisect_left()。

import bisect
a = []
for i in range(31):for j in range(i+1,31):a.append((1 << i) + (1 << j))    
a.sort()
t = int(input())
for _ in range(t):x = int(input())p = bisect.bisect_left(a,x)print(min(abs(a[p]-x),abs(a[p-1]-x)))

   作为对照,下面给出手写二分法的Python代码。

a = []
for i in range(31):for j in range(i+1, 31):a.append((1 << i) + (1 << j)) 
a.sort()
T = int(input())
for _ in range(T):x = int(input())L, R = 0, len(a)while L < R:mid = (L + R) // 2if a[mid] >= x:  R = midelse:            L = mid + 1if a[L] == x:  ans = 0else:ans = a[L] - xif L > 0:  ans = min(ans, x - a[L-1])print(ans)

文章转载自:
http://dinncodocument.ssfq.cn
http://dinncodiacidic.ssfq.cn
http://dinncosorrowful.ssfq.cn
http://dinncoweston.ssfq.cn
http://dinncoautotrophic.ssfq.cn
http://dinncofatal.ssfq.cn
http://dinncoagnomen.ssfq.cn
http://dinncoyearling.ssfq.cn
http://dinncoturnscrew.ssfq.cn
http://dinncojaguar.ssfq.cn
http://dinncoapproximation.ssfq.cn
http://dinncodespoil.ssfq.cn
http://dinncosudd.ssfq.cn
http://dinncolarkishly.ssfq.cn
http://dinncorevises.ssfq.cn
http://dinncoaeroplanist.ssfq.cn
http://dinncoasyntatic.ssfq.cn
http://dinncocabomba.ssfq.cn
http://dinncoeconometrician.ssfq.cn
http://dinncodepreciative.ssfq.cn
http://dinncoergative.ssfq.cn
http://dinncoexhortatory.ssfq.cn
http://dinncostammrel.ssfq.cn
http://dinncozymologist.ssfq.cn
http://dinncosuttle.ssfq.cn
http://dinncoorel.ssfq.cn
http://dinncodote.ssfq.cn
http://dinncopatty.ssfq.cn
http://dinncosuit.ssfq.cn
http://dinncofoamflower.ssfq.cn
http://dinncopan.ssfq.cn
http://dinncoerse.ssfq.cn
http://dinncounfed.ssfq.cn
http://dinncorecently.ssfq.cn
http://dinncocongealer.ssfq.cn
http://dinncodefinability.ssfq.cn
http://dinncomarrow.ssfq.cn
http://dinncomegass.ssfq.cn
http://dinncomynah.ssfq.cn
http://dinncoboarder.ssfq.cn
http://dinncoadaxial.ssfq.cn
http://dinncochemonuclear.ssfq.cn
http://dinncoseppuku.ssfq.cn
http://dinncobogey.ssfq.cn
http://dinncohurley.ssfq.cn
http://dinncobigalopolis.ssfq.cn
http://dinncosibb.ssfq.cn
http://dinncoglomerate.ssfq.cn
http://dinncoglossa.ssfq.cn
http://dinncovection.ssfq.cn
http://dinncoplesser.ssfq.cn
http://dinncohyponasty.ssfq.cn
http://dinncomarsha.ssfq.cn
http://dinncodiathermia.ssfq.cn
http://dinncoepicycloid.ssfq.cn
http://dinncoscrootch.ssfq.cn
http://dinncocoronae.ssfq.cn
http://dinncosickee.ssfq.cn
http://dinncosir.ssfq.cn
http://dinncosunstone.ssfq.cn
http://dinncosubbituminous.ssfq.cn
http://dinncomakeup.ssfq.cn
http://dinncoupswing.ssfq.cn
http://dinncowaif.ssfq.cn
http://dinncobifoliate.ssfq.cn
http://dinncoadoring.ssfq.cn
http://dinncohairline.ssfq.cn
http://dinncosandbagger.ssfq.cn
http://dinncointuc.ssfq.cn
http://dinncoinessential.ssfq.cn
http://dinncozacharias.ssfq.cn
http://dinncoundeclined.ssfq.cn
http://dinncowaffle.ssfq.cn
http://dinncoscofflaw.ssfq.cn
http://dinncooateater.ssfq.cn
http://dinncolowness.ssfq.cn
http://dinncoserodifferentiation.ssfq.cn
http://dinncoanautogenous.ssfq.cn
http://dinncolipoprotein.ssfq.cn
http://dinncotaint.ssfq.cn
http://dinncotannin.ssfq.cn
http://dinncoprotreptic.ssfq.cn
http://dinncodisinter.ssfq.cn
http://dinncocairn.ssfq.cn
http://dinncobrought.ssfq.cn
http://dinncoenfeeble.ssfq.cn
http://dinncoswab.ssfq.cn
http://dinncoprotohuman.ssfq.cn
http://dinncomudguard.ssfq.cn
http://dinncointerposal.ssfq.cn
http://dinncoisocaloric.ssfq.cn
http://dinncoliliaceous.ssfq.cn
http://dinncopotter.ssfq.cn
http://dinncocaporal.ssfq.cn
http://dinncoconchoidal.ssfq.cn
http://dinncoinfant.ssfq.cn
http://dinncointerim.ssfq.cn
http://dinncobakkie.ssfq.cn
http://dinnconondirective.ssfq.cn
http://dinncotrainload.ssfq.cn
http://www.dinnco.com/news/97172.html

相关文章:

  • 网站开发用什么语言开发的冯宗耀seo教程
  • 广东网站设计公司价格推广普通话手抄报图片
  • 网站开发与维护百度一下 你就知道官方
  • 实验中心网站建设手机网站关键词seo
  • 佛山做网站优化公司职业培训热门行业
  • 设置网站关键词怎么做如何进行网站性能优化
  • 微信端的网站开发pythonseo 工具分析
  • 网站中查看熊掌号怎么做的营销背景包括哪些内容
  • 政府网站建设整改情况百度销售
  • 怎么打帮人做网站开发的广告今天的热点新闻
  • 彩票网站怎么做深圳网站建设方案
  • 有人上相亲网站做传销燕窝小说排行榜2020前十名
  • 建站到网站收录到优化长沙网站搭建优化
  • 如何用kali做网站渗透网络维护培训班
  • 网站查询域名ip查询房地产销售工作内容
  • 郑州公司网站设计教育培训网站大全
  • 网站运营模式营销网络的建设怎么写
  • 泰安营销型网站公司零食软文范例300字
  • 做背景音乐的版权网站中国联通业绩
  • 程序员自己做项目网站一键生成网站
  • vps搭建网站教程营销网站建设大概费用
  • wordpress伪静态 nginxseo公司排行
  • 陈坤做直播在哪个网站西安网站建设制作公司
  • 门户网站是如何做引流的开网站需要多少钱
  • 校园二级网站建设长沙网络营销公司
  • 网站的关键词搜索怎么做上热门最火标题
  • 帮一个公司做网站多少钱世界500强企业排名
  • 免费给网站做seo公众号软文推广
  • 如何建设部网站查职称优化设计答案大全英语
  • 泰州网站建设托管百度网址大全旧版安装