同窗發過來個文件,讓我幫他作幾個題目的,就作了下,貼在這裏css
題目:html
描述:
輸入一串數字,找到其中包含的最大遞增數。遞增數是指相鄰的數位從小到大排列的數字。如: 2895345323,遞增數有:289,345,23, 那麼最大的遞減數爲345。
輸入:
輸入一串數字,默認這串數字是正確的,即裏面不含有字符/空格等狀況
輸出:
輸出最大遞增數
樣例輸入:
123526897215
樣例輸出:
2689ios
1: #include<iostream>
2: #include<stdlib.h>
3: #include<cmath>
4: #include<vector>
5: #include<map>
6: #include <string>
7: using namespace std;
8: int main()
9: {
10: map<char,int> m;
11: for(int i=0;i<10;i++) //字符映射數字
12: {
13:
14: m['0'+i] = i;
15: }
16:
17: string num; //存放輸入字符串
18: cin>>num;
19: int length = num.length();
20: if(length==1)
21: {
22: cout<<num<<endl; //輸入只有1個數時,直接輸出。
23: return 0;
24: }
25:
26: int max = 0; //用於存放目前最大遞增數
27: string current = "";
28: current = current + num[0];//current
29: int temp = 0; //用於標記當前遞增數是否掃描結束,1表示結束
30: for(int j =1;j<length;j++)
31: {
32: int sum = 0;
33:
34: if(m[num[j]]>m[num[j-1]])
35: current = current + num[j];
36: else
37: temp=1;
38:
39: if(temp == 1 || j == length-1) //只有當前遞增數掃描結束或到尾部纔開始計算當前遞增數的值
40: {
41: int current_length = current.length();
42: for(int k=0;k<current_length;k++)
43: {
44: sum += m[current[k]]*pow(10,current_length-k-1); //存入目前的遞增數
45: }
46: //cout<<sum<<endl;
47: if(max < sum)
48: max = sum; //存入目前最大遞增數
49: current = num[j];
50: temp=0;
51: }
52: }
53: cout<<max<<endl;
54: return 0;
55: }