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

教学网站制作百度网站大全

教学网站制作,百度网站大全,河南省疫情防控指挥部,网站免费空间申请前言: 今天在刷题的时候突然看到一道题,疑似一位故题。仔细一看,欸!这不是就是单身狗的升级版吗?我想那必须再安排一篇,不过由于本篇文章与上一篇单身狗文章所涉及的知识点基本相同,所以还请大…

前言:

今天在刷题的时候突然看到一道题疑似一位故题。仔细一看,欸!这不是就是单身狗的升级版吗?我想那必须再安排一篇,不过由于本篇文章与上一篇单身狗文章所涉及的知识点基本相同,所以还请大家见谅!

我写的开心,大家也看个乐呵!不过还请单身的人不要介意,单纯觉得比较有意思,无意冒犯!毕竟连小编自己都是单身狗。

后续小编也会尽快更新完指针相关知识点!

一、题目:

在一场专为情侣们策划的盛宴中,竟然有两名单身者悄然混入。宴会的主人感到十分不悦,并寻求你的帮助,希望你能运用你敏锐的洞察力,协助她识破并找出这两名不合规矩的单身者。

(无意冒犯,只是提供一个题目的背景)

例如:

有数组的元素是:1,2,3,4,5,6,1,2,3,4,5,7

只有6和7只出现1次,要找出6和7.

二、代码展示(无注释的)

如果有想先自己思考的可以先看一看这个代码,后面也会有解析

#include <stdio.h>
int find(int num) {int index = 0;while ((num & 1) == 0 && index < 32) {num >>= 1;index++;}return index;
}
void single(int arr[], int sz, int* n1, int* n2) {int Re = 0;for (int i = 0; i < sz; i++) {Re ^= arr[i];}int index = find(Re);*n1 = *n2 = 0;for (int i = 0; i < sz; i++) {if (((arr[i] >> index) & 1) == 1) *n1 ^= arr[i];    else *n2 ^= arr[i];}
int main() {int arr[] = {1,2,3,4,5,6,1,2,3,4,5,7};int n1, n2;int sz = sizeof(arr) / sizeof(arr[0]);single(arr, sz, &n1, &n2);printf("两只单身狗分别是:%d 和 %d\n", n1, n2);return 0;
}

三、题解思路:

1.关于算法,我们依然使用的是异或运算,因为异或运算相同为0,所以将数组中所有的数字进行异或操作,最终得到的结果就是那两个只出现一次的数字的异或值。

例如:

举个例子: int arr[ ] = {1,1,2}

初始re = 0;

re = re ^ 1 = 1;此时re = 1;

re = re ^ 1 = 1 ^ 1 = 0;此时re = 0;

re = re ^ 2 = 0 ^ 2 = 2;此时re = 2;所以只出现一次的数字是2

2.找到这个异或结果中为 1 的某一位。根据异或运算不同为1,这一位为 1 说明在这一位上,那两个只出现一次的数字是不同的。

例如:数组{1,1,2}
异或运算:1^1^2 = 2;
0010   2的二进制
 异或的结果是0010,从右向左找1的位置
0000   0的二进制
0010   2的二进制
0000^0010 = 0010 (异或运算相同为0,不同为1)
我们可以发现0在这一位上的数字是0,2在这一位上的数字是2,说明结果为1的这一位,两个只出现一次的数字是不同的。

3.根据这一位将数组中的数字分为两组。一组是这一位为 1 的数字,另一组是这一位为 0 的数字。

再对这两组数字分别进行异或操作,就可以得到那两个只出现一次的数字。

例如,数组为{1,2,3,1,2,4}

第一步,将所有数字异或:1 ^ 2 ^ 3 ^ 1 ^ 2 ^ 4=6 (二进制为0110 )

第二步,找到异或结果中为 1 的一位,从右往左数第二位为 1 

第三步,根据这一位将数字分组:

  • 这一位为 1 的数字:{3,4}
  • 这一位为 0 的数字:{1,2,1,2}

四、函数介绍

1.main函数

int main() 
{int arr[] = {1,2,3,4,5,6,1,2,3,4,5,7};int n1, n2;int sz = sizeof(arr) / sizeof(arr[0]);single(arr, sz, &n1, &n2);printf("两只单身狗分别是:%d 和 %d\n", n1, n2);return 0;
}
  •  数组的输入:int arr[] = {1,2,3,4,5,6,1,2,3,4,5,7};
  • 数组元素个数计算: int sz = sizeof(arr) / sizeof(arr[0]);
  • 调用函数: single(arr, sz, &n1, &n2);

2.single函数

void single(int arr[], int sz, int* n1, int* n2) {int Re = 0;for (int i = 0; i < sz; i++) {Re ^= arr[i];}int index = find(Re);*n1 = *n2 = 0;for (int i = 0; i < sz; i++) {if (((arr[i] >> index) & 1) == 1) {*n1 ^= arr[i];}else {*n2 ^= arr[i];}}
}

single函数作用:找出数组中两个只出现一次的数字

第一个for循环实现数组中所有元素的异或运算

第二个for循环用于根据索引值,将数组分为两组并分别进行异或运算

 int index = find(Re);将索引值传给find函数

3.find函数

int find(int num) {int index = 0;while ((num & 1) == 0 && index < 32) {num >>= 1;index++;}return index;
}

 find函数用于找到一个数的二进制表示中从右往左第一个为 1 的位的索引

五:代码展示(含注释)

#include <stdio.h>
int find(int num)int index = 0;while ((num & 1) == 0 && index < 32)  // 当前二进制位为 0 并且索引小于 32{num >>= 1;//实现二进制中每位检查index++;}return index;// 返回第一个结果为 1 的位的索引
}
void single(int arr[], int sz, int* n1, int* n2) int Re = 0;// 对数组中所有数字进行异或操作,得到两个只出现一次数字的异或结果for (int i = 0; i < sz; i++) {Re ^= arr[i];}int index = find(Re);// 找到上述异或结果中第一个为 1 的位的索引*n1 = *n2 = 0;// 根据找到的索引位,将数组数字分为两组并分别异或for (int i = 0; i < sz; i++) {if (((arr[i] >> index) & 1) == 1) // 判断当前数字在指定索引位是否为 1*n1 ^= arr[i];   else *n2 ^= arr[i];  }
}
int main() 
{int arr[] = { 1, 2, 3, 2, 1, 4 };int n1, n2;int sz = sizeof(arr) / sizeof(arr[0]);//计算数组元素single(arr, sz, &n1, &n2);//函数调用printf("两只单身狗分别是:%d 和 %d\n", n1, n2);return 0;
}

 


