Home  >  Article  >  Backend Development  >  Looking for a QQ number from a goddess

Looking for a QQ number from a goddess

WBOY
WBOYOriginal
2016-08-08 09:22:301092browse

Introduction

A beautiful programmer came to our team, I was secretly happy, haha, now is the opportunity. I'm thinking about how to start? Okay, let’s start with the QQ number. To find a goddess, you have to get a QQ number. Haha, I’m really a genius~~~

This is it

The idea is beautiful, but the reality is cruel. When looking for a goddess, you need a QQ number. , I didn’t expect that the goddess didn’t give it to me directly, but gave me a question (it was really a competition between programmers~~~). I was given all the questions. If I couldn’t do it, not only would I lose the opportunity to contact the goddess, Even the basic skill of making a living - programming ability has been questioned~~~The question is like this:

<code>给了一串数字(不是QQ号码),根据下面规则可以找出QQ号码:首先删除第一个数,紧接着将第二个数放到这串数字的末尾,再将第三个数删除,并将第四个数放到这串数字的末尾......如此循环,知道剩下最后一个数,将最后一个数也删除,按照刚才删除的顺序,把这些数字连在一起就是女神的QQ号码啦。</code>

Looking for a QQ number from a goddess

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

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

Solution

The first method is to use mathematical methods and cycle the following operations according to the rules of the question: rounding => remainder => remainder * 10 + rounding. . . . . The objects of remainder rounding are all multiples of 10, depending on the number of digits. After each rounding, there is one digit, and the cycle continues until the number is equal to 0.

<code><?php
$raw_num = 631758924;
$num = 0;
$devisor = 1;
while($devisor < $raw_num)
{
	$devisor *= 10;		//获取最小的大于raw_num的10的倍数的整数
}

while ($raw_num > 0) {
	$devisor /= 10;
	$next = floor($raw_num / $devisor);	//获取下一个数字
	$num = $num*10 + $next;				//计算”半成品“QQ号码
	$raw_num = $raw_num % $devisor;
	$last = floor($raw_num * 10 / $devisor);	//移动数字,拼接最新的QQ号码

	$pre = $raw_num % (ceil($devisor / 10));

	$raw_num = $pre * 10 + $last;	
}
echo "恭喜你啦,成功获取QQ号码:{$num}";	//恭喜你啦,成功获取QQ号码:615947283</code>

Use the FIFO of the queue to obtain the QQ number. According to the characteristics of the question, you can just use the queue to process it. The queue is simple, convenient, and easier to understand.

<code>#include<stdio.h>
struct queue {
	int *data;
	int head;
	int tail;
}; 
int main()
{
	int num, i;
	printf("请输出要破译的QQ号码长度:");
	scanf("%d", &num);
	
	struct queue q;
	q.data = (int *)malloc(sizeof(int)*(num*2-1));	//总共需要的数组长度为num*2-1 
	q.head = 0;
	q.tail = 0;
	
	for(i=1;i<=num;i++)
	{
		scanf("%d", &q.data[q.tail]);
		q.tail++;
	}
	
	printf("恭喜你啦,成功获取QQ号码:"); 
	while(q.head < q.tail)
	{
		printf("%d", q.data[q.head]);
		q.head++;
		
		q.data[q.tail] = q.data[q.head];
		q.tail++;
		q.head++;
	}
	return 0;
}

#下面是一个实验
请输出要破译的QQ号码长度:9
6
3
1
7
5
8
9
2
4
恭喜你啦,成功获取QQ号码:615947283请按任意键继续. . .</code>

Say something

The idea for this article comes from "Aha!" In Chapter 2, Section 1 [Decrypting QQ Numbers - Queue] in "Algorithm", the only illustration is also taken from this book.


WeChat ID: love_skills

The harder you work, the luckier you will be! The luckier you are, the harder you work!

Being a CEO is not a dream

Winning Bai Fumei is not a dream

Disi counterattack is not a dream

It’s now! ! Come on
Looking for a QQ number from a goddess

The above is an introduction to asking for a QQ number from a goddess, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn