C language learning elementary binary search daffodil number printing rhombus

Tip: Here you can add the directories of all articles in the series, and the directories need to be added manually.
For example: the first chapter Python machine learning introduction to the use of pandas


Tip: After writing the article, the table of contents can be automatically generated, how to generate it can refer to the help document on the right


Preface

For a student who re-learned the C language course, I would like to write some important notes here, and some difficult examples (of course, only for me), everyone can refer to.

1. C language is the easiest to start

1. Data Type

char        //字符数据类型
short       //短整型
int         //整形
long        //长整型
long long   //更长的整形
float       //单精度浮点数
double      //双精度浮点数

Each occupies a few bytes. Use the following function to check it, you know, this is best to remember.

#include<stdio.h>
void main(){
    
    
	printf("char= %d\n", sizeof(char));
	printf("short=%d\n", sizeof(short));
	printf("int=%d\n", sizeof(int));
	printf("long=%d\n", sizeof(long));
	printf("long long =%d\n", sizeof(long long));
	printf("double= %d\n", sizeof(double));
}

Insert picture description here

2. Variables, constants

As the name suggests, variables are values ​​that will change. Constants are values ​​that cannot be changed once they are defined.

  1. The scope of a local variable is the local scope of the variable.
  2. The scope of global variables is the entire project.
  3. The life cycle of a variable refers to the period of time between the creation of the variable and the destruction of the variable
  4. The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle of out of the scope ends.
  5. The life cycle of global variables is: the life cycle of the entire program.

3. String + escape character

A string of characters caused by double quotes is called String Literal, or string for short. (Note: The end mark of a string is an escape character of \0. When calculating the length of the string, \0 is the end mark and is not counted as a string)
Escape character
:? Used when writing consecutive question marks, Prevent them from being parsed into three-letter words.
'Used to denote character constants'
\" Used to denote a double quotation mark inside a string.
\ Used to denote a backslash, preventing it from being interpreted as an escape sequence character.
\a Warning Characters, buzzer
\b backspace
\f paper feed
\n line feed
\r carriage return
\t horizontal tab
\v vertical tab
\ddd ddd represents 1~3 octal numbers. For example: \130 X
\xdd dd means 2 hexadecimal digits. For example: \x30 0

#include <stdio.h>
int main()
{
    
    
    printf("%d\n", strlen("abcdef"));
    printf("%d\n", strlen("c:\test\328\test.c"));
    return 0;
}

The output results are: 6 14 The
first one is easy to understand. Here I divide the second one to make it easier to understand. The more embarrassing thing is that my understanding was wrong at the beginning, but I finally found that it was like this. For convenience Understand, I separate each character with #: c#:# \t#e#s#t#\32#8#\t#e#s#t#.#c, you can find a total of 13 #.

4. Select and loop statements

The choice sentence is what we call if, switch, switch. I think it is a special kind of choice sentence, which is similar to the choice of if and else, which is easier to understand. Note: The break can exit the switch, and the default clause is used for processing. If the case cannot be matched, it can be written before or after the case in the switch, it does not matter.
Loop statement
while
for
do while

#include <stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		printf("%d ", i);
		i = i + 1;
	}
	return 0;
}
#include <stdio.h>
int main()
{
    
    
 int i = 0;
 for(i=1; i<=10; i++)
 {
    
    
 printf("%d ", i);
 }
 return 0;
}
#include <stdio.h>
int main()
{
    
    
 int i = 10;
 do
 {
    
    
 printf("%d\n", i);
 }while(i<10);
 return 0;
}

Here again, the difference between break and continue, the former jumps out of the loop directly, and the latter ends this loop and enters the next loop.

2. Typical questions

1. Print rhombus

#include<stdio.h>
//打印菱形
int main()
{
    
    
	int i, j, N = 5;
	//上半部分
	for (i = 0; i < N; i++) //N控制上半部分行数
	{
    
    
		for (j = i; j < N - 1; j++) //随行数递增,空格数递减(这里每行前面的空格数分别为4,3,2, 1,0)
			printf(" ");
		for (j = 0; j < 2 * i + 1; j++) //随行数递增,“*”数递增(这里每行前面的*数分别为1,3,5,7,9)
			printf("*");
		printf("\n"); //记得换行
	}
	//下半部分,其实就是上半部分的翻转
	for (i = N - 2; i >= 0; i--) //这里i从N-2开始的,不知道为什么的可以换成N-1试试,保证秒懂
	{
    
    
		for (j = i; j < N - 1; j++) //这里的空格数刚好相反,是随行数递增的
			printf(" ");
		for (j = 0; j < 2 * i + 1; j++) //这里的“*”数则是递减的(想想图形的样子)
			printf("*");
		printf("\n"); //同样的记得换行
	}
	return 0;
}

2. Find and output all the "number of daffodils" between 0 and 100000.

#include <math.h>
void main(){
    
    
	int n;
	int a[6] = {
    
     0 };
	int i;
	int sum = 0;
	int count = 0;
	for (n = 0; n <= 100000; ++n){
    
     //遍历0-100000之间所有数字
		for (i = n; i; i /= 10){
    
    
			a[count] = i % 10;  //用数组a[count]将每一位存起来,用count记下数字的位数
			++count;
		}
		for (i = 0; i < count; ++i){
    
    
			sum += pow(a[i], count); //将数组中所存的数字的每一位进行对应的位数count次方并求和
		}
		if (n == sum){
    
    
			printf("%d \n", n);
		}
		count = sum = 0; //强制将count与sum归0
	}
}

3. Binary search

#include <math.h>
void main()
{
    
    
	int ar[] = {
    
     12, 23, 34, 45, 56, 67, 78, 90, 100, 120, 150, 200, 1234 };
	int n = sizeof(ar) / sizeof(ar[0]);
	int key;
	printf("input key:>");
	scanf("%d", &key);
	int low = 0;
	int high = n - 1;
	int mid, index = -1;
	while (low <= high)
	{
    
    
		mid = (low + high) / 2;
		if (key == ar[mid])
		{
    
    
			index = mid;
			break;
		}
		else if (key < ar[mid])
			high = mid - 1;
		else
			low = mid + 1;
	}
	if (index == -1)
		printf("要查找的%d不存在.....\n", key);
	else
		printf("要查找的%d在: 第%d个\n", key, index+1);
}

to sum up

Here is a little introduction, and there are many typical questions, the following functions, arrays, pointers, will be added, how to say, the second time to learn this language, I feel very different, not the first time, so ignorant Now, I know how to implement something for a thing, although sometimes the way it is implemented is rubbish, this is also the reason for too little writing, so I still have to disagree with the code.

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/109644258