C語言程式設計 現代方法 第九章程式設計題答案
阿新 • • 發佈:2019-01-08
以下程式都是在VS軟體下進行編譯的,如果要用VC編譯器,刪除system(“paues”);即可
9.1
//No.1不用遞迴 #include<stdio.h> #define N 20 void selection_sort(int k, int c[]); int main(int argc,char *argv[]) { int a[N]; int n; printf("Enter total of numbers:"); scanf("%d", &n); printf("Enter numbers:"); int i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } selection_sort(n, a); for (i = 0; i < n; i++) { printf("%d ", a[i]); } puts(""); system("pause"); return 0; } void selection_sort(int m, int b[]) { int j, i; int tmp;; for (j = 0; j < m; j++) { for (i = j+1; i < m; i++) { if (b[j] >= b[i]) { tmp = b[j]; b[j] = b[i]; b[i] = tmp; } } } }
#include<stdio.h> #define N 20 void sort(int a,int p[]); int main() { int num[N]; int i,n; printf("Enter total of numbers:"); scanf("%d",&n); printf("Enter numbers:"); for(i=0;i<n;i++) { scanf("%d",&num[i]); } printf("The sort number is:"); sort(n,num); for(i=0;i<n;i++) { printf("%d ",num[i]); } puts(""); return 0; } void sort(int a,int p[]) { int i; int m=p[0]; int j=0; for(i=0;i<a;i++) { if(m<=p[i]) { m=p[i]; j=i; } } p[j]=p[a-1]; p[a-1]=m; if(a==0)return; sort(a-1,p); }
9.2
#include<stdio.h> float Taxes(float n); int main(int argc, char *argv[]) { float taxes; float income; printf("Enter incomes:"); scanf("%f", &income); taxes = Taxes(income); printf("The taxes is:%.2f\n", taxes); system("pause"); return 0; } float Taxes(float n) { if (n <= 750)return n*0.01; else if (n <= 2250)return 7.5f + (n - 750)*0.02; else if (n <= 3750)return 37.5f + (n - 2250)*0.03; else if (n <= 5250)return 82.5f + (n - 3750)*0.04; else if (n <= 7000)return 142.5f + (n - 5250)*0.05; else { return 230.0f + (n - 7000)*0.06; } }