This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

String replaces space (one more)

Interview questions:

image-20210910180703837


Interview questions need to consider norms, algorithms, code, portability, robustness, etc., so it’s comfortable to start with that in mind

First OF all, I saw that it was %20 to replace the space, in case the interviewer said to change something else and you quickly changed it out. This is also a skill, so the function is indispensable, and it should also have the appearance of human-computer interaction, anyway, he did not write dead.

code

Here, the time complexity is also O(n), and the robustness is good. Basically, it is the code that can win the offer

#include<stdio.h>
#include<assert.h>
#include<string.h>
char* replace(char* parr, const char* e1, const char* e2)
{
	assert(e1 && e2);
	char arr2[100] = { 0 };// Create an array to store the changed string
	unsigned int len = strlen(parr);
	unsigned int len1 = strlen(e1);
	unsigned int len2 = strlen(e2);
	int i = 0;// To trace parr
	int j = 0;// To trace arR2
	int z = 0;// to track e2
	for (i = 0; i < len; i++)
	{
		if(parr[i] ! = *e1) { arr2[j++] = *(parr+i); }else
		{
			for (z = 0; z < len2; z++) { arr2[j++] = *(e2 + z); }}}return arr2;
}


int main(a)
{
	char arr[100] = { 0 };// Give enough space
	while (1)
	{
		printf("Please enter the original string :>");
		gets(arr);
		printf("Changed string :>%s\n", replace(arr, ""."% 20"));
	}
	return 0;
}
Copy the code

The results of

image-20210910203357973


Count ball search

Title :13 balls a balance, now know only one weight is different from the others, ask how to use three times to find the ball?

1.

The most important thing in the question is the number of balls. The number of balls determines how many times to find a different ball. Therefore, it is our favorite finding rule (only one ball with different weight).

A ball

image-20210910221920871


Two balls

image-20210910222400352


Three goals

This is the minimum number of balls, and the minimum experiment can be done

For the first time,

Lucky once

The second time

Direct result

image-20210910224340864


Four balls

You can actually see it more or less at this point

image-20210910232732693


Five balls

This is where the ball starts to mix. Lucky, one shot

The first time I was lucky I found it

image-20210910234912828


Law, (the first time to find the wrong, wasted a long time, directly see if there is a mathematical law it)

The following are the laws of mathematics to lay the foundation, you do not doubt, I tried to find their own, came to the end of the wrong, the following are changed

Twelve balls

For the first time,

The first time I’m with you is a simple extension

image-20210913191056502


The real beauty is the first difference