C Language programming introduction Training (2)

The article directories

Follow the previous introductory exercise (1) – blog entry \

Exercise 11: ASCII code

Topic describes

BoBo taught KiKi that characters represented by character constants or character variables are stored in memory as ASCII codes. BoBo has a problem for KiKi, convert the following ASCII characters into corresponding characters and output them.

73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116, 33

Input description:

There is no

Output description:

Convert all ASCII characters given in the output question to the corresponding characters.

The code is as follows:

#include  <stdio.h>

int main(a)
{
	char arr[12] = { 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<12; i++)
	{
		printf("%c", arr[i]);
	}
	return 0;
}
Copy the code

\

Exercise 12: Date of birth input and output

Topic describes

Enter a person’s date of birth (including year, month and day), and output the year, month and day of the birthday separately.

Input description:

Enter a single line, date of birth, including year, month, and day, with no delimiters between the numbers.

Output description:

Three lines: the first act is the year of birth, the second act is the month of birth, and the third act is the date of birth. If the month or day is 1 digit, 0 must be added before the 1 digit.

Example 1 Input

20130225

The output

year=2013

month=02

date=25

The code is as follows:

#include <stdio.h>

int main(a)
{
    int year=0;
    int month=0;
    int day=0;
    
    scanf("%4d%2d%2d",&year,&month,&day);
    printf("year=%4d\n",year);
    printf("month=%02d\n",month);
    printf("date=%02d\n",day);
    return 0;
}
Copy the code

Note: The input field width (number of columns) can be specified by scanf function’s %m format control, and the required data can be intercepted according to this width. By using the %0 format control of the printf function, an empty position not used on the left of a numeric value is automatically filled with 0.

\

Exercise 13: Format input and swap output

Topic describes

Enter two integers, ranging from -231 to 231-1, swap the two numbers and print the output.

Input description:

The input line consists of two integer ranges separated by commas (,).

Output description:

Print two integers in format separated by commas (,).

Example 1

The input

a=1,b=2

The output

a=2,b=1

The code is as follows:

#include <stdio.h>

int main(a)
{
    int a;
    int b;
    int tmp;
    scanf("a=%d,b=%d",&a,&b);
    tmp=a;
    a=b;
    b=tmp;
    printf("a=%d,b=%d",a,b);
    return 0;
}
Copy the code

Remark:

If there are non-format characters in the format control string, this non-format character is also entered. \

Exercise 14: Convert characters to ASCII values

Topic describes

BoBo taught KiKi that characters represented by character constants or character variables are stored in memory as ASCII codes. BoBo asks KiKi to input a character and output the corresponding ASCII code for that character.

Input description:

One line, one character.

Output description:

One line, output the ASCII code corresponding to the input character.

Example 1

The input

c

The output

99

The code is as follows:

#include <stdio.h>

int main(a)
{
    char ch;
    scanf("%c",&ch);
        printf("%d",ch);
    return 0;
}
Copy the code

\

Exercise 15: Evaluate expressions

Topic describes

Calculate the expression “(-8+22)× A-10 + C ÷2”, where a = 40, c = 212.

Input description:

No.

Output description:

(-8+22)× A-10 + C ÷2 after calculation, is an integer.

The code is as follows:

#include <stdio.h>
int main(a)
{
    int a=40;
    int c=212;
    int sum=0;
        sum=(- 8 -+22)*a- 10+c/2;
    printf("%d\n",sum);
    return 0;
}
Copy the code

\

Exercise 16: Calculating residual division

Topic describes

Given two integers A and b (-10,000 < a,b < 10,000), compute the integer quotient and remainder of a divided by b.

Input description:

A line containing the dividend and divisor (not zero) of two integers a and b separated by a space.

Output description:

A line containing two integers, the integer quotient and remainder, separated by a space.

Example 1

The input

15 2

The output

July 1

The code is as follows:

#include <stdio.h>
int main(a)
{
    int a=0;
    int b=0;
    scanf("%d %d",&a,&b);
    printf("%d %d",a/b,a%b);
    return 0;
}
Copy the code

\

Exercise 17: Kiki counts

Topic describes

Problem: KiKi is 5 years old and can already recognize and add non-negative integers up to 100. However, Mr BoBo noticed that KiKi’s rules for calculating positive integers greater than or equal to 100 were as follows:

  1. Keep only the last two digits of the number, for example: for KiKi 1234 is equivalent to 34;
  2. If the result is greater than or equal to 100, KIKI also keeps only the last two digits of the result, and only the ones digits if the tens place is 0.

For example, 45+80 = 25 requires that non-negative integers A and b be given, and the KiKi algorithm is simulated to calculate the value of a+b.

Input description:

On one line, enter two non-negative integers, a and b, separated by a space. (0 <= a,b<= 231-1).

Output description:

For each set of inputs, the output calculates the value of a+b according to KiKi’s algorithm.

Example 1

The input

45, 80

The output

25

The code is as follows:

#include <stdio.h>

int main(a)
{
    int a;
    int b;
    scanf("%d %d",&a,&b);
    printf("%d\n",(a%100+b%100) %100);
    //printf("%d",(a+b)%100);
    
    return 0;
}
Copy the code

\

Exercise 18: The ones digit of a floating point number

Topic describes

Given a floating point number, the units digit is required.

Input description:

One line, including a floating point number.

Output description:

A line containing an integer that corresponds to the units digit of the input floating point number.

Example 1

The input

13.141

The output

3

The code is as follows:

#include <stdio.h>
int main(a)
{
    float c;
    scanf("%f",&c);
    printf("%d", (int)c%10);
    return 0;
}
Copy the code

