C preliminary test

Table of contents

1 knowledge point

2 Find the value of a

3 Find the value of n

4 Find the greatest common multiple

5 reverse string


1 knowledge point

(1) When you see i++, ++i, etc., you should consider whether it is pre-positioned or post-positioned, and don't just add one and think more.

(2) The defaule statement in the switch statement may not be written

(3) The basic unit of a C program is a function.

(4) Write the comment wherever you want, but don't write it in a weird way.

(5) C language can write multiple statements per line, but we are used to writing one statement per line.

(6) The C language itself has no input and output statements, and the library functions of the C language are not officially provided, but provided by the compiler manufacturer.

(7) In the process of compiling a c program, errors in comments cannot be found.

(8) *p++ is p++, then dereference, because it is post ++, so use then ++, *p, then ++

(9) 10|11, | is bitwise or

(10) Transformation between integers and integer promotion, conversion between floating-point types and arithmetic conversion, arithmetic conversion between integers and floating-point types

2 Find the value of a

Code display:

#include <stdio.h>
#include <stdlib.h>
int a = 1;
void test()
{
	int a = 2;
	a += 1;
}
int main()
{
	test();
	printf("%d\n", a);
	return 0;
}

The local variable a of the function body is destroyed when the function exits, so the value of printing a is 1 for the global variable.

3 Find the value of n

Code display:

#include <stdio.h>
int main()
{
	int i = 0;
	int j = 0;
	int n = 0;
	for (i = 0, j = 0; (j = 123) && (i < 4); i++)
	{
		n++;
	}
	printf("%d", n);
	return 0;
}

Note: The value of j is assigned 123, not a judgment. So n = 4.

4 Find the greatest common multiple

Code 1 shows:

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	int c = (a > b) ? (a) : (b);
	int i = c;
	while (i % a != 0 || i % b != 0)
	{
		i++;
	}
	printf("%d", i);
	return 0;
}

Code 2 shows:

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	int i = 1;
	while ((i * a) % b != 0)
	{
		i++;
	}
	printf("%d", i * a);
	return 0;
}

Idea: A number (starting from 1) multiplied by a, and then divided by b, is the least common multiple.

5 reverse string

Invert the words of a sentence without inverting the punctuation. For example: I like beijing. After the function, it becomes beijing. like I

(separated by spaces)

Code display:

#include <stdio.h>
#include <assert.h>
#include <string.h>
void reverse(char* left, char* right)
{
	assert(left && right);
	while (left < right)
	{
		char tmp = *left;
		*left = *right;
		*right = tmp;
		left++;
		right--;
	}
}
int main()
{
	char arr[100] = { 0 };
	gets(arr);//输入
	int len = strlen(arr);//倒置
	char* start = arr;
	char* end = arr;
	while (*end != '\0')//单个单词逆序
	{
		while (*end != ' ' && *end != '\0')
		{
			end++;
		}
		reverse(start, end - 1);
		while (*end == ' ')
		{
			start = end + 1;
			end = end + 1;
		}
	}
	reverse(arr, arr + len - 1);//输出
	printf("%s", arr);
	return 0;
}

Idea: first reverse the order of each word, keep the spaces unchanged, put the punctuation marks on the last word, regard the punctuation marks as part of the word, and then reverse the order collectively.

Words in reverse order Words in reverse order are all strings in reverse order.

Guess you like

Origin blog.csdn.net/m0_57388581/article/details/123834653