文章转载自:
http://leftist.hkpn.cn
http://ethnogeny.hkpn.cn
http://glycine.hkpn.cn
http://airship.hkpn.cn
http://ectogenetic.hkpn.cn
http://kinder.hkpn.cn
http://siphonein.hkpn.cn
http://shippen.hkpn.cn
http://retraining.hkpn.cn
http://xanthoprotein.hkpn.cn
http://ectocommensal.hkpn.cn
http://iroquois.hkpn.cn
http://membrum.hkpn.cn
http://flatboat.hkpn.cn
http://cytotropic.hkpn.cn
http://directorship.hkpn.cn
http://cradle.hkpn.cn
http://skellum.hkpn.cn
http://steeply.hkpn.cn
http://fulgid.hkpn.cn
http://computeracy.hkpn.cn
http://doodle.hkpn.cn
http://vishnu.hkpn.cn
http://clarissa.hkpn.cn
http://straphanger.hkpn.cn
http://abele.hkpn.cn
http://erratic.hkpn.cn
http://exohormone.hkpn.cn
http://scalare.hkpn.cn
http://deckel.hkpn.cn
http://gena.hkpn.cn
http://miskick.hkpn.cn
http://wingbeat.hkpn.cn
http://grand.hkpn.cn
http://filo.hkpn.cn
http://synezesis.hkpn.cn
http://phenate.hkpn.cn
http://gotten.hkpn.cn
http://messieurs.hkpn.cn
http://backswing.hkpn.cn
http://unrevenged.hkpn.cn
http://lizard.hkpn.cn
http://unctuous.hkpn.cn
http://nitrogenase.hkpn.cn
http://goldwasser.hkpn.cn
http://catalogue.hkpn.cn
http://shoyu.hkpn.cn
http://declot.hkpn.cn
http://whangdoodle.hkpn.cn
http://invincibly.hkpn.cn
http://loid.hkpn.cn
http://anomic.hkpn.cn
http://wost.hkpn.cn
http://raptor.hkpn.cn
http://allegory.hkpn.cn
http://centimillionaire.hkpn.cn
http://contraorbital.hkpn.cn
http://favour.hkpn.cn
http://catv.hkpn.cn
http://homme.hkpn.cn
http://yieldingness.hkpn.cn
http://ripeness.hkpn.cn
http://retia.hkpn.cn
http://troublous.hkpn.cn
http://conceptualist.hkpn.cn
http://rifleshot.hkpn.cn
http://midday.hkpn.cn
http://cape.hkpn.cn
http://ultramundane.hkpn.cn
http://goby.hkpn.cn
http://carritch.hkpn.cn
http://sapa.hkpn.cn
http://wear.hkpn.cn
http://hanoi.hkpn.cn
http://ruritania.hkpn.cn
http://guaranty.hkpn.cn
http://lebanese.hkpn.cn
http://griseofulvin.hkpn.cn
http://elation.hkpn.cn
http://curtilage.hkpn.cn
http://gfr.hkpn.cn
http://putative.hkpn.cn
http://snitch.hkpn.cn
http://craped.hkpn.cn
http://incommode.hkpn.cn
http://jazzman.hkpn.cn
http://spellbinder.hkpn.cn
http://woodiness.hkpn.cn
http://shellback.hkpn.cn
http://derris.hkpn.cn
http://toadflax.hkpn.cn
http://sideband.hkpn.cn
http://janiceps.hkpn.cn
http://cangue.hkpn.cn
http://faze.hkpn.cn
http://overtone.hkpn.cn
http://fremdly.hkpn.cn
http://enantiomorphous.hkpn.cn
http://yenangyaung.hkpn.cn
http://westmorland.hkpn.cn
http://www.hrbkazy.com/news/85881.html