\

Exercise 19: How many seconds can you Live?

Topic describes

Question: there are about 3.156×107s in a year, ask to input your age, show how many seconds of this age.

Input description:

A line that includes an integer age(0<age<=200).

Output description:

A line containing an integer that outputs the number of seconds corresponding to the age.

Example 1 Input

20

The output

631200000

The code is as follows:

#include <stdio.h>
int main(a)
{
	int age=0;
	scanf("%d", &age);
	long long ret = age*3.156 e7;
		printf("%lld", ret);

	return 0;
}
Copy the code

Note: the exponential form of C language should be noted, and the size of the data value of int is limited. For large numbers, we should have long or long long stores.

\

Exercise 20: Time shift

Topic describes

Given the number of seconds (0< seconds < 100,000,000), convert seconds to hours, minutes, and seconds.

Input description:

A line containing an integer, the given number of seconds.

Output description:

A line containing three integers, the number of hours, minutes, and seconds (possibly zero) corresponding to the input integers, separated by a space.

Example 1

The input

3661

The output

1 1 1

The code is as follows:

#include <stdio.h>

int main(a)
{
    int sec=0;
    scanf("%d",&sec);
    printf("%d %d %d",sec/60/60,sec/60%60,sec%60);
    return 0;
}
Copy the code

Note: pay attention to the correct expressions of hour, minute and second in the question.

\

Exercise 21: Total score and even score calculation

Topic describes

Input a student’s 3 subject scores in turn, output the student’s total score and average score on the screen.

Input description:

One line, three subject scores separated by a space.

Output description:

A line, total grade and average grade (two decimal places reserved), separated by a space.

Example 1

The input

79.5 80.0 98.0

The output

257.50 85.83

The code is as follows:

#include <stdio.h>
int main(a)
{
    float a,b,c;
    scanf("%f %f %f",&a,&b,&c);
    printf("%.2f %.2f",a+b+c,(a+b+c)/3.0);
    return 0;
}
Copy the code

\

Exercise 22: Calculate your body mass index

Topic describes

Question: Calculate BMI (body mass index). BMI (Body Mass Index, also known as BMI) is calculated by dividing one’s weight in kilograms by the square of one’s height in metres. It is an international standard used to measure one’s weight, thinness and health. BMI is mainly used for statistical purposes. When we need to compare and analyze the health impact of a person’s weight on people of different heights, BMI value is a neutral and reliable indicator.

Input description:

A line of two integers representing weight in kilograms and height in centimeters, separated by a space.

Output description:

One line, BMI (keep two decimal places).

Example 1

The input

70, 170,

The output

24.22

The code is as follows:

#include <stdio.h>
#include<math.h>
int main(a)
{
    int wei,high;
    scanf("%d %d",&wei,&high);
   printf("%.2f",wei/(pow((high/100.0),2)));
    return 0;
}
Copy the code

Note: Mathematical exponential functions refer to header file <math.h>. Be familiar with basic mathematical notation functions

\

Exercise 23: Calculate the perimeter and area of a triangle

Topic describes

Calculate the perimeter and area of the triangle according to the given three sides a, B and C (0 < a, b, c < 100,000).

Input description:

A line of three sides of a triangle separated by a space.

Output description:

A line containing the perimeter and area of a triangle (two decimal digits reserved) separated by a space. For details about the output format, see the output example.

Example 1

The input

3 3 3

The output

Circumference area = 9.00 = 3.90

The code is as follows:

#include <stdio.h>
#include <math.h>
int main(a)
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d%d%d", &a, &b, &c);
	double circumference = a + b + c;
	float p = (a + b + c) / 2.0;
	float area = sqrt(p*(p - a)*(p - b)*(p - c));
		
		printf("circumference=%.2f area=%.2f", circumference, area);

		return 0;
}
Copy the code

Note: familiar with the application of square function, and Helen’s formula to calculate the area of a triangle.

\

Exercise 24: Calculate the volume of a sphere

Topic describes

Given the radius of a sphere, calculate its volume. The sphere volume formula is V = 4/3*π R3, where π= 3.1415926.

Input description:

A line of floating point radii of the sphere.

Output description:

One line, the volume of the sphere, three decimal places.

Example 1

The input

3.0

The output

113.097

The code is as follows:

#include  <stdio.h>
#include  <math.h>
int main(a)
{
    double r=0.0;
    double pi=3.1415926;
    scanf("%lf",&r);
    printf("%.3lf".4.0/3*pi*r*r*r);
    return 0;
}
Copy the code

\

Exercise 25: Case conversion

Topic describes

The realization of letter case conversion. Multiple input/output groups.

Input description:

Multiple groups of inputs, each line in capital letters.

Output description:

Lowercase letters for each set of input and output.

Example 1 Input

A

B

The output

a

b

The code is as follows:

#include <stdio.h>
int main(a)
{
	char ch ;
	while((ch = getchar()) ! = EOF) { getchar();putchar(ch+32);

		printf("\n");
	}
	return 0;
}
Copy the code

Note: When entering multiple groups, note that carriage return is also a letter, so “absorb” (getchar()) to remove the letter.

\

Exercise 26: Two to the NTH power

Topic describes

On the basis of not using the cumulative multiplication, by the shift operation (<<) to achieve the calculation of 2 to the n.

Input description:

Multiple groups of inputs, each line entering the integer n (0 <= n < 31).

Output description:

2 to the n for each set of inputs and outputs.

Example 1

The input

2

10

The output

4, 1024

The code is as follows:

#include <stdio.h>

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

Note: Be familiar with bitwise operators!!






\

To be continued…