Introduction training for beginners of Niuke network programming (C language)

 

  Display numbers in decimal, using% d; Display digits in octal, using% o; Display numbers in hexadecimal, using% X. In addition,%#o,% #x,% #x,% #x must be used to display the prefixes 0, 0x, and 0x of each hexadecimal number—— C premier plus 6th Edition

#include<stdio.h>

int main()
{
    printf("%#o %#0X", 1234, 1234);
    return 0;
}

Reverse output four digit

#include <stdio.h>

int main()
{
	int n = 0;
	scanf("%d", &n);
	while(n)
	{
		printf("%d", n % 10);
		n /= 10;
	}
	printf("\n");

	return 0;
}

 

There is a function in C language: getchar() -- get a character and enter a character.

C language also has a function: putchar() -- output a character and print a character.

About character @! ~, However, binary is stored in the computer, so these characters should be encoded, such as a: 65 and a: 97. These numbers are called ASCII codes.

#include <stdio.h>

int main()
{
	int ch = 0;
	ch = getchar();
	putchar(ch);
	return 0;
}

Here ch is an integer. When inputting A character, assign the ASCII code to ch, for example, A is 65, and then putchar will convert the ASCII code into the corresponding character output.

#include <stdio.h>

int main()
{
	int ch = 0;
	ch = getchar();
	putchar(ch+32);
	return 0;
}

A: 65 a: 97, the difference is 32, so the case conversion only needs to add 32

Features: only one character can be received!

#include <stdio.h>

int main()
{
	int ch = 0;
	while(ch = getchar())
	{
		putchar(ch + 32);
	}
	return 0;
}

  For example, if you enter A, A is in the input buffer, but it is not sent to getchar. You have to add A carriage return to give it, that is, A\n.

After a becomes a, because the loop sees \ n (10) from the input buffer again, plus 32 is' * '.

Because the while loop will always let you enter.

Is it uncomfortable.

#include <stdio.h>

int main()
{
	int ch = 0;
	while(ch = getchar())
	{
		putchar(ch + 32);
		getchar();
	}
	return 0;
}

  Just add another getchar() to eat \ n!

However, this loop will keep you typing, which is a bottomless hole.

#include <stdio.h>

int main()
{
	int ch = 0;
	while((ch = getchar()) != EOF)
	{
		putchar(ch + 32);
		getchar();
	}
	return 0;
}

The getchar function returns EOF (End of file) when the file is read incorrectly or at the end of the file, which is usually placed at the end of the file.

To stop, just ctrl+z.

#include <stdio.h>

int main()
{
    int ch = 0;
    while((ch = getchar()) != EOF)
    {
        printf("%c\n", ch+32);
        getchar();
    }
    return 0;
}

 

%The effect of 15d is to stand against the wall on the right.

#include <stdio.h>
int main()
{
	printf("%#X\n", 0xABCDEF);
    return 0;
}

  Hexadecimal is% X or% x, # used to display leading 0x or 0x

The printf function returns the number of characters printed on the screen.

#include <stdio.h>

int main()
{
	int ret = printf("hehe");
	printf("%d\n", ret);
	return 0;
}

 

#include <stdio.h>

int main()
{
    printf("\n%d", printf("Hello world!"));
    return 0;
}

Call the first printf1 function first, and its value will be the next printf2. Printf2 prints Hello world!, The return value is the number of printed characters, and then print printf1 on a new line.

 

         It's better to use double if there are decimals. Although there are floats, floating-point numbers are of double type by default, and the value range of double type is much larger than that of float. Double is recommended.

         When programming in C language, when the constant is written as 0, it is an integer constant int by default. When writing 0.0, it is a double precision floating-point constant by default.

        % f is data of single precision type (4 bytes), while%lf is data of double precision type (8 bytes). Out of range will be rounded.

        %. 2f,%.2lf stands for 2 decimal places after the decimal point.

#include <stdio.h>

int main()
{
    int id = 0;
    double c_score = 0.0;
    double math_score = 0.0;
    double eng_score = 0.0;
    scanf("%d;%lf,%lf,%lf", &id, &c_score, &math_score, &eng_score);
    printf("The each subject score of  No. %d is %.2lf, %.2lf, %.2lf.", id, c_score, math_score, eng_score);
    return 0;
}

Input 17140216; 80.845,90.55,100.00

  double type and float type will lose precision during storage, so the rounding answer is wrong.

Then why is there a loss of accuracy?

         It is because we input it in decimal, but we need to convert it into binary when calculating inside the computer.

reference resources: How to understand the problem of double precision loss- Know

For example, convert 0.1 to binary, 0.1 = 1 / 10. Then the binary is 0.1 = 1 / 1010 = 0.0001100110011... And put it into the double type.

  • Half precision (16bit): 11 significant digits
  • Single precision (32bit): 24 significant digits
  • Double precision (64bit): 53 significant digits
  • Four precision (128bit): 113 significant digits

//I can't count the significant figures. Say what you see!

The double precision significant number is 53 bits, so what about the last number? If the part to be processed reaches half of the previous one, carry it when it reaches it, and discard it if it does not reach it. That is, 0 is rounded to 1.

Then convert the binary number to decimal, and there will be error in decimal.

//Abandoned a few days, did not learn at all, hate! reflect! Adjust the state immediately.

 

#include <stdio.h>

int main()
{
    int id = 0;
    float c_score = 0.0;
    float math_score = 0.0;
    float eng_score = 0.0;
    scanf("%d;%f,%f,%f", &id, &c_score, &math_score, &eng_score);
    printf("The each subject score of  No. %d is %.2f, %.2f, %.2f.", id, c_score, math_score, eng_score);
    return 0;
}

#include <stdio.h>

int main()
{
    int ch = 0;
    while((ch = getchar()) != EOF)
    {
        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
        getchar();
    }
    
    return 0;
}

1) The first is to judge the case. The letter 'a' enclosed in single quotation marks is its ASCII value. So ch of type int can be judged with it. Those between 'a' and 'Z' are lowercase or those between 'a' and 'Z' are uppercase. Be sure to write or.

Or use the isalpha function to determine whether a character is a letter, whether it returns true or not, and whether it is 0

To add the header file #include < ctype. H >