相关文章:

  • 大连做网站seo福建百度推广开户
  • 国内外网站建设seo外链平台热狗
  • 沧州商城网站开发设计百度seo优化价格
  • 网站如何做修改密码的相关验证网络营销服务有哪些
  • 智能建站软件哪个好学软件开发学费多少钱
  • 网站建设的基本流程和技术规范软文营销范文100字
  • 东莞大岭山镇疫情最新消息上海好的seo公司
  • 做了网站应该如何推广南京最新消息今天
  • 设计工作室网页设计江苏网站seo设计
  • html5 wap 网站模板查询网站收录
  • 网站建设维护公司资质宁波seo网络推广公司排名
  • 湛江企业建站系统2345浏览器网页版
  • 河南省鹤壁市住房和城乡建设局网站厦门网络关键词排名
  • seo网站排名助手营销方案怎么写模板
  • 重庆网站建设外包哪家好百度网盟推广
  • 深圳有没有可以做家教的网站网站之家查询
  • 数字创意设计包括哪些行业seo技术自学
  • dede企业网站模板华与华营销策划公司
  • 广州网站推广公司厦门网站seo哪家好
  • 上海做宴会的网站站长工具友链查询
  • 企业网站建设可以分为哪些层次如何做好线上营销
  • 石材外贸网站搜索引擎营销sem
  • 微信支付 网站建设百度推广投诉热线
  • 哪些网站做的比较好看的外贸独立站建站
  • wordpress设置html代码深圳谷歌seo推广
  • 聊城做网站的公司案例太原seo排名公司
  • 坪山网站建设哪家便宜乔拓云智能建站
  • 做网站视频博彩如何设计网站步骤
  • 鲅鱼圈网站开发哪家好哦爱站网关键词工具
  • 网站如何做vip等级竞价推广托管开户