OJ achieve some common questions -In-C-Language

Some implementations of the basic problems finishing Oj platform


Macro definition

#define ABS(x) ((x) > 0 ? (x) : -(x))       //绝对值
#define MAX(x, y) (((x) > (y)) ? (x) : (y)) //两个数最大值
#define MIN(x, y) (((x) < (y)) ? (x) : (y)) //两个数最小值
#define toupper(ch) ((ch) - 'a' + 'A')      //小写转换为大写
#define tolower(ch) ((ch) - 'A' + 'a')      //大写转换为小写
#define SWAP(a,b) {a=a^b; b=a^b; a=a^b;}    //交换两个数值

Integer-related

Determining if a number is an integer

//使用fmod对1取模是否为0
float x;
if(fmod(x,1) == 0)
//第二种
float b;
if(b - (int)b == 0)
    

% Operator
% only for integer calculation, after a number is not 0, the data involved in computing can be positive or negative.

FMOD () function
FMOD () function can be a modulo operation on the floating-point data, may be after a number 0, then the function returns NaN.

Determine the number of digits

int length_int(int x)
{
    int count = 0;
    while (x != 0)
    {
        x /= 10;
        count++;
    }
    return count;
}

Exchange two numbers (without the use of temporary variables)

int a,b; //a=11 b=99
a=a+b;
b=a-b;
a=a-b;   //a=99 b=11

Remove the digital number on you

int figure[1000];
//逆序输出          x 必须为 正数 
int figure_int(int x, int figure[])
{
    for (int i = 0; x > 0; i++)
    {
        figure[i] = x % 10;
        x /= 10;
    }
}              // x =1892               [0]=2 [1]=9 [2]=8 [3]=1

Digital output reverse

int input,output = 0;
    while (input)
    {
        output = output * 10 + input % 10;
        input/=10;
    }
printf("%d",output);

Sorting Algorithm

Bubble algorithm