2) The getchar function can only get one character at a time. When you enter H, there will be two characters, namely H and \ n, so you should add a getchar() below to eat the newline. Be sure to pay attention! Don't forget the invisible, lonely line feed.

3) The return value of getchar function is integer!

 

#include <stdio.h>

int main()
{
    char ch = 0;
    int i = 0;
    ch = getchar();
    for(i=0; i<5; i++)
    {
        int j = 0;
        for(j=0; j<4-i; j++)
        {
            printf(" ");
        }
        for(j=0; j<=i; j++)
        {
            printf("%c ", ch);
        }
        printf("\n");
    }
    return 0;
}

  Deal with it line by line to make the problem simple.

C language stipulates that variables are defined in the front of the current code block.

The code block is the position of braces.

#include <stdio.h>

int main()
{
	char arr[] = {73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33};
	int sz = sizeof(arr) / sizeof(arr[0]);
	int i = 0;
	for(i=0; i<sz; i++)
	{
		printf("%c", arr[i]);
	}
    return 0;
}

  Put it into the one-dimensional array of char type for easy processing. Pay attention to the calculation of length. There is no '\ 0' in the one-dimensional array, but only the string. Therefore, calculate as usual. sizeof(arr) is 1 * number / 1 = number.

#include <stdio.h>

int main()
{
    int year = 0;
    int month = 0;
    int date = 0;
    scanf("%4d%2d%2d", &year, &month, &date);
    printf("year=%4d\nmonth=%02d\ndate=%02d\n", year, month, date);
}

The input needs to specify the field width, so the size of the number of columns should be given. Pay attention to supplement 0 when outputting%0#d, and supplement 0 when the number of # bits is insufficient

#include <stdio.h>

int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        printf("%d\n", 1<<n);
    }
    return 0;
}

 

#include <stdio.h>

int main()
{
    int a = 0;
    int b = 0;
    scanf("a=%d,b=%d", &a, &b);
    printf("a=%d,b=%d\n", b, a);
    return 0;
}

#include <stdio.h>

int main()
{
    char ch = 0;
    scanf("%c", &ch);
    printf("%d\n", ch);
    return 0;
}

Note: there are two types of character input

1)scanf("%c", &ch);,, Don't forget that the front is% C

2)ch = getchar();

#include <stdio.h>

int main()
{
    int weight = 0;
    int height = 0;
    scanf("%d %d", &weight, &height);
    printf("%.2lf\n", weight / ((height / 100.0) * (height / 100.0)));
    return 0;
}

Note how BMI is calculated: weight in kilograms divided by height in meters squared. The input units are kilograms and centimeters, so the latter needs to be converted and calculated accurately, so it is height / 100.0 = 1.7, and then its square.

Finally, don't forget to keep two decimal places,%. 2lf

  The requirements of the topic must be carefully read!

#include <stdio.h>
#include <math.h>

int main()
{
    double a = 0.0;
    double b = 0.0;
    double c = 0.0;
    double circumference = 0.0;
    double area = 0.0;
    scanf("%lf %lf %lf", &a, &b, &c);
    circumference = a+b+c;
    double p = circumference / 2;
    area = sqrt(p*(p-a)*(p-b)*(p-c));
    printf("circumference=%.2lf area=%.2lf\n", circumference, area);
    return 0;
}

The output is floating-point, so the side length should be set as floating-point and initialized to 0.0.

Be sure to keep the correct rate again. If you use sqrt, it depends on whether there is a header file.

%. 2lf is 2 digits after the decimal point, and the default is 6 digits after the decimal point.

 

#include <stdio.h>
#define PI 3.1415926

int main()
{
    double r = 0.0;
    scanf("%lf", &r);
    double v = 0.0;
    v = 4.0/3*PI*r*r*r;
    printf("%.3lf\n", v);
    return 0;
}

  Note: both sides of 4 / 3 are int type, so the value is 1, which is not accurate, so it will become 4.0/3

  pow function can also be used to power

#include <stdio.h>
#include <math.h>

int main()
{
	int num = (int)pow(2, 6);
	printf("num = %d\n", num);
	return 0;
}

The return value of pow is of double type. Cast it.

 

#include <stdio.h>

int main()
{
    int score1 = 0;
    int score2 = 0;
    int score3 = 0;
    scanf("%d %d %d", &score1, &score2, &score3);
    printf("score1=%d,score2=%d,score3=%d\n", score1, score2, score3);
    return 0;
}

If there are multiple sets of input, how can it be easier?

#include <stdio.h>

int main()
{
    int score[3] = {0};
    int i = 0;
    for(i=0; i<3; i++)
    {
        scanf("%d", &score[i]);
    }
    printf("score1=%d,score2=%d,score3=%d\n", score[0], score[1], score[2]);
    return 0;
}

  The array is indexed from 0.

#include <stdio.h>

int main()
{
    int i = 0;
    for(i=10000; i<=99999; i++)
    {
        int j = 0;
        int sum = 0;
        for(j=10; j<=10000; j*=10)
        {
            sum += (i%j) * (i/j);
        }
        if(sum == i)
        {
            printf("%d ", i);
        }
    }
    return 0;
}

  Note: sum += (i%j) * (i/j);

i% and i / are variable values, not 10 and other fixed values.

#include <stdio.h>

int main()
{
    unsigned a = 0;
    unsigned b = 0;
    scanf("%d %d", &a, &b);
    int sum = (a%100 + b%100) % 100;
    printf("%d\n",sum);
    return 0;
}

  Only the last two digits of the number are reserved, then a and B are directly a% 100 and B% 100. If the result is greater than or equal to 100, add it and then% 100. No judgment is required. It is also right to write (a+b)%100.

#include <stdio.h>

int main()
{
    double f = 0.0;
    scanf("%lf", &f);
    printf("%d\n", (int)f%10);
    return 0;
}
#include <stdio.h>

int main()
{
    int d = 0;
    scanf("%d", &d);
    printf("%d\n", d%10);
    return 0;
}

#include <stdio.h>

int main()
{
    int age = 0;
    scanf("%d", &age);
    long seconds = 3.156e7 * age;
    printf("%ld\n", seconds);
    return 0;
}

3.156e7 is a large integer, which should be of type long and then become% ld.

e is the abbreviation of exponent, that is, how many times of 10.

