每日一题3.27.2

每日一题3.27.2

MP3光标位置

每日一题3.27.2
每日一题3.27.2
解题思路: 当歌曲数小于4时,只有一页,所以只需考虑光标当前的特殊位置只有第一位和最后一位,第一位向上操作光标指向最后一行,最后一行向下光标指向第一行
当歌曲数大于4时,就要考虑翻页问题,多设置一个变量now用来表示光标在当前页面上的位置
代码实现:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int n;
	string m;
	while (cin >> n >> m)
	{
		int point = 1;
		int now = 1;
		if (n <= 4)
		{
			for (int i = 1; i <= n; i++)
			{
				cout << i << " ";
			}
			cout << endl;
			for (int i = 0; i < m.size(); i++)
			{
				if (m[i] == 'U')
				{
					if (point == 1)
						point = n;
					else
						point--;
				}
				else if (m[i] == 'D')
				{
					if (point = n)
						point = 1;
					else
						point++;
				}
			}
			cout << point << endl;
		}
		else
		{
			for (int i = 0; i < m.size(); i++)
			{
				if (m[i] == 'U')
				{
					if (point == 1)
					{
						point = n;
						now = 4;
					}
					else
					{
						if (now != 1)
						{
							point--;
							now--;
						}
						else
							point--;
					}
				}
				else if (m[i] == 'D')
				{
					if (point == n)
					{
						point = 1;
						now = 1;
					}
					else
					{
						if (now != 4)
						{
							point++;
							now++;
						}
						else
						{
							point++;
						}
					}
				}
			}
			switch (now)
			{
			case 1:
				cout << point << " " << point + 1 << " " << point + 2 << " " << point + 3 << endl << point << endl;
				break;
			case 2:
				cout << point - 1 << " " << point << " " << point + 1 << " " << point + 2 << endl << point << endl;
				break;
			case 3:
				cout << point - 2 << " " << point - 1 << " " << point << " " << point + 1 << endl << point << endl;
				break;
			case 4:
				cout << point - 3 << " " << point - 2 << " " << point - 1 << " " << point << endl << point << endl;
				break;

			}
		}
		
	}
	system("pause");
	return 0;
}

参考答案:
每日一题3.27.2
每日一题3.27.2