n 个数字(0,1,…,n-1)形成一个圆圈,从数字0 开始,每次从这个圆圈中删除第m 个数字

题目:

n 个数字(0,1,…,n-1)形成一个圆圈,从数字0 开始,每次从这个圆圈中删除第m 个数字(第一个为当前数字本身,第二个为当前数字的下一个
数字), 当一个数字删除后,从被删除数字的下一个继续删除第m 个数字。
求出在这个圆圈中剩下的最后一个数字。

该题目是以下题目的变形。

(n 个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),
凡报到3的人退出圈子,问最后留下的是原来第几号的那个人?)

 

思路一:

该题目还是比较简单的,在一个数组中不断的进行遍历,从Index 0开始进行数,当Index值等于m的时候,就把该位置值设置为退出标志(0), 不断的进行重复,但是要小心一个点就是当遇到Index值等于n的时候,重新设置Index值从0开始。

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include <assert.h>  
  3.   
  4. /*-------------------------------- 
  5. 删除第N个数字只到剩下最后一个数字. 
  6. Copyright by yuucyf.  2011.07.21 
  7. ---------------------------------*/  
  8. int RemainLastOne(int i32Count, int i32Pos)  
  9. {  
  10.     assert(i32Count > 0 && i32Pos > 0);  
  11.     int *pi32ArrNum = new int[i32Count];  
  12.     assert(NULL != pi32ArrNum);  
  13.     for (int i32I = 0; i32I < i32Count; i32I++)  
  14.         pi32ArrNum[i32I] = i32I+1;  
  15.   
  16.     int i32ExitPos = 0;  
  17.     int i32CurPos = 0;  
  18.     int i32ExitCnt= 0;  
  19.     while(i32ExitCnt != i32Count-1)  
  20.     {  
  21.         if (pi32ArrNum[i32CurPos] != 0)  
  22.             i32ExitPos++;  
  23.   
  24.         if (i32ExitPos == i32Pos)  
  25.         {  
  26.             pi32ArrNum[i32CurPos] = 0; //设置退出标志位(对应的数组元素置为0)。  
  27.             i32ExitPos = 0;  
  28.             i32ExitCnt++;  
  29.         }  
  30.   
  31.         i32CurPos++;  
  32.         if (i32CurPos == i32Count)  
  33.             i32CurPos = 0;  
  34.     }  
  35.   
  36.     int RetVal = 0;  
  37.     for (int i32J = 0; i32J < i32Count; i32J++)  
  38.     {  
  39.         if (pi32ArrNum[i32J] != 0)  
  40.         {  
  41.             RetVal = pi32ArrNum[i32J] - 1;  
  42.         }     
  43.     }  
  44.     delete [] pi32ArrNum;  
  45.     return RetVal;  
  46. }  
  47.   
  48. int RemainLastOne_2(int i32Count, int i32Pos)  
  49. {  
  50.     assert(i32Count > 0 && i32Pos > 0);  
  51.   
  52.     // if there are only one integer in the circle initially,  
  53.     // of course the last remaining one is 0  
  54.     int i32Value = 0;  
  55.   
  56.     // find the last remaining one in the circle with n integers  
  57.     for (int i32I = 1; i32I <= i32Count; i32I++)  
  58.         i32Value = (i32Value + i32Pos) % i32I;  
  59.   
  60.     return i32Value;  
  61. }  
  62.   
  63.   
  64. int _tmain(int argc, _TCHAR* argv[])  
  65. {  
  66.     _tprintf(_T("The last one is %d.\n"), RemainLastOne(20, 4));  
  67.     return 0;  
  68. }  


 

Kafka拉取据的配置设置主要包括以下几个方面: 1. 消费者组的配置:消费者组是Kafka中用于分组管理消费者的概念,可以通过设置消费者组来实现负载均衡和故障转移。在消费者代码中,需要设置消费者组的ID,以便Kafka可以将多个消费者组织成一个消费者组。 2. 消费者配置:消费者配置包括消费者ID、自动提交偏移量、读取超时时间等参。消费者ID是用于唯一标识消费者的字符串,自动提交偏移量可以设置消费者是否自动提交读取位置,读取超时时间可以设置消费者等待据的超时时间。 3. 消费者订阅的主题和分区:在消费者代码中,需要指定消费者订阅的主题和分区,以便Kafka可以将消息发送到正确的消费者。 4. 消费者的消费逻辑:在消费者代码中,需要编写消费逻辑,以处理从Kafka中读取的消息。消费逻辑可以根据业务需求进行自定义,例如将消息写入据库、发送到其他系统等。 下面是一个使用Java语言编写的Kafka消费者的配置示例: ```java Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "1000"); props.put("session.timeout.ms", "30000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("test-topic")); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value()); // TODO: 消费逻辑 } } ``` 在这个示例中,我们通过设置Properties对象来配置消费者的参,包括Kafka集群的地址、消费者组ID、自动提交偏移量、反序列化器等。然后创建一个KafkaConsumer对象,并通过subscribe()方法订阅一个主题。在while循环中,我们通过poll()方法从Kafka中读取消息,然后通过for循环遍历消息,并对消息进行处理(这里只是简单地打印消息的内容)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值