Description: |
Enter a string of numbers to find the maximum number of increments. Increment refers to the number of adjacent digits arranged from small to large. For example, if the number of increments is 2895345323 or 23, the maximum number of increments is 289,345. |
Running time limit: |
Unlimited |
Memory limit: |
Unlimited |
Input: |
Enter a number. By default, the number is correct, that is, it does not contain characters or spaces. |
Output: |
Maximum number of output increments |
Sample input: |
123526897215 |
Sample output: |
2689 |
Answer: |
|
A basic dynamic planning algorithm is implemented in C ++. The Code is as follows:
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 char num[50]; 6 int currentValue=0; 7 int maxValue=0; 8 while (cin>>num) 9 {10 int i=0;11 for (i=0;num[i];i++)12 {13 currentValue=currentValue*10+num[i]-‘0‘;14 if(num[i]>=num[i+1])15 {16 if(currentValue>maxValue)17 maxValue=currentValue;18 currentValue=0;19 }20 }21 cout<<maxValue<<endl;22 maxValue=0;23 }24 return 0;25 }
Running result:
2014 Huawei school recruitment test simulation-calculate the maximum incremental number