C++ 不使用算术、关系或条件运算符设置一个变量
给定三个变量a、b和c,我们的任务是在不使用任何算术、关系和条件运算符的情况下设置x的值。我们需要遵循以下规则。
遵循的方法
If c = 0
x = a
Else // here, in this case, the c variable is binary
x = b.
示例
Input: a = 5, b = 10, c = 0;
Output: x = 5
Input: a = 5, b = 10, c = 1;
Output: x = 10
算术运算符 C++代码
// here we are writing down the C++ programming language code to
// demonstrate the concept of arithmetic operators
#include
using namespace std;
// the main driver code functionality starts from here
int main()
{
int JTP1, JTP2;
JTP1 = 10;
JTP2 = 3;
// printing the sum
cout<< "JTP1 + JTP2= " << (JTP1 + JTP2) << endl;
// printing the difference
cout << "JTP1 - JTP2 = " << (JTP1 - JTP2) << endl;
// printing the product
cout << "JTP1 * JTP2 = " << (JTP1 * JTP2) << endl;
// printing the division
cout << "JTP1 / JTP2 = " << (JTP1 / JTP2) << endl;
// printing the modulo
cout << "JTP1 % JTP2 = " << (JTP1 % JTP2) << endl;
return 0;
}
输出:
JTP1 + JTP2= 13
JTP1 - JTP2 = 7
JTP1 * JTP2 = 30
JTP1 / JTP2 = 3
JTP1 % JTP2 = 1
关系运算符
序号 | 关系运算符 | 运算符描述 | 示例代码片段 |
---|---|---|---|
1 | == |
等于运算符 | x == y |
2 | != |
不等于运算符 | x != y |
3 | < |
小于运算符 | x < y |
4 | <= |
小于等于运算符 | x <= y |
5 | > |
大于运算符 | x > y |
6 | >= |
大于等于运算符 | x >= y |
条件运算符
// here we are writing down the C++ programming language code to
// demonstrate the concept of relational operators
#include
using namespace std;
// the main driver code functionality starts from here
int main()
{
// here, we are declaring the variables
int n1 = 5, n2 = 10, max;
// using the relational operators, we are trying to find the largest one
max = (n1 > n2) ? n1 : n2;
// using the cout output statement, we are trying to print the largest one
cout << "The largest number between " << n1
<< " and " << n2
<< " is " << max;
return 0;
// end of the program
}
输出:
The largest number between 5 and 10 is 10
不使用算术、关系或条件运算符来设置变量
C++ 代码
// here we are writing down the C++ programming language code to
// demonstrate the concept of Setting a variable without using Arithmetic,
// Relational or Conditional Operator
#include
using namespace std;
// function to calculate the value of x returned to the driver code call
int calc(int a, int b, int c)
{
return ((1 - c) * a) + (c * b);
}
// the main driver code functionality starts from here
int main()
{
// assigning the values of the variables
int a = 45, b = 90, c = 0;
int x = calc(a, b, c);
// displaying the output value returned by the calc function
cout << x << endl;
return 0;
}
输出:
45
// here we are writing down the C++ programming language code to
// demonstrate the concept of Setting a variable without using Arithmetic,
// Relational or Conditional Operator
#include
using namespace std;
// function to calculate the value of x returned to the driver code call
int calculate(int a, int b, int c)
{
int arr[] = {a, b};
return *(arr + c);
}
// the main driver code functionality starts from here
int main()
{
// assigning the values of the variables
int a = 6, b = 7, c = 1;
int x = calculate(a, b, c);
// displaying the output value returned by the calc function
cout << x << endl;
return 0;
}
输出:
7