long long type is% lld

 

#include <stdio.h>

int main()
{
    int seconds = 0;
    int h = 0;
    int m = 0;
    int s = 0;
    scanf("%d", &seconds);
    h = seconds / 60 / 60;
    m = seconds / 60 % 60;
    s = seconds % 60;
    printf("%d %d %d\n", h, m, s);
    return 0;
}

 

#include <stdio.h>

int main()
{
    int i = 0;
    double score = 0.0;
    double sum = 0.0;
    for(i=0; i<3; i++)
    {
        scanf("%lf", &score);
        sum += score;
    }
    printf("%.2lf %.2lf\n", sum, sum/3);
}

Don't forget that the double type is% lf, and the decimal point is required.

 

#include <stdio.h>

int main()
{
    int n = 0;
    int h = 0;
    int m = 0;
    while(scanf("%d %d %d", &n, &h, &m) != EOF)
    {
        if(m % h)
        {
            printf("%d\n", n-m/h-1);
        }
        else
        {
            printf("%d\n", n-m/h);
        }
    }
    return 0;
}

Assuming that only one minute is given and there are 8 bottles in total, one bottle has been opened in one minute, so there are 7 bottles left.

In other words, it may be on the way to drink.

 

reference resources: Interesting question, why do hexadecimal numbers in C begin with the prefix 0x?

        0 is to tell the compiler that it is a number, and then 0 has no effect in the front.

         The hexadecimal prefix 0x or 0 in 0x is used to tell the compiler that this is a number, which has a little historical origin and some benefits. X can be regarded as an arbitrary symbol.  

#include <stdio.h>

int main()
{
    int a = 0;
    int b = 0;
    scanf("%x %o", &a, &b);
    printf("%d\n", a+b);
    return 0;
}

 % x hexadecimal data control format

%o octal data control format

#include <stdio.h>

int main()
{
    double money = 0.0;
    int month = 0;
    int day = 0;
    int flag = 0;
    scanf("%lf %d %d %d", &money, &month, &day, &flag);
    if(month == 11 && day == 11)
    {
        money *= 0.7;
    }
    else if(month == 12 && day == 12)
    {
        money *= 0.8;
    }
    money -= flag*50.0;
    if(money < 0.0)
    {
        money = 0.0;
    }
    printf("%.2lf\n", money);
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int arr[40] = {0};
    int i = 0;
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for(i=0; i<n-1; i++)
    {
        int j = 0;
        for(j=0; j<n-1-i; j++)
        {
            if(arr[j] > arr[j+1])
            {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    for(i=n-1; i>=n-5; i--)
    {
        printf("%d ", arr[i]);
    }
    
    return 0;
}

  Second, you can use the library function qsort(quick sort) of C language

#include <stdio.h>
#include <stdlib.h>

int cmp_int(const void* e1, const void* e2)
{
    return *(int*)e1 - *(int*)e2;
}

int main()
{
    int n = 0;
    scanf("%d", &n);
    int arr[40] = {0};
    int i = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    qsort(arr, n, sizeof(arr[0]), cmp_int);
    for(i=n-1; i>=n-5; i--)
    {
        printf("%d ", arr[i]);
    }
    
    return 0;
}

Note: 1) the function definition must write the return value int.

2) As for why const should be used for null pointers, one is to prevent you from mistakenly modifying the value of objects that cannot be modified, and the other is to determine which objects cannot be modified when reusing code.

3) Functions are in ascending order by default, and descending order is e2 first.

  About the function gets (string), s in is the initial letter of string, which means string, that is, enter a string of characters.

#include <stdio.h>

int main()
{
    char buf[100] = {0};
    gets(buf);
    int i = 0;
    int count_a = 0;
    int count_b = 0;
    while(buf[i] != '0')
    {
        if(buf[i] == 'A')
        {
            count_a++;
        }
        else if(buf[i] == 'B')
        {
            count_b++;
        }
        i++;
    }
    if(count_a > count_b)
    {
        putchar('A');
    }
    else if(count_a < count_b)
    {
        putchar('B');
    }
    else
    {
        putchar('E');
    }
    return 0;
}

#include <stdio.h>

int main()
{
    char buf[100] = {0};
    gets(buf);
    int i = 0;
    int flag = 0;
    while(buf[i] != '0')
    {
        if(buf[i] == 'A')
        {
            flag++;
        }
        else if(buf[i] == 'B')
        {
            flag--;
        }
        i++;
    }
    if(flag > 0)
    {
        putchar('A');
    }
    else if(flag < 0)
    {
        putchar('B');
    }
    else
    {
        putchar('E');
    }
    return 0;
}

Count with a variable.

#include <stdio.h>

int main()
{
    int ch = 0;
    int flag = 0;
    while(((ch = getchar()) != '0') && ch != EOF)
    {
        if(ch == 'A')
        {
            flag++;
        }
        else if(ch == 'B')
        {
            flag--;
        }
    }
    if(flag > 0)
    {
        printf("A\n");
    }
    else if(flag < 0)
    {
        printf("B\n");
    }
    else
    {
        printf("E\n");
    }
    return 0;
}

  getchar() is read one character at a time, so add= EOF, to avoid dead circulation.

#include <stdio.h>

int main()
{
    int num = 0;
    while(~scanf("%d", &num))
    {
        if(num >= 140)
        {
            printf("Genius\n");
        }
    }
    return 0;
}

 ~ Is a bitwise negation operator.

When an error occurs in the scanf function, it returns - 1

Integers store complements in memory.

Reason 1) the representations of + 0 and - 0 are different in the original code and inverse code.

2) Complement can turn subtraction into addition without additional hardware circuit.

#include <stdio.h>

int main()
{
    int s1 = 0;
    int s2 = 0;
    int s3 = 0;
    while(scanf("%d %d %d", &s1, &s2, &s3) != EOF)
    {
        int max = s1 > s2 ? s1 : s2;
        max = max > s3 ? max : s3;
        printf("%d\n", max);
    }
    return 0;
}

  s1 and s2 are compared first to obtain a larger value, and then compared with s3. If it is larger than s3, it is the maximum value, otherwise s3 is the maximum value.

#include <stdio.h>

