做题WA了,写代码时考虑的不周全,没有考虑到当木棒长度相等时,还需要按重量对此排序。
然后看了网友博客里写的代码,感觉自己写的代码相比别人的来说很乱,。
#include<stdio.h> #define LOCAL struct wooden { int length, weight; }s[5005], buf[5005], tmp, pre; int main() { #ifdef LOCAL freopen("Wooden.in.txt", "r", stdin); #endif int t, n, i, j, k, m, count; scanf("%d", &t); while(t-- && scanf("%d", &n) && n>=1 && n<= 5000) { count=0; i=n; while(i--) scanf("%d%d", &s[i].length, &s[i].weight); for(i=0; i<n-1; i++) for(j=0; j<n-1; j++) if(s[j].length>s[j+1].length) { tmp=s[j]; s[j]=s[j+1]; s[j+1]=tmp; } k=0; for(i=0; i<n; i++) { if( !s[i].length && !s[i].weight ) continue; pre=s[i]; for(j=i+1; j<n; j++) { if(s[j].weight && s[j].length && s[j].weight>=pre.weight) { pre=s[j]; s[j].length=0; s[j].weight=0; } } count++; } printf("%d\n", count); } return 0; }