輸入一個字元串,內有數字和非數字字元。例如:a123x456 17960 302tab5876。將其中連續的數字作為一個整數,依次存放到一維數組a中,例如123放在a[0],456放在a[1]……統計共有多少個整數,並輸出這些數。

  • 2022 年 4 月 14 日
  • 筆記

題目內容:輸入一個字元串,內有數字和非數字字元。例如:a123x456 17960 302tab5876。將其中連續的數字作為一個整數,依次存放到一維數組a中,例如123放在a[0],456放在a[1]……統計共有多少個整數,並輸出這些數。

輸入格式:輸入一個字元串(允許空格)。

輸出格式:第1行輸出個數,第2行輸出多個整數,用空格分隔。

輸入樣例:a123X456  7689?89njmk32lnk123

輸出樣例:

6

123 456 7689 89 32 123

解決思路:
最近同時在學C++和python,正好python又學到正則表達式,

所以我想試試怎麼用C++的正則,這樣可以大大減少程式碼數目!

寫完之後在VScode里的結果都能對上,但是貼到OJ之後程式運行錯誤。

而且後面搜了一圈,沒有像我一樣用正則做這道題的,所以分享一下程式碼~

 

 1 #include <iostream>
 2 #include <string>
 3 #include <regex>
 4 using namespace std;
 5 int main()
 6 {
 7     regex pattern("\\d+");
 8     string content;
 9     getline(cin, content);
10     smatch result;
11     int count = 0;
12     auto begin = content.cbegin();
13     auto end = content.cend();
14     while (regex_search(begin, end, result, pattern))
15     {
16 
17         count += 1;
18 
19         begin = result[0].second;
20     }
21     cout << count << endl;
22     begin = content.cbegin();
23 
24     while (regex_search(begin, end, result, pattern))
25     {
26 
27         cout << result[0];
28         begin = result[0].second;
29         if (begin != end)
30             cout << " ";
31     }
32     return 0;
33 }

👆這個是沒有末尾空格的

下面這個是有末尾空格的

 1 #include <iostream>
 2 #include <string>
 3 #include <regex>
 4 using namespace std;
 5 int main()
 6 {
 7     regex pattern("\\d+");
 8     string content;
 9     getline(cin, content);
10     smatch result;
11 
12     int count = 0;
13     int n = result.size();
14     string m[n];
15 
16     auto begin = content.cbegin();
17     auto end = content.cend();
18     while (regex_search(begin, end, result, pattern))
19     {
20 
21         count += 1;
22         begin = result[0].second;
23     }
24 
25     cout << count << endl;
26     begin = content.cbegin();
27     while (regex_search(begin, end, result, pattern))
28     {
29 
30         cout << result[0] << " ";
31         begin = result[0].second;
32     }
33 
34     return 0;
35 }

 最後運行正確的解決方法:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 void count_digit(char *str)
 6 {
 7     int digit[32], count = 0;
 8     char *ptr = str;
 9     int i = 0, str_len = strlen(str);
10     while (i < str_len)
11     {
12         if (*(ptr + i) >= '0' && *(ptr + i) <= '9')
13         {
14             int len = 1; //用於統計連續數字的個數
15             while (*(ptr + i + len) >= '0' && *(ptr + i + len) <= '9' && (i + len) < str_len)
16             { //找出從當前位置連續數字的個數
17                 len++;
18             }
19             int sum = *(ptr + i + len - 1) - '0'; //先獲取個位數的數據
20             int unit = 1;                         //每一位的單位,從十位開始每次乘以10作為單位
21             for (int j = len - 2; j >= 0; j--)
22             { //從右往左逐個處理
23                 unit *= 10;
24                 sum += (*(ptr + i + j) - '0') * unit;
25             }
26             digit[count++] = sum;
27             i += len; // i需要加上len的長度,越過這個數字,防止一個連續數字中的字元重複處理
28             continue;
29         }
30         i++;
31     }
32     cout << count << endl;
33     for (int i = 0; i < count; i++)
34     {
35 
36         cout << digit[i] << ' ';
37     }
38 
39     return;
40 }
41 int main()
42 {
43     char buf[1024] = {0};
44     cin.get(buf, 1024);
45     count_digit(buf);
46     return 0;
47 }