int main()
{
    int num[3] = {0};
    while(scanf("%d%d%d", &num[0], &num[1], &num[2]) != EOF)
    {
        int max = num[0];
        int i = 0;
        for(i=1; i<3; i++)
        {
            if(num[i] > max)
            {
                max = num[i];
            }
        }
        printf("%d\n", max);
    }
    return 0;
}

  This way of writing is equivalent to playing a challenge arena. Suppose 0 is Bruce Lee and the others are students in the class, then one student can't beat him, so choose a student with student number 1 first.

#include <stdio.h>

int main()
{
    int ch = 0;
    while((ch = getchar()) != EOF)
    {
        if(ch=='A' || ch=='a' || ch=='E' || ch=='e' || ch=='I' || ch=='i' || ch=='O' || ch=='o' || ch=='U' || ch=='u') 
        {
            printf("Vowel\n");
        }
        else
            printf("Consonant\n");
        getchar();
    }
    return 0;
}

reference resources: The return type of C language is int. why is the return type of getchar() in C language int_ Youlink L's blog - CSDN blog

What is the EOF value_ Baidu Knows

About ((CH = getchar())! = EOF), the problem that ch is declared as an integer_ talent_CYJ-CSDN blog

Note 1) the range of values that can be represented by various data types is determined by the compiler. Char type is defined in the range of 0 ~ 255 in some compilers and - 128 ~ 127 in others. When the range defined in the compiler is 0 ~ 255, an error will occur when receiving the return value of getchar() with char.

         After changing int to char, it can be compiled normally on my computer. Because the range defined in my compiler is - 128 ~ 127, it will be implicitly converted to char type when receiving with char.

         Even if the compiler always defines the range of - 128 ~ 127, the program may make errors. Although common characters end at 127 bits. However, the number assigned in the ASCII table is 256. 128 ~ 256 are extended characters, such as commonly used Euro symbols.

         To sum up: the contents returned by getchar() can be received in int type with a wider range, so as to make the program more robust.

2) For EOF, EOF is used as the end of file flag in the while loop. The range of ASCII code value is 0 ~ 255, and - 1 cannot appear, so EOF can be used as the end of file flag.  

3)While ((CH = getchar())! = EOF)_ A312568 blog - CSDN blog

4) About why (ch = getchar())= EOF, because if ch = getchar()= EOF, because= If the priority of is higher than =, judge getchar ()= EOF, return 0 or 1, and assign it to Ch.

#include <stdio.h>

int main()
{
    int ch = 0;
    char vowel[] = "AaEeIiOoUu";
    while((ch = getchar()) != EOF)
    {
        int i = 0;
        for(i=0; i<10; i++)
        {
            if(vowel[i] == ch)
            {
                printf("Vowel\n");
                break;
            }
        }
        if(i == 10)
        {
            printf("Consonant\n");
        }
        getchar();
    }
    return 0;
}

         Since getchar() is a buffered input, it will not be printed until you press Enter. After you press Enter, '\ 0' will be stored in the buffer and wait for the next output. Therefore, we need to write another getchar() to read it unnecessarily.

        You can also use the library functions strchr, cat - > coalition. chr is not a word, but a combination of search char, as if wintel is a combination of windows and intel.

        Strchr (string, character), including the header file string.h

        strchr is used to determine whether there is this character in the string. If there is an address returned, NULL will be returned.

#include <stdio.h>
#include <string.h>

int main()
{
    int ch = 0;
    char vowel[] = "AaEeIiOoUu";
    while((ch = getchar()) != EOF)
    {
        if(strchr(vowel, ch))
        {
            printf("Vowel\n");
        }
        else
        {
            printf("Consonant\n");
        }
        getchar();
    }
    return 0;
}
#include <stdio.h>
#include <string.h>

int main()
{
    int ch = 0;
    char vowel[] = "AaEeIiOoUu";
    while(scanf("%c", &ch) != EOF)
    {
        if(strchr(vowel, ch))
        {
            printf("Vowel\n");
        }
        else
        {
            printf("Consonant\n");
        }
        getchar();
    }
    return 0;
}
#include <stdio.h>
#include <string.h>

int main()
{
    int ch = 0;
    char vowel[] = "AaEeIiOoUu";
    while(scanf(" %c", &ch) != EOF)
    {
        if(strchr(vowel, ch))
        {
            printf("Vowel\n");
        }
        else
        {
            printf("Consonant\n");
        }
    }
    return 0;
}

scanf("space% c", & CH) adding a space before% c will digest all the previous invisible characters and read one character.

#include <stdio.h>
#include <string.h>

int main()
{
    int ch = 0;
    char vowel[] = "AaEeIiOoUu";
    while(scanf("%c\n", &ch) != EOF)
    {
        if(strchr(vowel, ch))
        {
            printf("Vowel\n");
        }
        else
        {
            printf("Consonant\n");
        }
    }
    return 0;
}

Putting a \ n after% c will digest this \ n character.

#include <stdio.h>

int main()
{
    int ch = 0;
    while(scanf(" %c", &ch) != EOF)
    {
        if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
        {
            printf("%c is an alphabet.\n", ch);
        }
        else
        {
            printf("%c is not an alphabet.\n", ch);
        }
    }
    return 0;
}

Note: the printf function is written as% c, and the corresponding contents will be corrected later. Very silly error 0.0

#include <stdio.h>
#include <ctype.h>

int main()
{
    int ch = 0;
    while(scanf(" %c", &ch) != EOF)
    {
        if(isalpha(ch))
        {
            printf("%c is an alphabet.\n", ch);
        }
        else
        {
            printf("%c is not an alphabet.\n", ch);
        }
    }
    return 0;
}

You can also use the function isalpha (alphabet) to include the header file ctype.h, which means type in C language.

 

#include <stdio.h>
#include <ctype.h>

int main()
{
    int ch = 0;
    while(scanf("%c\n", &ch) != EOF)
    {
        if(isupper(ch))
        {
            printf("%c\n", ch+32);
        }
        else if(islower(ch))
        {
            printf("%c\n", ch-32);
        }
    }
    return 0;
}

isupper determines whether it is an uppercase letter, while islower determines whether it is a lowercase letter.

#include <stdio.h>
#include <ctype.h>

