C++开发任务:判断移位处理后的数字序列能否构成非递增序列
Alright, let's tackle this C++ problem step by step. I'll break down the requirements, clarify key definitions, and provide working code solutions that fit the problem description.
Problem Breakdown
Here's what you need to accomplish:
- Accept a set of numeric strings as input.
- Rotate each number by moving its first digit to the end (e.g.,
"1234"becomes"2341"). - Determine if these rotated numbers can be rearranged into a non-increasing sequence. Output
yesif possible, otherwiseno.
What Exactly is a Non-Increasing Sequence?
A sequence counts as non-increasing if every element is greater than or equal to the element that comes after it. For example: 9341 ≥ 8941 ≥ 8157 ≥ 5186 ≥ 2813 is a valid non-increasing sequence.
Step-by-Step Approach
1. Rotate Each Input Number
For any numeric string s, creating its rotated version is straightforward: take the substring starting from the second character, then append the first character to the end. Here's a helper function for that:
string rotate(string s) { return s.substr(1) + s[0]; }
2. Compare Rotated Values
Since all input numbers are the same length (implied by the problem's example), we can compare the rotated strings directly using lexicographical order—this is more efficient than converting to integers, especially for large numbers.
3. Check for Valid Non-Increasing Sequence
To determine if the rotated numbers can form a non-increasing sequence:
- Collect all rotated numbers into a list.
- Sort the list in descending order.
- Verify the sorted list follows the non-increasing rule (though a proper descending sort will inherently satisfy this, adding a check makes the logic explicit).
Working Code
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; // Helper to rotate the first digit to the end string rotate(string s) { return s.substr(1) + s[0]; } // Check if a sequence is non-increasing bool isNonIncreasing(const vector<string>& seq) { for (int i = 0; i < seq.size() - 1; ++i) { // Lexicographical comparison works for same-length numeric strings if (seq[i] < seq[i+1]) { return false; } } return true; } int main() { // Sample input (replace with user input handling as needed) vector<string> inputs = {"1934", "7815", "1894", "6518", "3281"}; vector<string> rotatedNumbers; // Generate rotated version for each input for (string num : inputs) { rotatedNumbers.push_back(rotate(num)); } // Sort rotated numbers in descending order sort(rotatedNumbers.begin(), rotatedNumbers.end(), greater<string>()); // Check and output result if (isNonIncreasing(rotatedNumbers)) { cout << "yes" << endl; } else { cout << "no" << endl; } return 0; }
For the Sample Input
The rotated numbers are: "9341", "8157", "8941", "5186", "2813". When sorted in descending order, we get:"9341", "8941", "8157", "5186", "2813"
This sequence is clearly non-increasing, so the code outputs yes.
Edge Cases to Consider
- If input numbers have varying lengths, you'll need to convert strings to integers/long longs for accurate numeric comparison instead of using lexicographical order.
- If the problem required maintaining the original input order (choosing between original or rotated numbers for each position), the logic would shift to greedy selection of valid values—let me know if you need that variation!
内容的提问来源于stack exchange,提问作者Kakashi Sasuke

