[2018.04.12][水][日志][4][#1160][龙舟][啥也没有][背景->][简单的最后一题][暴力+离散化]

[背景]

        今天是第一次虚伪的信息学测试,蒟蒻的我RP不够,没能写好所有Code,但我虚伪掉了最后一题[龙舟],虚伪虚伪...

[#1160 龙舟比赛]

现在正在举行龙舟比赛,我们现在获得了最后冲刺时的俯视图像,现在你要输出各条龙舟的名次。
这张图像由r行c列的字符组成,每行的最左边的字符表示起点,所以字符为’S’,最右边的字符为’F’。并且确定在9行里有数字字符1..9,来表示每条龙舟的编号,保证包含数字的行里,只包含连续的相同的3个数字。其余地方都是字符’.’。 具体看样例。越靠近右边的龙舟排名越靠前。如果有并列的,那么他们的名次相同。
### 输入格式
第一行是两个整数r和c(10 ≤ r,c ≤ 50)。表示图像有r行,c列。下面有r行字符,以字符’S’开头,’F’结尾,还含有数字字符和’.’字符。保证只有9行含有数字字符,并且每行的数字字符完全相同,含有3个数字。
### 输出格式
9行,第i行表示i号龙舟的名次。
*input*
10 15
S..........222F
S.....111.....F
S...333.......F
S...555.......F
S.......444...F
S.............F
S......777....F
S..888........F
S........999..F
S...666.......F
*output*
5
1
6
3
6
6
4
7

2

很好,这道题和前面的#134#147相比简直是水题中的水题,用的思路也非常简单,处理工具的复杂性和用的方法的巧妙性成反比;

虚伪的:



然后只要这样。

不过,你的(我的)自定义函数还没有好,只有一个泛型。


输入是第一步;把所有输进的字符+数字存到x,y以及一张地图中去;


最关键的是给船排序,由于我们的是等宽字体,不难看出,龙舟数字所在的列就代表它的rank值,但还需要处理一下这个rank(因为列的序号不一定是连续的)


输出


自定义排序函数(看清楚列位值的排序即可)

#include <bits/stdc++.h>


using std::cout;
using std::cin;
using std::endl;
using std::sort;

struct boat
{
int id; //id
int rank; //rank
int location; //locations
};
boat boats[10]; //suppose that there is 10 boats

char map[100][100]; //the inputted map
int x=0,y=0; //side of the map

bool mycmp1(boat input1,boat input2); //DIY compare fuction1
bool mycmp2(boat input1,boat input2); //DIY compare fuction1

void input(void); //input
void sort_boats(void); //sort the boats
void output(void); //output

int main(void)
{
input();

sort_boats();

output();
return 0;
}


bool mycmp1(boat input1,boat input2) //sort the boats with the location in order to calculate the ranks
{
return (input1.location>input2.location);
}


bool mycmp2(boat input1,boat input2) //replace the boats
{
return (input1.id<input2.id);
}


void input(void)
{
memset(map,0,sizeof(map)); //Re:start!

cin>>x>>y;
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
cin>>map[i][j];

return;
}


void sort_boats(void)
{
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
{
if(map[i][j]>='1'&&map[i][j]<='9')
{
boats[map[i][j]-'0'].location=j; //record the location of j 
boats[map[i][j]-'0'].id=map[i][j]-'0'; //record the id
break;
}
}

sort(boats+1,boats+10,mycmp1);

int rank=1;
for(int i=1;i<=9;i++) //set the ranks
{
boats[i].rank=rank;
if(boats[i].location!=boats[i+1].location)
rank++;
}

sort(boats+1,boats+10,mycmp2); //replace
return;
}


void output(void)
{
for(int i=1;i<=9;i++)
cout<<boats[i].rank<<endl;
return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值