int main()
{
    int ch = 0;
    while(scanf("%c\n", &ch) != EOF)
    {
        if(isupper(ch))
        {
            printf("%c\n", tolower(ch));
        }
        else if(islower(ch))
        {
            printf("%c\n", toupper(ch));
        }
    }
    return 0;
}

You can also use tower () and toupper() to convert case~

Don't forget to include the header file ctype.h!

Be careful, huh.  

#include <stdio.h>

int main()
{
    int t = 0;
    while(scanf("%d", &t) != EOF)
    {
        if(t > 0)
        {
            printf("1\n");
        }
        else if(t == 0)
        {
            printf("0.5\n");
        }
        else
        {
            printf("0\n");
        }
    }

    return 0;
}

  This does not need to handle line feed, because scanf is in the form of% d, so the character type will not be read.

 

#include <stdio.h>

int main()
{
    int weight = 0;
    int height = 0;
    while(~scanf("%d %d", &weight, &height))
    {
        double BMI = weight / ((height/100.0)*(height/100.0));
        if(BMI < 18.5)
        {
            printf("Underweight\n");
        }
        else if(BMI <= 23.9)
        {
            printf("Normal\n");
        }
        else if(BMI <= 27.9)
        {
            printf("Overweight\n");
        }
        else
        {
            printf("Obese\n");
        }
    }
    return 0;
}

Note: precision will be lost when height/100, so it will become floating point - > height/100.0

 

The sign of this triangle is discriminant;

#include <stdio.h>
#include <math.h>

int main()
{
    double a = 0.0;
    double b = 0.0;
    double c = 0.0;
    while(~scanf("%lf %lf %lf", &a, &b, &c))
    {
        if(a == 0.0)
            printf("Not quadratic equation\n");
        else
        {
            double d = b*b - 4*a*c;
            if(d == 0.0)
            {
                printf("x1=x2=%.2lf\n", -b/(2*a));
            }
            else if(d>0.0)
            {
                printf("x1=%.2lf;x2=%.2lf\n", (-b-sqrt(d))/(2*a), (-b+sqrt(d))/(2*a));
            }
            else
            {
                printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", -b/(2*a), sqrt(-d)/(2*a), -b/(2*a), sqrt(-d)/(2*a));
            }
        }
    }
    
    return 0;
}

The reason why double and 0.0 are not 0 is that 0.0 is double and 0 is int.

 

  My code

#include <stdio.h>

int main()
{
    int year = 0;
    int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31,30,31};
    int mon = 0;
    while(~scanf("%d %d", &year, &mon))
    {
        if(((year%400==0) || (year%4==0&&year%100!=0)) && mon==2)
            printf("%d\n", month[2]+1);
        else
            printf("%d\n", month[mon]);
    }
    return 0;
}

#include <stdio.h>

int main()
{
    double n1 = 0.0;
    double n2 = 0.0;
    char ch = 0;
    while(~scanf("%lf %c %lf", &n1, &ch, &n2))
    {
        if(ch == '+')
        {
            printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1+n2);
        }
        else if(ch == '-')
        {
            printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1-n2);
        }
        else if(ch == '*')
        {
            printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1*n2);
        }
        else if(ch == '/')
        {
            if(n2 == 0.0)
            {
                printf("Wrong!Division by zero!\n");
            }
            else
            {
                printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1/n2);
            }
        }
        else
        {
            printf("Invalid operation!\n");
        }
    }
    return 0;
}
#include <stdio.h>

int main()
{
    double n1 = 0.0;
    double n2 = 0.0;
    char ch = 0;
    while(~scanf("%lf %c %lf", &n1, &ch, &n2))
    {
        switch(ch)
        {
            case '+':
                printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1+n2);
            break;
            case '-':
                printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1-n2);
            break;
            case '*':
                printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1*n2);
            break;
            case '/':
                if(n2 == 0.0)
                    printf("Wrong!Division by zero!\n");
                else
                    printf("%.4lf%c%.4lf=%.4lf\n", n1, ch, n2, n1/n2);
            break;
            default:
                printf("Invalid operation!\n");
            break;
        }
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    while(~scanf("%d", &n))
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                if(i+j<n-1)
                    printf("  ");
                else
                    printf("* ");
            }
            printf("\n");
        }
    }
    return 0;
}

You can find rules!

 

#include <stdio.h>

int main()
{
    int n = 0;
    while(~scanf("%d", &n))
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<i; j++)
            {
                printf(" ");
            }
            for(j=0; j<n-i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    return 0;
}

Note: the number of * will change, so it is n-i, not 5-i.

 

 #include <stdio.h>

int main()
{
    int n = 0;
    while(~scanf("%d", &n))
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                if(i == j)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

Diagonal solution

  my

#include <stdio.h>

int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            if(i==0 || i==n-1)
            {
                for(j=0; j<n; j++)
                {
                    printf("* ");
                }
            }
            else
            {
                for(j=0; j<n; j++)
                {
                    if(j==0 || j==n-1)
                    {
                        printf("* ");
                    }
                    else
                    {
                        printf("  ");
                    }
                }
            }
            printf("\n");
        }
    }
    return 0;
}

simplify

#include <stdio.h>

int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                if(i==0 || i==n-1)
                {
                    printf("* ");
                }
                else if(j==0 || j==n-1)
                {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

if(i==0 || i==n-1)
            {
                for(j=0; j<n; j++)
                {
                    printf("* ");
                }
            }

i judge whether i is a qualified line before printing.  

  for(j=0; j<n; j++)
            {
                if(i==0 || i==n-1)
                {
                    printf("* ");
                }

           }

This is a line. No matter which line, judge again.

#include <stdio.h>

int main()
{
    int n = 0;
    while(~scanf("%d", &n))
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<=i; j++)
            {
                if(j==0 || i==n-1 || i==j)
                {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
            printf("\n");
        }
        
    }
    return 0;
}

J < = I, because it's the lower triangle.

#include <stdio.h>

int main()
{
    double arr[5] = {0.0};
    int i = 0;
    double sum = 0.0;
    for(i=0; i<5; i++)
    {
        scanf("%lf", &arr[i]);
        sum += arr[i];
    }
    printf("%.2lf\n", sum/5);
    return 0;
}

Note that it's an average, so sum is divided by 5

#include <stdio.h>

