1、 定义一个函数int fun(int a,int b,int c),它的功能是:若a,b,c能构成等边三角形函数返回3,若能构成等腰三角形函数返回2,若能构成一般三角形函数返回1,若不能构成三角形函数返回0。
时间: 2024-03-09 22:45:02 浏览: 23
以下是对应的 C++ 代码实现:
```cpp
int fun(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) { // 任意一条边为非正数,不能构成三角形
return 0;
}
if (a == b && b == c) { // 三边相等,构成等边三角形
return 3;
}
if (a == b || b == c || a == c) { // 两边相等,构成等腰三角形
return 2;
}
if (a + b > c && b + c > a && a + c > b) { // 三边之和大于第三边,能构成三角形
return 1;
}
return 0; // 不能构成三角形
}
```
在这个版本的函数中,除了判断是否能构成三角形和是否是等边三角形、等腰三角形之外,还增加了一种情况,即能构成一般的三角形,返回值为1。
相关问题
1、 定义一个函数int fun(int a,int b,int c),它的功能是:若a,b,c能构成等边三角形函数返回3,若能构成等腰三角形函数返回
2,若不能构成三角形函数返回0。
以下是对应的 Java 代码实现:
```java
public static int fun(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) { // 任意一条边为非正数,不能构成三角形
return 0;
}
if (a == b && b == c) { // 三边相等,构成等边三角形
return 3;
}
if (a == b || b == c || a == c) { // 两边相等,构成等腰三角形
return 2;
}
if (a + b > c && b + c > a && a + c > b) { // 三边之和大于第三边,能构成三角形
return 1;
}
return 0; // 不能构成三角形
}
```
Java 代码中和 C++ 代码中的实现类似,注意在 Java 中函数定义时需要加上 `public static`,而在函数内部,变量的比较和逻辑判断符号也和 C++ 相同。
用c语言交换两个整数的值,函数接口定义:void fun (int *a,int *b);
下面是交换两个整数的值的 C 语言代码:
```c
void fun(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
```
调用这个函数时,传入两个整数的地址即可:
```c
int main() {
int a = 1, b = 2;
printf("Before swap: a = %d, b = %d\n", a, b);
fun(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
```
输出:
```
Before swap: a = 1, b = 2
After swap: a = 2, b = 1
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)