Table of contents
Method 1: scanf matches a specific character method
Method 2: Double pointer method
2. Count the total number of rabbits every month
Method 2: Fibonacci's recursion
3. Pearls and magic calculations
4. Look for odd numbers (singles)
Foreword:
If you want to learn programming well, you need to learn more questions. We not only need to do more questions, but also do well! For this reason, I started a series of must-do questions for cornering and overtaking. This is the third programming question , each with about 5 questions. This series will be updated from time to time, so stay tuned!
1. Word inversion
Method 1: scanf matches a specific character method
#include <stdio.h>
int main()
{
char str[1000][1000];
int i = 0;
while (1)
{
if (scanf("%[a-zA-Z]", str[i]))
{
i++;
}
if (getchar() == '\n')
{
break;
}
}
while(i)
{
printf("%s ", str[--i]);
}
return 0;
}
Parse:
Method 2: Double pointer method
Problem-solving ideas:
- The overall idea is
双指针法
to define a pair of fast and slow pointers p1, p2.- First look for a word
末尾
, so when s[p1] is not a letter, p1 needs to --, when the loop terminates, as long as p1 will point to the end of a word (if there is a word), then p2 will be used to record the end of the word .- Continue to look for words
开头
, so when s[p1] is a letter, let p1--, and finally p1+1 points to the beginning of the word, we traverse from p1+1 to p2, and print in turn.
The scanf function cannot accept strings with spaces, so use gets
the function
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[10000];
gets(s);
int len = strlen(s);
int p1 = len - 1, p2 = len;
while (p1>=0)
{
while (p1 >= 0 && !isalpha(s[p1]))
{
p1--;
}
p2 = p1;
while (p1 >= 0 && isalpha(s[p1]))
{
p1--;
}
int i;
for (i = p1 + 1; i <= p2; i++)
{
printf("%c", s[i]);
}
printf(" ");
}
return 0;
}
2. Count the total number of rabbits every month
Method 1: Fibonacci Sequence
We can first deduce
Only one in the first month
Only one in the second month
In the third month, the original one gave birth to a total of 2
In the fourth month, the first one gave birth to another one, a total of 3
In the fifth month, the first one will give birth to one, and from the second to the third month, one will be born, a total of 5
In the sixth month, the first one gave birth to one, the second gave birth to one, and the third gave birth to one, totaling 8
1 1 2 3 5 8……
It can be found that f(n) = f(n-1)+f(n-2)
Essentially the Fibonacci sequence
#include<stdio.h>
int main()
{
int arr[31],i = 0,n;
arr[0] = 1; arr[1] = 1;
scanf("%d", &n);
for (i = 2; i < n; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
printf("%d", arr[n - 1]);
return 0;
}
Method 2: Fibonacci's recursion
#include<stdio.h>
int fb(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
return fb(n - 1) + fb(n - 2);
}
int main()
{
int n = 0;
scanf("%d", &n);
printf("%d", fb(n));
return 0;
}
3. Pearls and magic calculations
Zhuji Miracle https://leetcode.cn/problems/master-mind-lcci/
Method: traverse
int* masterMind(char* solution, char* guess, int* returnSize) {
int* ret = (int*)malloc(sizeof(int) * 2);
ret[0] = 0; ret[1] = 0;
int i = 0,j=0;
for (i = 0; i < 4; i++)
{
if (solution[i] == guess[i])
{
ret[0]++;
solution[i] = ' ';
guess[i] = ' ';
}
}
for (i = 0; i < 4; i++)
{
if (solution[i]!=' ')
{
for (j = 0; j < 4; j++)
{
if (solution[i] == guess[j])
{
ret[1]++;
guess[j] = ' ';
break;
}
}
}
}
*returnSize = 2;
return ret;
}
Parse:
solution: RGBY
guess: GGRR
Traverse guess first, each character corresponds one by one
R-G G-G B-R Y-R
We regard the same as a guess, that is, G==G, that is, once, and offset these two characters
Then we traverse each bit in the guess again. At this time, we need to search whether there is this character in the entire solution. If there is, offset the characters in the solution and guess, and treat it as a false guess, and then the next character is the same
4. Look for odd numbers (singles)
Method 1: traversal method
#include<stdio.h>
int main()
{
int n = 0, arr[2000000],i=0,count=0,j=0,flg=0;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n-1; i++)
{
if (arr[i] < 0)
{
continue;
}
count = 1;
for (j = i+1; j < n; j++)
{
if (arr[j]>0 && arr[i] == arr[j])
{
arr[j] = arr[j] * -1;
count++;
}
}
if (count % 2 != 0)
{
flg = arr[i];
break;
}
}
printf("%d",flg);
return 0;
}
but will time out
Method 2: XOR algorithm
Summary: Use the XOR operation to calculate the number that appears odd number of times
#include<stdio.h>
int main()
{
int n = 0;
int ans = 0;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
int tmp = 0;
scanf("%d",&tmp);
ans ^= tmp;
}
printf("%d\n", ans);
return 0;
}
5. Intercept the string
Parse:
To intercept the first n characters of a string, you only need to replace the data at the n subscript position of the array with the end of the string
#include <stdio.h>
int main()
{
char str[101];
scanf("%s", str) > 0;
int n;
scanf("%d", &n);
str[n] = '\0';
printf("%s\n", str);
return 0;
}
The above is my personal sharing, if you have any questions, welcome to discuss! ! !
I've seen this, why don't you pay attention and give a free like