参考http://blog.csdn.net/jun_sky/article/details/7008625
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 using namespace std; 5 struct node 6 { 7 int x,y,p; 8 char c; 9 }b[70],w[70]; 10 int in(char c) 11 { 12 switch(c) 13 { 14 case 'K':case 'k':return 1; 15 case 'Q':case 'q':return 2; 16 case 'R':case 'r':return 3; 17 case 'B':case 'b':return 4; 18 case 'N':case 'n':return 5; 19 default:return 6; 20 } 21 } 22 int cmpw(node a,node b) 23 { 24 if(a.p!=b.p) return a.p<b.p; 25 if(a.x!=b.x) return a.x<b.x; 26 if(a.y!=b.y) return a.y<b.y; 27 } 28 int cmpb(node a,node b) 29 { 30 if(a.p!=b.p) return a.p<b.p; 31 if(a.x!=b.x) return a.x>b.x; 32 if(a.y!=b.y) return a.y<b.y; 33 } 34 int main() 35 { 36 int i,j,cnb=0,cnw=0; 37 char s[50],t1,t2,a; 38 //freopen("in.txt","r",stdin); 39 for(i=8;i>=1;i--) 40 { 41 gets(s); 42 for(j=0;j<8;j++) 43 { 44 getchar(); 45 scanf("%c%c%c",&t1,&a,&t2); 46 if(a>='a' && a<='z') 47 { 48 b[cnb].x=i; 49 b[cnb].y=j; 50 b[cnb].p=in(a); 51 b[cnb++].c=a; 52 } 53 else if(a>='A' && a<='Z') 54 { 55 w[cnw].x=i; 56 w[cnw].y=j; 57 w[cnw].p=in(a); 58 w[cnw++].c=a; 59 } 60 else continue; 61 } 62 getchar();getchar(); 63 } 64 gets(s); 65 sort(w,w+cnw,cmpw); 66 sort(b,b+cnb,cmpb); 67 printf("White: "); 68 for(i=0;i<cnw;i++) 69 { 70 if(w[i].c!='P')printf(i==0?"%c%c%d":",%c%c%d",w[i].c,w[i].y+'a',w[i].x); 71 else printf(i==0?"%c%d":",%c%d",w[i].y+'a',w[i].x); 72 } 73 printf("\n"); 74 printf("Black: "); 75 for(i=0;i<cnb;i++) 76 { 77 if(b[i].c!='p')printf(i==0?"%c%c%d":",%c%c%d",b[i].c-32,b[i].y+'a',b[i].x); 78 else printf(i==0?"%c%d":",%c%d",b[i].y+'a',b[i].x); 79 } 80 printf("\n"); 81 return 1; 82 }