[Sword refers to offer] - 2 selected interview questions (replace spaces)

content

 [Programming question] Replace spaces

 [Programming question] Fibonacci sequence


 [Programming question] Replace spaces

Please implement a function that replaces each space in a string with "%20". For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.

For interface type programming questions, you only need to follow the prompt code to implement this function;

The idea is as follows:

First, the number of spaces in the string is calculated, and the converted length of the string is determined by the number of spaces.

Every space becomes %20 is to encounter a space string length + 2

The string has two spaces, so add 4 to the string length. is end2;

 If str[end1]!=' ' then str[end2]=str[end1]

If str[end1] == ' ' then str[end2--] = '0';str[end2--] = '2';str[end2--] = '%20';end1--;

code show as below: 

class Solution {
public:
    void replaceSpace(char* str, int length) {
        //数空格
        int space = 0;
        char* p = str;
        while (*p)
        {
            if (*p == ' ')
                space++;
            p++;
        }
        int newlen = length + space * 2;
        int end1 = length - 1;
        int end2 = newlen - 1;
        while (end1 != end2)
        {
            if (str[end1] != ' ')
            {
                str[end2--] = str[end1--];
            }
            else
            {
                str[end2--] = '0';
                str[end2--] = '2';
                str[end2--] = '%';
                end1--;

            }
        }
    }
};

 [Programming question] Fibonacci sequence


The Fibonacci sequence is defined like this:
F[0] = 0
F[1] = 1
for each i ≥ 2: F[i] = F[i-1] + F[i-2]
Therefore, the Fibonacci sequence looks like : 0, 1, 1, 2, 3, 5, 8, 13, ..., the numbers in the Fibonacci sequence are called Fibonacci numbers. Give you an N, you want it to be a Fibonacci number, you can change the current number X to X-1 or X+1 at each step, now give you a number N to find the minimum number of steps required to become a Fibonacci number.

Give you a number N to find the minimum number of steps required to become a Fibonacci number

analyse as below:

Enter the number n, and first determine whether n belongs to the Fibonacci sequence through the loop. If it belongs to print 0, it means that 0 steps are required to make n a Fibonacci sequence; if n does not belong to the Fibonacci sequence, then use n and b is compared, if b>n, compare the distances from a to n and b to n, that is, compare the absolute values ​​of (an) and (bn), if the absolute value of an is large, output the absolute value of bn, The opposite is true.

 

 

#define _CRT_SECURE_NO_WARNINGS

//斐波那契数列 求最小步数
#include<stdio.h>
#include<math.h>

int main()
{
	int a = 0;
	int b = 1;
	int c = 1;
	int n = 0;
	scanf("%d", &n);
	while (1)
	{
		if (b == n) 
		{
			printf("0\n");
			break;
		}

		else if(b > n)
		{
			if (abs(a - n) > abs(b - n))
			{
				printf("%d", abs(b - n));

			}
			else
			{
				printf("%d", abs(a - n));

			}
			break;
		}
		a = b;
		b = c;
		c = a + b;
	}


	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_53939785/article/details/124129440
Recommended