Be sure to know#Collection several typical programming that C language beginners must know

Table of contents

Functional judging prime numbers

functional dichotomy

array replacement

Find the number of different bits in binary

password input

recursive output single value

Find the number of ones in binary

foreword

This article will combine some typical examples to explain the knowledge points. At the same time, in order to make it easier for everyone to understand, I will add some necessary notes in the programming for everyone to understand. The directories are arranged in no order. The author, I am also a beginner of C language B station, welcome to learn and communicate together, criticize and correct.

1. Functional judgment of prime numbers

What are prime numbers?

A prime number, also known as a prime number, refers to a number greater than 1 that cannot be divisible by other natural numbers except 1 and the integer itself (it can also be defined as a number with only two factors of 1 and itself). Numbers greater than 1 but not prime are called composite numbers. 1 and 0 are neither prime nor composite. Prime numbers play an important role in number theory.

Code analysis:

#include<stdio.h>
//当实参实比例化之后其实相当于实参的一份临时拷贝
int is_prime(int x)
{
    int j;
    for(j = 2;j <= sqrt(x); j++)
    {
        if(x%j == 0)
        return 0;
    }
    return 1;
}
int main()
{
//打印1-100内的素数
    for(int arr = 1;arr <= 100; arr++)
    {
        if(is_prime(arr) == 1)
        {
            printf("%d ",arr);
        }
    }
}

Our function is_prime is to judge whether X is a prime number, and return 1 if it is, and return 0 if it is not, where x%j==0 is the judgment condition for prime numbers within 1-100, we use the for loop to execute this condition, It is to divide the number X by all the numbers of 2-x to judge whether there is a number divisible by him in the process of traversal, and if there is, it is not a prime number.

functional dichotomy

What is dichotomy?

Simply put, the dichotomy method is a search method in which one is divided into two, and it is illustrated with a diagram:
8546a3f2de54401da0c65f65919e3cea.png

 Code analysis:

#include<stdio.h>
int is_ar(int arr[],int q,int sz)
{
   int left = 0;
//元素个数减一就是数组下标
   int right = sz - 1;
   while(left <= right)
{
//数组中间的元素下标
   int mid = (left + right)/2;
  if ( arr[mid] > q)
 {
     right = mid -1;
 }
     else if(arr[mid] < q)
 {
    left = mid +1;
 }
     else{
   return mid;
 }
}
  if(left > right)
{
    return -1;
}
}
int main()
{
//arr数组1-10
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
//查找9
    int q = 9;
//元素个数
    int sz = sizeof(arr)/sizeof(arr[0]);
//查找函数
    int s = is_ar(arr,q,sz);
  if( s == -1){
    printf("no");
         }
     else{
    printf("this is %d",s);
         }
    return 0;
}

Search for 9 in 1-10, return -1 if not found, and return other means found. The number to be searched is always compared with the middle element in the array to see which interval it is in, so the area is divided.

array replacement

#include<stdio.h>
#include<string.h>
#include<windows.h>
int main ()
{
    char arr [] = "wertsyui";
    char  arr2[] = "########";
    int c = 0;
    int d = sizeof(arr)/sizeof(arr[0])-2;
   while(c <= d)
   {
       arr2[c] = arr[c];
       arr2[d] = arr[d];
       c++;
       d--;
       Sleep(1000);
       system("cls");
       printf("%s\n",arr2);
   }
}

Here is to replace the # sign in the array arr2 with the characters in the arr array. The subtraction in int d is to subtract /0 by one. Here are two library functions in Windows.h. Sleep is a slow time function, where 1000 is 1 second; system is a console function, and cls is a console clearing.

Find the number of different bits in binary

Here is the input of two values, which are converted to binary and compare the number of different bits in their binary sequence.

#include<stdio.h>
//异或 不同为1,同则为0
//求二进制不同位个数
int get_diff_bit(int n,int m)
{
    int count;
    int tmp = n^m;
  while(tmp)
  {
      tmp = tmp&(tmp-1);
      count++;
  }
  return count;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int count = get_diff_bit(n,m);
    printf("%d",count);
}

Here is XOR comparison. 

password input

#include<stdio.h>
int main()
{
    int ret = 0;
    char password[20] = {0};
    int ch = 0;
    printf("请输入密码\n");
    scanf("%s",password);
    //输入密码时确认会输入回车
    //这时缓存区会接收一个\n
    //这个\n会被getchar接收
    //所以再输入一个getchar()接收/n
   while((ch = getchar() )!= '\n')
   {
       ;
   }
    printf("是否确认");
    ret = getchar();
    if(ret == 'y')
    {
        printf("输入成功\n");

    }
    else{
        printf("输入失败\n");
    }
    return 0;
}

Here we did not set the correct password, but just mentioned a password input format. It should be noted that while((ch = getchar() )!= '\n') { ; } is to judge whether the input password is empty, ( ch = getchar() )!= '\n' is a common character comparison function, ret = getchar(); has the same function as scanf.

recursive output single value

#include<stdio.h>
//strlen Çó×Ö·û´®³¤¶È
int my_strlen(char* str)
{
    int count = 0;
    while (*str != '\0')
    {
        count++;
        str++;
    }
    return count;
}
int main ()
{
    char arr[] = "bit";
    int len =my_strlen(arr);
    printf("len = %d\n",len);
    return 0;
}

Use the recursive method to calculate the number of strings in the arr array. *str != '\0' in our function int my_strlen(char* str) is to judge whether the value of the last array element is \n when \n is encountered Then str++, and count counts ++.

Find the number of ones in binary

int teat( int n)//这个可以算负数
{
    int i,count=0;
   for(i =0;i < 32;i++)
   {
       if(((n >> i) & 1) == 1)//按位与 只有俩个都为1则为1
        //这里再添加一个方法 if n = n & (n - 1);count++;有几个1循环几次
       {
           count++;
       }
   }
   return count;
}
int main()
{
     int arr;
    scanf("%d",&arr);
    int count = teat(arr);
    printf("%d",count);
    system("pause");//system库函数-执行系统命命-pause(暂停)
    return 0;
}

Here, bitwise AND is used to determine the number of ones when only two are one, and if in if, & is followed by one level of ++.

 

 

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/130165502