void swap(int *p,int *q) {
int t
t=*p
*p=*q
*q=t
}
void sort(int a[],int n) {
int i,j,temp
for(i=0i<n-1i++) {
for(j=0j<n-i-1j++) {
if(a[j]>a[j+1])
swap(&a[j],&a[j+1])
}
}
}
int main() {
int a[] = {6,3,8,5,1}
int n = 5
int sum,i
sort(a,n)
n = (n+1) / 2 - 1 /纤和春/ -1 as array indexing in C starts from 0
printf("Median = %d ", a[n])
return 0
}
没有包含头文件conio.h,还有倒数第三行御旁扒改为printf("%d\n",zws(a,b,c))
#include<conio.h>
#include<stdio.h>
int zws(int a, int b, int c){
int ans
if (a >= b &&a <= c) ans = a
if (a <= b &&a >= c) ans = a
if (b >= a &&b <= c) ans = b
if (b <= a &&b >= c) ans = b
if (c >= a &&c <镇昌= b) ans = c
if (c <= a &&c >= b) ans = c
return ans
}
int main()
{
int a, b, c
printf("please input 3 integar:")
scanf("%d %d %d"启薯, &a, &b, &c)
printf("%d\n",zws(a, b, c))
getch()
}
Dim a() As Integer, n As IntegerPrivate Sub Command1_Click()
ReDim a(1 To n)
Randomize (Timer)
For i = 1 To n
a(i) = Int(Rnd(i) * 900 + 100)
Next i
Call median(a, n)
End Sub
Private Sub Command2_Click()
ReDim a(1 To n)
For i = 1 To n
a(i) = InputBox("No_" & i & "=")
Next i
Call median(a, n)
End Sub
Private Sub median(a() As Integer, n As Integer)
Cls
Print " n=" n
Dim median As Single
For i = 1 To n
Print a(i)
If i Mod 10 = 0 Then Print
Next i
For i = 1 To n - 1
For j = 逗首i + 1 To n
If a(j) < a(i) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
山弊数 Next j
Next i
For i = 1 To n
Print a(i)
If i Mod 10 = 0 Then Print
Next i
If n Mod 2 = 1 Then
median = a((n + 1) / 2)
Else
卜坦 median = (a(n / 2) + a(n / 2 + 1)) / 2
End If
Print " 中位数 m=" median
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
n = InputBox("n=", "求n个数的中位数", 100)
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)