(UVA)299-泡沫排序
#include <stdio.h> int main() { int time, n; scanf("%d", &time); /*共幾組*/ while (time--){ int a[50], count = 0, temp,i,j; scanf("%d", &n); /*每組n個*/ for ( i = 0; i < n; i++) /*輸入n個值*/ scanf("%d", &a[i]); for (i = 0; i < n; i++) for ( j = 0; j < n-1 ; j++) if (a[j] > a[j + 1]){ /*比大小 交換 count++*/ temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; count++; } printf("Optimal train swapping takes %d swaps.\n", count); } return 0; }
留言
張貼留言