#include <stdio.h>
void bubble_sort(int arr[], int len)
{
    for (int i = 0; i < len - 1; i++)   //外侧循环次数为 len-1 或 len
        for (int j = 0; j < len - 1 - i; j++)   //内循环次数为  len-1-i
        {
            int temp;
            if (arr[j] > arr[j + 1])   //从小到大             升序
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
}
int main()
{
    int arr[] = {22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70};
    int len = (int)sizeof(arr) / sizeof(*arr); //  数组长度
    //sizeof测字节长度.
    //short int 字节长  2 , int,long int,float 字节长为4  ,double ,long long int 字长 8
    bubble_sort(arr, len);
    return 0;
}

Selection Sort

#include <stdio.h>
void selection_sort(int arr[], int len)
{
    for (int i = 0; i < len - 1; i++)     //外侧循环次数为 len-1
        for (int j = i + 1; j < len; j++) //内循环 为 len
        {
            if (arr[i] > arr[j])         //从小到大             升序
            {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
}
int main()
{
    int arr[] = {22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70};
    int len = (int)sizeof(arr) / sizeof(*arr); //  数组长度
    //sizeof测字节长度.
    //short int 字节长  2 , int,long int,float 字节长为4  ,double ,long long int 字长 8
    selection_sort(arr, len);
    return 0;
}

Print triangle

Pyramid _1

           *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
#include <stdio.h>
int main(void)
{
    int rows;
    scanf("%d", &rows);
    for (int i = 1; i <= rows; i++)
    {
        for (int blank = 1; blank <= rows - i; blank++) //打印空格
        {
            printf("  "); //两个空格               第i行有2*(行数-i)个空格
        }
        for (int k = 0; k != 2 * i - 1; k++) //打印(星号+空格) 1-3-5-7-9...
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Inverted triangle _1

#include <stdio.h>
int main(int argc, char *argv[])
{
    int rows;
    scanf("%d", &rows);
    for (int i = rows; i >= 1; i--)
    {
        //打印空格
        for (int j = 0; j < rows - i; j++)
        {
            printf("  ");
        }
        for (int j = 0; j < 2 * i - 1; j++)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Digital triangle

           1
         2 3 2
       3 4 5 4 3
     4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include <stdio.h>
int main(int argc, char *argv[])
{
    int rows;
    scanf("%d", &rows);
    for (int i = 1; i <= rows; i++)
    {
        //打印空格
        for (int j = 0; j < rows - i; j++)
        {
            printf("  ");
        }
        //左小一半
        for (int k = i; k < 2 * i - 1; k++)
        {
            printf("%d ", k);
        }
        //右大半
        for (int k = 2 * i - 1; k >= i; k--)
        {
            printf("%d ", k);
        }
        printf("\n");
    }
    return 0;
}

Pascal's Triangle

Freud Triangle

    1
    2    3
    4    5    6
    7    8    9   10
   11   12   13   14   15
   16   17   18   19   20   21
#include <stdio.h>
int main()
{
    int N;
    scanf("%d", &N);
    int j = 1;
    for (int i = 1; i <= N; i++) //i控制打印行数
    {
        for (int l = 1; l <= i; l++, j++) //l 控制每行打印 l 个数字 
            printf("%5d", j);   //j 为打印出来的数字,实为总共进行循环的次数
        printf("\n");
    }
    return 0;
}

Ary digital conversion

Binary -> Decimal

Decimal -> Binary

String-related

Delete spaces in the string

Baidu really not, I love Google, I love Stack overflow

//Version 1:
void remove_spaces(char *s)
{
    char *d = s;
    while (*s != 0)
    {
        while (*d == ' ')
        {
            d++;
        }
        *s = *d;
        s++;
        d++;
    }
} // https://stackoverflow.com/questions/1726302/removing-spaces-from-a-string-in-c

//Version 2:
void delete_spaces(char *s)
{
    for (int i = 0, j = 0; s[i] != 0; i++)
    {
        if (s[i] != ' ')
        {
            s[j] = s[i];
            j++;
        }
        s[j] = 0;
    }
} 

Other minor problems

scanf Rear gets

//形似
int n;char srt[10];
scanf("%d",&n);
//getchar();        //解决方法
gets(str);   //这里gets不会执行

scanfWhen the function of input characters other than read,
not to receive '\n', to '\n'remain in the input buffer, the back is getswhen a character is received;
and getsin reading '\n'after the end of the string believe, so you had nothing to see but grumbled.
The solution is to getsadd that before getchar(), those damn '\n'removed from the buffer.

Ambiguity pointer array / array of pointers

int* arr1[8];    //declare arr1 as array 8 of pointer to int
int (*arr2)[8];  //declare arr2 as pointer to array 8 of int
int *(arr3[8]);  //declare arr3 as array 8 of pointer to int
int (*a[8])[5];  //a is an array of pointers to integer array of size 5 
char *(*(**foo [][8])())[];  //foo is array of array of 8 pointer to pointer to function returning pointer to array of pointer to char
int (*(*)())();  //foo is a pointer to function returning pointer to function returning int

More

Multidimensional arrays of pointers

int a[i][j];
/*
a      :  指向第 0 行数组地址
a + i  :  指向第 i 行数组地址
*(a + i)        ==> a[i]      : 指向第 i 行首元素地址
*(a + i)+j     ==> &a[i][j]  : 指向第 i 行,第 j 列元素地址
*(*(a + i) + j) ==> a[i][j]   : 第 i 行, 第 j 列的元素
*/

Output multi-dimensional array of characters

#include <stdio.h>
int main(void)
{
    char a[3][6] = {"Illlll","Loveoo", "China"};
    puts(a);
    printf("%s", a);
    return 0;
}
//输出
/* IlllllLoveooChina
   IlllllLoveooChina */

The problem is not to get rid of

Enter a string length less than 1000, the numbers and non-numeric characters (may contain spaces), wherein the consecutive number as an integer, calculates and outputs the string and all integers.

For example: Enter:?! B234x7892 2% tab7654
which can form 234,7892,2,7654 total of four integers, the output and four integers, as follows: 15782

Error practices are as follows:

#include <stdio.h>
int main(void)
{
    char a[1000];
    gets(a);
    int sum = 0, sum1 = 0;
    for (int i = 0; a[i]!= 0; i++)
    {
        if (a[i]>='0' && a[i]<='9')
        {
            sum1 = a[i] + sum1 * 10; //Q1 : sum1 = a[i] -'0' + sum1 * 10;
        }
        else
        {
            sum += sum1;
            sum1 = 0;
        }  
    } //Q2 : sum+=sum1;
    printf("%d", sum);
    return 0;
}
  • Q1: ANSI is a character array digital code, to be turned into digital a[i]-'0’
  • Q2: if the last character array ends with a number, the last number is not added sumin.

Josephus

It is known n individuals (with numbers 1,2,3 ... n, respectively) were sitting around a round table. From the number of people began to count off 1, that person's number to the m column; his next person and from 1 Countin, that person has a number of columns to m; and so the law is repeated until around round table were all out of the line. Such as

Input: 103
Output: 4

//方法1 : 还不懂
#include <stdio.h>
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    int f = 0;
    for (int i = 2; i <= n; ++i)
        f = (f + m) % i;
    printf("%d\n", f + 1);
    return 0;
}

Equation Sn = 1 + 11 + 121 + 1221 + 12321 + 123321 + 1234321 + 12344321 + ...., n items before programming the calculation formula and

N is input: 4
in the preceding calculation and of 4: 1 + 11 + 121 + 1221 = 1354
so the output is: 1354

Enter a floating point type float, the float occupies 4 bytes respectively, according to the order from high byte to low byte of the output manner% 5d in the order of integer output, output.

Input
2.3
Output
   64   19   51   51

Input 10 2 positive integer, output in V-shape

Snipaste_2019-12-22_18_33_44

Descending the left data and right data from small to large;

Left data maximum less the minimum value right-side data;

Each data in % 3d output form.

Guess you like

Origin www.cnblogs.com/Delta2019/p/12089778.html