题目链接:
http://codeforces.com/problemset/problem/748/A
Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture).
The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right.
Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right!
The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place.
Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right.
4 3 9
2 2 L
4 3 24
4 3 R
2 4 4
1 2 R
题目大意:
第一行输入n m k,n表示有n竖道桌子(两张桌子放一块),m表示有m排桌子,k表示该同学的桌子编号,要求计算该学生在第几竖道、在第几排、在左边还是在右边(左边输出‘L’,右边输出‘R’)桌子编号规则:如题目中的图示。
解题思路:
从该生所在位置开始算起,ansm存储排数,k依次减2,ansm++,直到到达第一排为止(需要提前标记第一排桌子编号,我运用了mapring器),计算出排数,列数利用公式:
k为奇数:
(该生所在列的第一排桌子编号 - 1)/(2*m)
k为偶数:
(发生所在列的第一排桌子编号 - 2)/(2*m)
代码:
#include<iostream>
#include<map>
using namespace std;
int main()
{
int n,m,k;
cin>>n>>m>>k;
map<int,int>M;
for(int i=0;;i=i+2)
{
if(i*m+1>2*n*m) //根据n和m的取值范围,桌子最大编号为2*n*m
break;
M[i*m+1]=1; //把每一竖排第一行的第一张桌子标记下来
M[i*m+2]=1; //把每一竖排第一行的第二张桌子标记下来
}
int ansm=1,s=k; //ansm储存该学生在第几排
while(1) //计算该学生在第几排(从该生所在的排数开始减,知道第一排为止,ansm依次++)
{
if(M[s]==1) //已经到达第一排,可以跳出循环了,ansm即为排数
break;
ansm++;
s=s-2; //因为两张桌子放一块的,所以每次减2
}
if(k%2==0) //k为偶数,则一定在右边,再计算列数
cout<<(s-2)/(2*m)+1<<' '<<ansm<<' '<<'R'<<endl;
else //k为奇数,则在左边,计算列数
cout<<(s-1)/(2*m)+1<<' '<<ansm<<' '<<'L'<<endl;
return 0;
}