int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i = 0;
        for(i=1; i<=n; i++)
        {
            int j = 0;
            for(j=1; j<=i; j++)
            {
                printf("%d ", j);
            }
            printf("\n");
        }
    }
    return 0;
}

i can start from 1 without + 1

#include <stdio.h>

int main()
{
    int score = 0;
    int i = 0;
    int sum = 0;
    int max = 0;
    int min = 100;
    for(i=0; i<7; i++)
    {
        scanf("%d", &score);
        sum += score;
        if(score > max)
            max = score;
        if(score < min)
            min = score;
    }
    printf("%.2lf\n", (sum-max-min) / 5.0);
    return 0;
}

  First add without brain, then subtract max and min.

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int arr[51] = {0};
    int i = 0;
    int m = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    scanf("%d", &m);
    for(i=n-1; i>=0; i--)
    {
        if(m < arr[i])
        {
            arr[i+1] = arr[i];
        }
        else
        {
            arr[i+1] = m;
            break;
        }
    }
    if(i == -1)
    {
        arr[0] = m;
    }
    for(i=0; i<n+1; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

  First of all, pay attention to the order of input, which can not be added blindly.

for(i=n-1; i>=0; i--)
    {
        if(m < arr[i])
        {
            arr[i+1] = arr[i];
        }
        else
        {
            arr[i+1] = m;
            break;
        }
    }
    if(i == -1)
    {
        arr[0] = m;
    }

  Consider boundary values.

 

#include <stdio.h>

int main()
{
    int n = 0;
    int arr[101] = {0};
    while(~scanf("%d", &n))
    {
        int i = 0;
        for(i=2; i<=n; i++)
        {
            arr[i] = i;
        }
        for(i=2; i<=n; i++)
        {
            int j = 0;
            for(j=i+1; j<=n; j++)
            {
                if(arr[j] % i == 0)
                {
                    arr[j] = 0;
                }
            }
        }
        int count = 0;
        for(i=2; i<=n; i++)
        {
            if(arr[i] != 0)
                printf("%d ", arr[i]);
            else
                count++;
        }
        printf("\n%d\n", count);
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int arr[101] = {0};
    while(~scanf("%d", &n))
    {
        int i = 0;
        for(i=2; i<=n; i++)
        {
            arr[i] = i;
        }
        int count = 0;
        for(i=2; i<=n; i++)
        {
            int j = 0;
            for(j=2; j<i; j++)
            {
                if(i % j == 0)
                {
                    count++;
                    break;
                }
            }
            if(j == i)
                printf("%d ", i);
        }
        
        printf("\n%d\n", count);
    }
    return 0;
}

Trial division  

 #include <stdio.h>

int main()
{
    int m = 0;
    int n = 0;
    int i = 0;
    scanf("%d %d", &m, &n);
    int arr1[100][100];
    int arr2[100][100];
    int j = 0;
    int count = 0;
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d", &arr1[i][j]);
        }
    }
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d", &arr2[i][j]);
        }
    }
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            if(arr1[i][j] == arr2[i][j])
            {
                count++;
            }
        }
    }
    printf("%.2f\n", 100.0*count/(m*n));
    return 0;
}

Note the percentage.

#include <stdio.h>

int main()
{
    char name[100] = {0};
    char pass[100] = {0};
    while(scanf("%s %s", &name, &pass) != EOF)
    {
        if(strcmp(name, "admin") == 0 && strcmp(pass, "admin") == 0)
        {
            printf("Login Success!\n");
        }
        else
        {
            printf("Login Fail!\n");
        }
    }
    return 0;
}

Use string, use strcmp for comparison, and include the header file string.h

#include <stdio.h>

int main()
{
    int i = 0;
    int count = 0;
    for(i=1; i<=2019; i++)
    {
        int t = i;
        while(t)
        {
            if(t%10 == 9)
            {
                count++;
                break;
            }
            t /= 10;
        }
    }
    printf("%d\n", count);
    return 0;
}

t should be used to temporarily store i to avoid changing the value of i. when t%10, the value of t will not be changed, and t/=10 will lose the last bit, so as to realize bit-by-bit comparison.

#include <stdio.h>

int main()
{
    int N = 0;
    scanf("%d", &N);
    int i = 0;
    int odd = 0;
    for(i=1; i<=N; i++)
    {
        if(i % 2)
        {
            odd++;
        }
    }
    printf("%d %d\n", odd, N-odd);
    return 0;
}

  Odd number is odd in English. There are N numbers in total. If it is not odd, it is even. The number of odd numbers is obtained. The number of N-odd numbers is even.

#include <stdio.h>

int main()
{
    int N = 0;
    scanf("%d", &N);
    if(N % 2)
    {
        printf("%d %d\n", N/2+1, N/2);
    }
    else
    {
        printf("%d %d\n", N/2, N/2);
    }
    return 0;
}

When there are only even numbers, odd numbers and even numbers are equal. Otherwise, there is one more odd number.

 

#include <stdio.h>

int main()
{
    int num = 0;
    int i = 0;
    int positive = 0;
    for(i=0; i<10; i++)
    {
        scanf("%d", &num);
        if(num > 0)
            positive++;
    }
    printf("positive:%d\nnegative:%d\n", positive, 10-positive);
    return 0;
}

Note that positive numbers are > 0, and 0 is not a positive number.

#include <stdio.h>

int main()
{
    int N = 0;
    scanf("%d", &N);
    int arr[50] = {0};
    int i = 0;
    int flag1 = 0;
    int flag2 = 0;
    for(i=0; i<N; i++)
    {
        scanf("%d", &arr[i]);
        if(i > 0)
        {
            if(arr[i] > arr[i-1])
            {
                flag1 = 1;
            }
            else
            {
                flag2 = 1;
            }
        }
    }
    if(flag1 + flag2 == 1)
    {
        printf("sorted\n");
    }
    else
    {
        printf("unsorted\n");
    }
    
    return 0;
}

