I asked the programmer goddess for her QQ number, but...

I asked the programmer goddess for her QQ number, but...

introduction

A beautiful programmer came to our team, and I was secretly delighted. Haha, this is my chance. I was thinking how to start? Well, I'll start with the QQ number. I'll find the goddess and get her QQ number. Haha, I'm such a genius~~~

It's like this

The idea is beautiful, but the reality is cruel. When I asked the goddess for her QQ number, I didn't expect that the goddess didn't give it to me directly, but gave me a question (it's really a programmer's competition~~ ~). If I can't solve the problem, not only will I lose the opportunity to contact the goddess, but even the basic skill for survival - programming ability will be questioned~~~ The question is this:
Given a string of numbers (not a QQ number), you can find out the QQ number according to the following rules: first delete the first number, then put the second number at the end of the string, then delete the third number, and put the fourth number at the end of the string... repeat this process until there is only one number left, delete the first number as well, and connect these numbers together in the order you just deleted to get the goddess's QQ number.

[[137432]]

That's it. The goddess gave a string of numbers 631758924. Now what we need to do is to find the goddess' QQ number from this number. There are many ways to do it. For example, you can use 9 cards to write these 9 numbers respectively, simulate the process of the question, and calculate it, or you can use a pen to calculate one by one~~~~

These methods are too low-level and do not show the ability of programmers. It is cooler to write a program (actually, I am thinking that if I meet a goddess who asks such a question next time, the program will be very convenient, haha~~~)

Solution

The first method uses mathematical methods. According to the rules of the question, loop the following operations: round up => remainder => remainder*10+round up. ... The objects of the remainder and round up are all multiples of 10. Depending on the number of digits, each rounding results in a single digit. Loop until the number is equal to 0.

  1. <?php
  2. $raw_num = 631758924;
  3. $num = 0;
  4. $devisor = 1;
  5. while ( $devisor < $raw_num )
  6. {
  7.      $devisor *= 10; //Get the smallest integer that is a multiple of 10 greater than raw_num  
  8. }
  9.  
  10. while ( $raw_num > 0) {
  11.      $devisor /= 10;
  12.      $next = floor ( $raw_num / $devisor ); //Get the next number  
  13.      $num = $num *10 + $next ; //Calculate the "semi-finished product" QQ number  
  14.      $raw_num = $raw_num % $devisor ;
  15.      $last = floor ( $raw_num * 10 / $devisor ); //Move numbers and concatenate the QQ number of the user  
  16.  
  17.      $pre = $raw_num % ( ceil ( $devisor / 10));
  18.  
  19.      $raw_num = $pre * 10 + $last ;
  20. }
  21. echo   "Congratulations, you have successfully obtained the QQ number: {$num}" ; //Congratulations, you have successfully obtained the QQ number: 615947283  

Use the queue's FIFO to obtain the QQ number. According to the characteristics of the question, the queue can be used to process it. The queue is simple, convenient, and easier to understand.

  1. #include < stdio.h >  
  2. struct queue {
  3. int *data;
  4. int head;
  5. int tail;
  6. };
  7. int main()
  8. {
  9. int num, i;
  10. printf("Please output the length of the QQ number to be decrypted: ");
  11. scanf("%d", &num);
  12.       
  13. struct queue q;
  14.      q.data = (int *)malloc(sizeof(int)*(num*2-1)); //The total required array length is num*2-1
  15.      q.head = 0 ;
  16.      q.tail = 0 ;
  17.       
  18. for( i = 1 ;i < = num;i++)
  19. {
  20. scanf("%d", &q.data[q.tail]);
  21. q.tail++;
  22. }
  23.       
  24. printf("Congratulations, you have successfully obtained your QQ number:");
  25. while(q.head <   q.tail )
  26. {
  27. printf("%d", q.data[q.head]);
  28. q.head++;
  29.           
  30. q.data[q.tail] = q.data[q.head];
  31. q.tail++;
  32. q.head++;
  33. }
  34. return 0;
  35. }
  36.  
  37. #The following is an experiment
  38. Please input the length of the QQ number to be decrypted: 9
  39. 6
  40. 3
  41. 1
  42. 7
  43. 5
  44. 8
  45. 9
  46. 2
  47. 4
  48. Congratulations, you have successfully obtained the QQ number: 615947283. Please press any key to continue. . .

Congratulations, you have successfully obtained the QQ number: 615947283. Please press any key to continue. . .

Say something

The idea for this article comes from Chapter 2, Section 1 [Decrypting QQ Numbers — Queues] in Aha! Algorithms, and the only illustration is also taken from this book.

------------------------------------Dividing line------------------------------------

The js of @朱影 on the 21st floor uses array push and shift, which is also good (slightly modified), "push first and then shift":

  1. var n = "631758924" ;
  2. var arr = [];
  3. var res = [];
  4.  
  5. for ( var i=0; i<n.length; i++) {
  6. arr.push(n.charAt(i));
  7. }
  8.  
  9. while (arr.length) {
  10.      if (arr.length !== 1) {
  11. res.push( arr.shift() );
  12. arr.push( arr.shift() );
  13. } else {
  14. res.push( arr.shift() );
  15. }
  16. }
  17. console.log(res.join( '' )); //615947283  

<<:  10 Steps to Becoming a Professional iOS Developer — From Scratch

>>:  Make your PHP 7 faster (GCC PGO)

Recommend

Tencent architect teaches you how to write Android specification documents

Preface A qualified code should not only realize ...

How did cholera harm you? Which is more serious, cholera or COVID-19?

Mixed Knowledge Specially designed to cure confus...

Tractica: Asia Pacific AI market to reach $136 billion by 2025

The artificial intelligence (AI) market is beginn...

Should Apple open source Swift? Seven reasons for and against

It has only been a few months since Apple's n...

How to evaluate, monitor and promote KOL marketing channel conversion?

With the rapid development of the Internet , we h...

A set of online event planning templates

As we enter 2022, the epidemic, which had been do...

Advice from an old programmer: Don’t rely on technology for your whole life

[[225216]] I am doing it myself now, but I have b...

In just one day, two gophers transformed a volcano

The impact lasts for decades. On May 18, 1980, Mo...

Google expected to return to mainland China this fall

Reuters quoted technology blog Information as say...