poj.2996

来源:互联网 发布:c语言内嵌汇编 64位 编辑:程序博客网 时间:2024/06/06 01:40

这道题目的大意是;给定一个8*8的棋盘,其中的格子中可能放有white的棋子,用大写字母表示;也可能放有black的棋子,用小写字母表示。a-h表示列,从左到右分别代表1到8列,用1到8的数字表示行,从低到高分别为1到8,题目要求输入这样的棋盘,根据棋盘输出两行,分别为white类型的棋子,black类型的棋子,并按一定次序输出。本题的核心就是模拟输入,然后开两个结点数组,分别记录white和black棋子的标记和坐标,然后分别排序,分别输出即可,另外还需注意algorithm头文件在c++中才有,其中的sort函数,与之匹配的cmp函数的使用是,比较两个数据,返回值为0则交换顺序,否则不交换,为bool类型,当没有特殊的数据结构式默认为升序排序。下面是代码:

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <ctype.h>using namespace std;#define Max 64struct Node{int row;char col;//(a-h)char value;//kq,rb,n,p};Node white[Max],black[Max];int Compare(const char ch1,const char ch2){switch(ch1){case 'K':switch(ch2){case 'K': return 0; default:return 1;}case 'Q':switch(ch2){case 'K': return -1;case 'Q': return 0;default: return 1;}case 'R':switch(ch2){case 'K':case 'Q': return -1;case 'R': return 0;default: return 1;}case 'B':switch(ch2){case 'K':case 'Q':case 'R': return -1;case 'B': return 0;default: return 1;}case 'N':switch(ch2){case 'K':case 'Q':case 'R':case 'B': return -1;case 'N': return 0;default: return 1;}case 'P':  switch(ch2)  {case 'P': return 0;default: return -1;  }  // up case   case 'k':switch(ch2){case 'k': return 0; default:return 1;}case 'q':switch(ch2){case 'k': return -1;case 'q': return 0;default: return 1;}case 'r':switch(ch2){case 'k':case 'q': return -1;case 'r': return 0;default: return 1;}case 'b':switch(ch2){case 'k':case 'q':case 'r': return -1;case 'b': return 0;default: return 1;}case 'n':switch(ch2){case 'k':case 'q':case 'r':case 'b': return -1;case 'n': return 0;default: return 1;}case 'p':  switch(ch2)  {case 'p': return 0;default: return -1;  }}}bool cmp(const Node p,const Node q){if(p.value>='A' && p.value<='Z'){if(p.value!=q.value)return Compare(p.value,q.value)>0;else{if(p.row!=q.row)return p.row<q.row;elsereturn p.col<q.col;}}else{if(p.value!=q.value)return Compare(p.value,q.value)>0;else{if(p.row!=q.row)return p.row>q.row;elsereturn p.col<q.col;}}}int main(){int i,j,index1=0,index2=0;char ch;for(i=1;i<=17;i++){  for(j=1;j<=33;j++){  ch=getchar();  switch(ch){case 'K':case 'Q':case 'R':case 'B':case 'N':case 'P':  white[index1].row=9-i/2;  white[index1].col='a'+(j+1)/4-1;  white[index1++].value=ch;   break;case 'k':case 'q':case 'r':case 'b':case 'n':case 'p':  black[index2].row=9-i/2;  black[index2].col='a'+(j+1)/4-1;  black[index2++].value=ch;   break;  }  }  getchar();}//==================whitesort(white,white+index1,cmp);printf("White: ");for(i=0;i<index1-1;i++)switch(white[i].value){case 'P':printf("%c%d,",white[i].col,white[i].row);break;default:printf("%c%c%d,",white[i].value,white[i].col,white[i].row);}    switch(white[index1-1].value){case 'P':printf("%c%d\n",white[index1-1].col,white[index1-1].row);break;default:printf("%c%c%d\n",white[index1-1].value,white[index1-1].col,white[index1-1].row);}//==================blackprintf("Black: ");sort(black,black+index2,cmp);for(i=0;i<index2-1;i++)switch(black[i].value){case 'p':printf("%c%d,",black[i].col,black[i].row);break;default:printf("%c%c%d,",toupper(black[i].value),black[i].col,black[i].row);}    switch(black[index2-1].value){case 'p':printf("%c%d\n",black[index2-1].col,black[index2-1].row);break;default:printf("%c%c%d\n",toupper(black[index2-1].value),black[index2-1].col,black[index2-1].row);}return 0;}


 

 

原创粉丝点击