Use two markers.

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int arr[50] = {0};
    int i = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    int delete = 0;
    scanf("%d", &delete);
    int j = 0;
    for(i=0; i<n; i++)
    {
        if(arr[i] != delete)
        {
            arr[j++] = arr[i];
        }
    }
    for(i=0; i<j; i++)
        printf("%d ", arr[i]);
}

  i   Traverse the entire array. If it is not the number to be deleted, add the arr array again. If it is deleted, do not put it in.

 

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int arr[1000] = {0};
    int i = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for(i=0; i<n; i++)
    {
        int j = 0;
        for(j=i+1; j<n; j++)
        {
            if(arr[i] == arr[j])
            {
                int k = 0;
                for(k=j; k<n-1; k++)
                {
                    arr[k] = arr[k+1];
                }
                n--;
                j--;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int arr1[100] = {0};
    int arr2[100] = {0};
    int arr3[200] = {0};
    int i = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr1[i]);
    }
    for(i=0; i<m; i++)
    {
        scanf("%d", &arr2[i]);
    }
    i = 0;
    int j = 0;
    int k = 0;
    while(i<n && j<m)
    {
        if(arr1[i] < arr2[j])
        {
            arr3[k++] = arr1[i++];
        }
        else
        {
            arr3[k++] = arr2[j++];
        }
    }
    if(i == n)
    {
        for(; j<m; j++)
        {
            arr3[k++] = arr2[j];
        }
    }
    else
    {
        for(; i<n; i++)
        {
            arr3[k++] = arr1[i];
        }
    }
    for(i=0; i<k; i++)
    {
        printf("%d ", arr3[i]);
    }
    return 0;
}

Note: 1) number of array inputs

2) Remember to give 0 again when using i, otherwise the value is m

3) The while loop should ensure that two arrays do not cross the boundary. It will exit only when one or two arrays read the end

4) Both arrays are in ascending order, so compare the first element of the array to know who is the smallest element

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int arr[10][10] = {0};
    int i = 0;
    int j = 0;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int arr[10][10] = {0};
    int i = 0;
    int j = 0;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &arr[i][j]);
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}
#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int i = 0;
    int num = 0;
    for(i=1; i<=n*m; i++)
    {
        scanf("%d", &num);
        printf("%d ", num);
        if(i % m == 0)
        {
            printf("\n");
        }
    }
    
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int i = 0;
    int j = 0;
    int num = 0;
    int max = 0;
    int x = 0;
    int y = 0;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &num);
            if(num > max)
            {
                max = num;
                x = i;
                y = j;
            }
        }
    }
    printf("%d %d\n", x+1, y+1);
    return 0;
}

 

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int i = 0;
    int j = 0;
    int arr1[9][9] = {0};
    int arr2[9][9] = {0};
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &arr1[i][j]);
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &arr2[i][j]);
        }
    }
    int flag = 0;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(arr1[i][j] != arr2[i][j])
            {
                printf("No\n");
                return 0;
            }
        }
    }
    printf("Yes\n");
    return 0;
}

printf("No\n");
                return 0;

The return 0 tabular main function has been executed. No more statements will be executed.

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int j = 0;
    int arr[10][10] = {0};
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            if(i > j)
            {
                if(arr[i][j] != 0)
                {
                    printf("NO\n");
                    return 0;
                }
            }
        }
    }
    printf("YES\n");
    return 0;
}

  I > J is observed at 0.

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int j = 0;
    int arr[10][10] = {0};
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d", &arr[i][j]);
            if(i > j)
            {
                if(arr[i][j] != 0)
                {
                    printf("NO\n");
                    return 0;
                }
            }
        }
    }
    printf("YES\n");
    return 0;
}

simplify

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int i = 0;
    int j = 0;
    int arr[10][10] = {0};
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            printf("%d ", arr[j][i]);
        }
        printf("\n");
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int i = 0;
    int j = 0;
    int arr[10][10] = {0};
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    int k = 0;
    scanf("%d", &k);
    char t = 0;
    int a = 0;
    int b = 0;
    for(i=0; i<k; i++)
    {
        scanf(" %c %d %d", &t, &a, &b);
        if(t == 'r')
        {
            for(j=0; j<m; j++)
            {
                int tmp = arr[a-1][j];
                arr[a-1][j] = arr[b-1][j];
                arr[b-1][j] = tmp;
            }
        }
        else if(t == 'c')
        {
            for(j=0; j<n; j++)
            {
                int tmp = arr[j][a-1];
                arr[j][a-1] = arr[j][b-1];
                arr[j][b-1] = tmp;
            }
        }
        
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");    
    }
    
    return 0;
}

  Be careful not to read carriage return.

my

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int arr[30][30] = {0};
    for(i=0; i<n; i++)
    {
        arr[i][0] = 1;
        arr[i][i] = 1;
    }
    int j = 0;
    for(i=2; i<n; i++)
    {
        for(j=1; j<i; j++)
        {
            arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<=i; j++)
        {
            printf("%5d", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int arr[30][30] = {0};
    int j = 0;
    for(i=0; i<n; i++)
    {
        for(j=0; j<=i; j++)
        {
            if(j == 0 || i == j)
            {
                arr[i][j] = 1;
            }
            if(i>=2 && j>=1)
            {
                arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
            }
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<=i; j++)
        {
            printf("%5d", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

#include <stdio.h>

int main()
{
    char arr[3][3] = {0};
    int i = 0;
    int j = 0;
    char flag = 'O';
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf(" %c", &arr[i][j]);
        }
    }
    for(i=0; i<3; i++)
    {
        if(arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][0] != 'O')
        {
            flag = arr[i][0];
            break;
        }
        if(arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i] && arr[0][i] != 'O')
        {
            flag = arr[0][i];
            break;
        }
    }
    if(arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] != 'O')
    {
        flag = arr[2][2];
    }
    if(arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[0][2] != 'O')
    {
        flag = arr[0][2];
    }
    if(flag == 'K')
    {
        printf("KiKi wins!\n");
    }
    else if(flag == 'B')
    {
        printf("BoBo wins!\n");
    }
    else
    {
        printf("No winner!\n");
    }
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    int arr[40] = {0};
    scanf("%d", &n);
    int i = 0;
    while(n)
    {
        arr[i] = n % 6;
        i++;
        n /= 6;
    }
    for(i--; i>=0; i--)
    {
        printf("%d", arr[i]);
    }
    return 0;
}

 

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    long sum = 0;
    for(i=1; i<=n; i++)
    {
        sum += i;
    }
    printf("%ld\n", sum);
    return 0;
}

  The output format of long shaping is% ld, remember!

