C++ knowledge intensive 4 - abs function and practical application

In this article, we will talk about the fourth article of C++ knowledge, abs function and practical application. This column will talk about many, various types. If you like this column, please subscribe and continue to pay attention. Thank you for your support. Next, let’s get into today’s intensive knowledge talk.

What is the abs function used for? 

The main function of the abs function is to calculate an absolute value of a numerical result.

Header files used by abs

#include<iostream>
//或者使用万能头文件也可以,如下:
#include<bits/stdc++.h>

abs function usage

abs (required)

#include<bits/stdc++.h>//头文件

using namespace std;

int main(){
    int a,b,c;//定义三个变量
    cin>>a>>b>>c;//输入
    int sum=(a+b+abs(a-b))/2;//代入公式
    int t=(sum+c+abs(sum-c))/2;//代入公式,替代为sum
     cout<<t<<" eh o maior"<<endl;//输出

}
/*公式中的abs()是直接将变量进行预算取绝对值
abs用法就可以参考上述代码
*/

Instructions for using the abs function 

The abs function can take the absolute value of an integer or a floating-point number. A variable can be filled in the brackets of abs(). Note that the variable calculates a number, or you can directly perform numerical operations in the brackets.

 Knowledge points brought into combat:

Actual import:

Given three integers, find the largest of them.

The following formula may help you:

max(a,b)=(a+b+abs(a−b))/2

Input format:

The input takes one line and contains three integers.

Output format:

The output format is  X eh o maiorwhere X is the largest of the three numbers.

data range

1<=given integer<=1091<=given integer<=109

enter:

7 14 106

Output:

106 eh o maior

Analysis of Algorithms:

Are there any calculation formulas in the two number questions of a and b? But how to find three numbers? We can find the maximum value of a and b, store it in a variable (sum), and then use sum and c to calculate to find the maximum value. There is no big difficulty in the calculation part, and the output pays attention to the output format.

Code:

#include<bits/stdc++.h>//头文件

using namespace std;

int main(){
    int a,b,c;//定义三个变量
    cin>>a>>b>>c;//输入
    int sum=(a+b+abs(a-b))/2;//代入公式
    int t=(sum+c+abs(sum-c))/2;//代入公式,替代为sum
     cout<<t<<" eh o maior"<<endl;//输出

}
/*公式中的abs()是直接将变量进行预算取绝对值
abs用法就可以参考上述代码
*/

Output results

106 is the biggest

Summarize:

This is how to use abs, bringing the knowledge points into actual combat. If you have any questions, please leave a message in the comment area. The author will reply one by one after seeing it.

Guess you like

Origin blog.csdn.net/djfihhfs/article/details/126224459