求大佬帮忙看一道c语言编程题 20
#include <stdio.h>
#include <math.h>
void sort(double a[],int n) {
int i,j,k;
double t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j) {
if(a[k] > a[j]) k = j;
}
if (k != i) {
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
}
int main() {
double a[3],b[3],eps = 1E-6;
double judge1,judge2,judge3;
double dif1,dif2,dif3;
while(scanf("%lf%lf%lf%lf%lf%lf",&a[0],&a[1],&a[2],&b[0],&b[1],&b[2]) == 6) {
sort(a,3);
sort(b,3);
if((a[0] + a[1] <= a[2]) || (a[1] + a[2] <= a[0]) || (a[2] + a[0] <= a[1])) printf("A_NO\n");
if((b[0] + b[1] <= b[2]) || (b[1] + b[2] <= b[0]) || (b[2] + b[0] <= b[1])) printf("B_NO\n");
else {
judge1 = a[0]/b[0];
judge2 = a[1]/b[1];
judge3 = a[2]/b[2];
dif1 = judge1 - judge2;
dif2 = judge2 - judge3;
dif3 = judge3 - judge1;
if((fabs(dif1) <= eps) && (fabs(dif2) <= eps) && (fabs(dif3) <= eps)) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
(a1+a2<=a3)||(b1+b2<=b3)我用这句两边之和如果小于等于第三边来判断是否构成三角形(前面已经排好序)不是的话输出NO,然后else里面再用对应成比例判断是否相似
2020-08-10
你写的Num函数无论升序还是降序都不能正确插入。给你写一个做参考——
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdio.h"
void Num(int *a, int n, int x){
int i,k;
k = *a>*(a+n-1) ? 1 : 0;
for(i=n;i>0;i--){
*(a+i)=*(a+i-1);
if(k==1 ? x<=*(a+i) : x>=*(a+i))
break;
}
*(a+i)=x;
}
int main(int argc,char *argv[]){
int a[100],n,x,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&x);
Num(a,n,x);
for(i=0;i<n+1;i++)
printf("%d ",a[i]);//这里有改
printf("\n");//加这一句
return 0;
}
运行样例: