[Take you through the questions by hand]-Introduction to C language programming (1)

Hello everyone, I am deep fish~

Table of contents

Foreword:

Online OJ

<1>What is online OJ

<2>Why train online OJ

1. Real knowledge comes from practice

2. I am a big V

3. Tolerance is great

4. Shorten binary

5. Output a four-digit number in reverse

6. Case conversion

7. Small plane

8. Hexadecimal to decimal

9. The return value of printf

10. Input and output of basic student information

Conclusion: There is still glory in the other side, and young people are not afraid of the long years


Foreword:

This part is the initial part of the C language topic, a total of 130 questions, from easy to difficult, suitable for beginners to learn, non-beginners do not read the topic link: programming language beginners training camp Kewang (nowcoder.com), let’s rush together https://www.nowcoder.com/ta/beginner-programmers-v1

Online OJ

<1>What is online OJ

Online Judge is an online system used to test participating programs in programming competitions

<2>Why train online OJ

a. Some competitions, such as ACM and other programming competitions, are conducted in the form of online OJ

b. OJ written test, this is the future trend

1. Real knowledge comes from practice

Link: Niuke.com—True knowledge comes from practice

topic:

 answer:

#include <stdio.h>

int main() 
{
   printf("Practice makes perfect!");
   
    return 0;
}

It is recommended to copy and paste the printed content directly , so as to prevent typing mistakes (this question may be forgotten by yourself!)

The corresponding knowledge point is the first program in C language: Hello World!

#include<stdio.h> is the inclusion of the header file, std in stdio is the standard, i is the input, o is the output

The main function is the entry point of the program, and there is only one main function in a program

printf is a library function, which is used to print output. The use of library functions must include header files, and its header file is stdio.h

return 0 is the return value at the end of the main function is 0

Except for the fifth line, the other parts are fixed parts of the C language program, which is an indispensable part of typing code

2. I am a big V

Link: I am a big V

topic:

Solution 1: Note that a newline character is used here\n

#include <stdio.h>

int main()
 {
   printf("v   v\n");
   printf(" v v\n");
   printf("  v\n");
    return 0;
}

Solution 2: Simplified version: print directly with a printf function

#include <stdio.h>

int main() 
{
   printf("v   v\n v v\n  v\n");
    return 0;
}

3. Tolerance is great

topic:

 answer:

#include <stdio.h>

int main() {
    printf("The size of short is %d bytes.\n",sizeof(short));
    printf("The size of int is %d bytes.\n",sizeof(int));
    printf("The size of long is %d bytes.\n",sizeof(long));
    printf("The size of long long is %d bytes.\n",sizeof(long long));


    return 0;
}

sizeof is a keyword used to calculate the size of the space occupied by variables or types (in bytes)

C language standard: sizeof (long long) >= sizeof (long) >=sizeof (int)  >sizeof (short) >sizeof (char)

Supplement: Common units and conversions in computers:

bit - bits

byte - byte

KB                          1byte=8bit

MB                         1KB=1024byte

GB                          1MB=1024KB

TB                          1GB=1024MB

PB                          1TB=1024GB

4. Shorten binary

link: shorten binary

topic:

Solution 1: Pay attention to how to control the case of letters, %x is lowercase, %X is uppercase

#include <stdio.h>

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

Solution 2: Use the # suggested by the title

#include <stdio.h>

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

Expansion: format control of various data

%c - character

%d - shaping

%s - string

%f - single precision floating point number

%lf - double precision floating point number

%p - address format

5. Output a four-digit number in reverse

Link: output a four-digit number in reverse

topic:

 Solution: The idea is to keep going - print 10 divided by 10 -

#include <stdio.h>

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

6. Case conversion

Link: Case Conversion

topic:

 Solution 1:

#include <stdio.h>

int main()
{
	int ch = '0';
	while ((ch=getchar()) != EOF)
	{
		getchar();
		putchar(ch + 32);//大写字母的ASCII值比小写的小32
		printf("\n");//防止输出的字符和下次输入的字符在同一行
	}
	return 0;
}

Question 1: Why is ch defined as int type instead of char type

Answer: getchar: input/read a character putchar: output a character (only one character can be operated at a time)

getchar() returns the ASCII value of the character, which is an integer

EOF -end of file The flag of the end of the file, usually at the end of the file, its value is -1 and it is also an integer


Question 2: Why getchar() is needed in the loop;

Answer: Because there is a buffer between getchar and the keyboard, every time we enter a character, we will press the Enter key, which will add a '\ n' invisible , if there is no getchar(),' \n' is also a character, and 32 is also added in the loop to become another character. In order to take this character away, you need getchar()

Question 3: How to write multiple sets of input?

while((ch=getchar())!=EOF)
{

}

Solution 2: Use printf() plus \n directly instead of putchar()

#include <stdio.h>

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

7. Small plane

Link: Small Airplane

topic:

Solution: This question is mainly to examine the printing of the printf function

#include <stdio.h>

int main()
 {
    printf("     **\n");
    printf("     **\n");
    printf("************\n");
    printf("************\n");
    printf("    *  *\n");
    printf("    *  *\n");
    
    return 0;
}

8. Hexadecimal to decimal

Link: Hexadecimal to Decimal

topic:

 Solution: This question mainly examines the output of the format

#include <stdio.h>

int main() 
{
    printf("%15d",0XABCDEF);//注意这里不能直接写ABCDEF
 
    return 0;
}

9. The return value of printf

Link: return value of printf

topic:

 Solution 1:

#include <stdio.h>

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

 This question mainly examines the return value of printf: the number of characters printed on the screen, such as this question "Hello world!" is 13 characters (including spaces and exclamation points), so the result is

Note: \n cannot be written in the first printf function , so the return value of printf will become 13, one more

Solution 2: Simplified version

#include <stdio.h>

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

Expansion: What is the result of this print?

#include<stdio.h>

int main()
{

	printf("%d", printf("%d", printf("%d", 43)));
	return 0;
}

Answer: 4321 (printf printing starts from the inner layer, the inner layer is called, and then the outer layer, the innermost printf prints 43, and then the next printf prints the return value of the previous printf (that is, 43), which is 2, The last is the return value of 2, which is 1)

10. Input and output of basic student information

Topic link: Input and output of basic information of students

topic:

Solution: Pay attention to two points

(1) Pay attention to the format when scanningf input, see if it is; (semicolon) or, (comma)

(2) Decimals may not be stored accurately in memory. This question needs to be rounded. If the double type does not work, replace it with the float type.

(3) The way to keep m decimal places: %.mf

#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;

}

Conclusion: There is still glory in the other side, and young people are not afraid of the long years

 Please, let's have a three-in-a-row.

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/131652147