#include <stdio.h>

int main()
{
    long n = 0;
    scanf("%d", &n);
    int i = 0;
    long sum = 0;
    sum = n * (1 + n) / 2;
    printf("%ld\n", sum);
    return 0;
}

Using mathematical formulas can make the algorithm faster, but remember that when the value of n is of type int, it will overflow during calculation!  

#include <stdio.h>

int main()
{
    int h = 0;
    int m = 0;
    int k = 0;
    scanf("%d:%d %d", &h, &m, &k);
    h = (h+(m+k)/60) % 24;
    m = (m+k) % 60;
    printf("%02d:%02d\n", h, m);
    return 0;
}

  First add the original minutes and the minutes to sleep together, / 60 is the hours, and the rest is the minutes. Note that the original hours should also be added.

What should be added before the number of dissatisfied digits? Write it directly in d   In front of the.

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    printf("%d\n", n/12*4+2);
    return 0;
}

Note that it will take two minutes to go upstairs.

When it is less than 12, n/12=0, which is also satisfied without additional consideration.

#include <stdio.h>

int main()
{
    long long n = 0;
    long long m = 0;
    scanf("%lld %lld", &n, &m);
    long long tmp = 0;
    long long n2 = n;
    long long m2 = m;
    while(tmp = n % m)
    {
        n = m;
        m = tmp;
    }
    long long max = m;
    long long min = n2*m2 / max;
    printf("%lld\n", min+max);
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int sum = 0;
    while(n)
    {
        if(n%10%2)
        {
            sum += pow(10, i);
        }
        i++;
        n /= 10;
    }
    printf("%d\n", sum);
    return 0;
    
}

#include <stdio.h>

int f(int n)
{
    if(n<=2)
    {
        return n;
    }
    else
    {
        return f(n-1) + f(n-2);
    }
}
int main()
{
    int n = 0;
    scanf("%d", &n);
    int ret = f(n);
    printf("%d\n", ret);
    
    return 0;
}

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int arr[100001] = {0};
    int tmp = 0;
    for(i=0; i<n; i++)
    {
        scanf("%d", &tmp);
        arr[tmp] = tmp;
    }
    for(i=0; i<n; i++)
    {
        if(arr[i] != 0)
        {
            printf("%d ", arr[i]);
        }
    }
    return 0;
}

  Use the characteristics of array subscript ascending order, and you can also de duplicate!

#include <stdio.h>

int main()
{
    char arr[8000] = {0};
    scanf("%s", arr);
    char* p = arr;
    long long c = 0;
    long long ch = 0;
    long long chn = 0;
    while(*p)
    {
        if(*p == 'C')
        {
            c++;
        }
        else if(*p == 'H')
        {
            ch += c;
        }
        else if(*p == 'N')
        {
            chn += ch;
        }
        p++;
    }
    printf("%lld\n", chn);
    return 0;
}

Here, directly use the pointer to define the character pointer variable p, perform * dereference, and exit at \ 0.

 

#include <stdio.h>

int main()
{
    int arr[4] = {0};
    int i = 0;
    for(i=0; i<4; i++)
    {
        scanf("%d", &arr[i]);
    }
    int max = arr[0];
    for(i=1; i<4; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }
    }
    printf("%d\n", max);
    return 0;
}

Please note that you can write the challenge arena title directly in array!

#include <stdio.h>

int main()
{
    int score = 0;
    scanf("%d", &score);
    if(score >= 90 && score <= 100)
        printf("A\n");
    else if(score >= 80 && score <= 89)
        printf("B\n");
    else if(score >= 70 && score <= 79)
        printf("C\n");
    else if(score >= 60 && score <= 69)
        printf("D\n");
    else if(score <= 59 && score >= 0)
        printf("E\n");
    else
        printf("F\n");
    return 0;
}

#include <stdio.h>

int max3(int a, int b, int c)
{
    int max = a > b ? a : b;
    max = max > c ? max : c;
    return max;
}

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    scanf("%d %d %d", &a, &b, &c);
    float m = (float)max3(a+b, b, c) / (max3(a, b+c, c) + max3(a, b, b+c));
    printf("%.2f\n", m);
    
    return 0;
}

#include <stdio.h>

int main()
{
    printf("printf(\"Hello world!\\n\");\n");
    printf("cout << \"Hello world!\" << endl;\n");
    return 0;
}

Pay attention to the pairing of "", do not use \, and \ n use escape characters when there is no line drawing requirement.

 

#include <stdio.h>

int is_primer(int n)
{
    int i = 2;
    for(i=2; i<n; i++)
    {
        if(n % i == 0)
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    int i = 0;
    int count = 0;
    for(i=100; i<=999; i++)
    {
        if(is_primer(i))
        {
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}

  Violence solving  

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int arr[1000] = {0};
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    int j = 0;
    for(i=0; i<n-1; i++)
    {
        for(j=0; j<n-1-i; j++)
        {
            if(arr[j] > arr[j+1])
            {
                int tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp;
            }
        }
    }
    for(i=0; i<n-1; i++)
    {
        if(arr[i] == arr[i+1])
        {
            for(j=i; j<n-1; j++)
            {
                arr[j] = arr[j+1];
            }
            n--;
            i--;
        }
    }
    for(i=0; i<n; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

Ingenious method

#include <stdio.h>

int main()
{
    int n = 0;
    scanf("%d", &n);
    int num = 0;
    int i = 0;
    int arr[1001] = { 0 };
    for (i = 0; i < n; i++)
    {
        scanf("%d", &num);
        arr[num] = num;
    }
    for (i = 0; i <= 1000; i++)
    {
        if (arr[i] != 0)
        {
            printf("%d ", arr[i]);
        }
    }
    return 0;
}

Note that i should be < = 1000, and all elements in the array should be traversed. If it is n, the entered number is likely to be placed at the position with large subscript in the array.

 

 

Box 1 must have a number and the address of the next number, so it should be divided into two parts.

  Box 1 also needs to know the starting address!

  There are two parts, so use structure~

 

  Definition of linked list node

Note that I left a mistake "deliberately".

There is no semicolon when defining the structure!

I haven't learned data structure. Put it temporarily!

Tags: html css

Posted on Sun, 21 Nov 2021 05:16:54 -0500 by nonso