[improve a little every day] C language problem brushing skills and training 2

Posted by theblacksheep on Wed, 26 Jan 2022 20:59:45 +0100

Congratulations on finding the treasure. This is a moment. I'm glad to serve you~

Today is the new year in the south. I wish you a happy New Year!

1. Return value of printf

Title:
describe
KiKi wrote an output "Hello world!" Mr. BoBo told him that the printf function has a return value. You can help him write a program to output printf("Hello world!") Return value of?
Enter Description:
nothing
Output Description:
Includes two lines:
The first line is "Hello world!"
The second line is printf("Hello world!") The return value after the call.


Core knowledge:
The printf function returns the number of characters printed on the screen
The space is also a character, so the first line prints "Hello world!" There are 12 characters, so the return value is 12.

2. Input and output of students' basic information

Title:
describe
Input the student number of a student and the scores of 3 subjects (C language, mathematics and English) in turn, and output the student's student number and scores of 3 subjects on the screen (Note: the scores shall be rounded and 2 decimal places shall be reserved).
Data range: the student number meets 1 ≤ n ≤ 20000000, the score of each subject uses the percentage system, and there can be no negative number
Enter Description:
The student number and the grades of 3 subjects are separated by English semicolons and English commas.
Output Description:
Student number, grade of 3 subjects. See the output example for the output format.

Examples
Input:
17140216;80.845,90.55,100.00
Output:
The each subject score of No. 17140216 is 80.85, 90.55, 100.00.

#include <stdio.h>
int main()
{
	int id = 0;
	float c_score = 0.0;
	float math_score = 0.0;
	float english_score = 0.0;
	//input
	scanf("%d;%f,%f,%f", &id, &c_score, &math_score, &english_score);
	//output
	printf("The each subject score of No. %d is %.2f, %.2f, %.2f.", id, c_score, math_score, english_score);
	return 0;
}

Core knowledge:
Decimals may not be saved accurately in memory
When rounding, the result may be different because the precision of float and double are different. Therefore, select the appropriate type to define the floating point number according to the output result of the topic

3. Judgment letters

Title:
describe
Input a character arbitrarily from the keyboard, and program to judge whether it is a letter (including case).
Enter Description:
Multiple sets of input, each line of input includes one character.
Output Description:
For each line of input, output whether the character is a letter (YES) or not (NO).

Examples
Input:
H
9
Output:
YES
NO

#include <stdio.h>
int main()
{
	char ch;
	//Multi group input
	while (scanf("%c", &ch) != EOF)
	{
		//Judge and output
		if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
			printf("YES\n");
		else printf("NO\n");
		getchar();//Processing \ n
	}
	return 0;
}

a key:
Multi group input
Distinguishing between characters and letters:
Letters are part of characters, including lowercase a ~ Z and uppercase A ~ Z
getchar(); Used to process \ n
This is a very important point. It is used to deal with redundant characters in character questions. This usage also appeared in the previous issue, which shows its importance.

4. Character pyramid

Title:
Enter description
The input has only one line and one character
Output description
This character forms a triangular pyramid

Examples
Input 1

#include <stdio.h>
int main()
{
	//Enter a character
	char ch = 0;
	ch = getchar();
	//Output pyramid
	int i = 0;
	//Print one line per cycle
	//Each line consists of two parts, spaces and characters
	for (i = 0; i < 5; i++)
	{
		int j = 0;
		//Space
		for (j = 0; j < 4 - i; j++)
		{
			printf(" ");
		}
		//character
		for (j = 0; j <=i; j++)
		{
			printf("%c ", ch);
		}
		printf("\n");
	}
	return 0;
}

Idea:
For the problem of printing graphics, the focus is to find the law. You can divide the problem into several parts.
For example, this problem is divided into two parts to deal with spaces and characters.

5.ASCII code

Title:
describe
BoBo teaches KiKi that characters represented by character constants or character variables are stored in ASCII code in memory. BoBo > there is a problem with KiKi. Convert the following ASCII codes into corresponding characters and output them.
73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33
Enter Description:
nothing
Output Description:
Convert all ASCII characters given in the output title to the corresponding characters.

#include <stdio.h>
int main()
{
	char arr[] = { 73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33 };
	// arr is an array, which is accessed by subscript!
	// 0
	int i = 0;
	//Calculate the number of elements of the array
	int sz;
	sz = sizeof(arr) / sizeof(arr[0]);//Number of elements = total bytes ÷ number of bytes of an element
	for (i = 0; i < sz; i++)
	{
		printf("%c", arr[i]);
	}
	return 0;
}

High energy is coming, magic is coming, gulara, God of darkness, chirp ~ change~


The result is "I can do it!"
I hope we can all say to ourselves "I can do it!"
Cat cat rush~

6. Date of birth input and output

Title:
describe
Enter a person's date of birth (including year, month and day), and output the year, month and day of the birthday respectively.
Data range: the year meets 1990 ≤ y ≤ 2015, the month meets 1 ≤ m ≤ 12, and the day meets 1 ≤ d ≤ 30
Enter Description:
There is only one line to enter. The date of birth includes month, year and day. There is no separator between month, year and day.
Output Description:
Three lines, the first line is the year of birth, the second line is the month of birth, and the third line is the date of birth. When outputting, if the month or day is 1 digit, you need to fill 0 in front of 1 digit.

Examples
Input:
20130225
Output:
year=2013
month=02
date=25

#include <stdio.h>
int main()
{
	int year = 0;
	int month = 0;
	int date = 0;
	//Enter in format
	scanf("%4d%2d%2d", &year, &month, &date);
	//output
	printf("year=%4d\n", year);
	printf("month=%02d\n", month);
	printf("date=%02d\n", date);
	return 0;
}

Core knowledge:
Through the%m format control of scanf function, you can specify the input field width and input data field width (number of columns), and intercept the required data according to this width;
Through the% 0 format controller of printf function, specify the empty position not used on the left when outputting the value, and fill in 0 automatically.

7. Calculation to the nth power of 2

Title:
describe
The calculation of the n-th power of 2 is realized by shift operation (< <) without cumulative multiplication.
Data range: 0 ≤ n ≤ 31
Enter Description:
Enter the integer n in one line (0 < = n < 31).
Output Description:
Output the result of the corresponding n-th power of 2.

Examples
Input:
2
Output:
4

#include <stdio.h>
int main()
{
	int n = 0;
	scanf("%d", &n);
	// Calculation to the nth power of 2
	printf("%d\n", 1 << n);
	return 0;
}

Core knowledge:

Make a little progress every day

OK, everybody
This is the whole content of this blog. Thank you very much for seeing here. I'll see you next time~

If you think this article is well written, please praise, collect and forward. The most important thing is to pay great attention. Your support is my biggest motivation

Topics: C Back-end