牛客網 - PPY的字串(字串處理)
阿新 • • 發佈:2019-01-02
題目連結:https://ac.nowcoder.com/acm/contest/322/E
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
Siry特別喜歡數學, 在他很小的時候他就對數字特別感興趣, 他喜歡念數字。
具體念法是這樣的: 給你一個數字, 依次念出每個數字有幾個相鄰(Siry會大聲說出a個b, c個d...), 組合起來形成一個新的數字。
如:
2331的念法就是1個2,2個3,1個1, 形成的新數字就是122311。 再念一次就是1個1,2個2,1個3, 2個1, 形成的數字是11221321。
現在Siry大聲的念出了第一次的數字x, Siry總共想要念n次, 你能快速的知道第n次的數字是多少嗎?
輸入描述:
每行輸入兩個數字x,n。
1≤ x≤ 109,1≤ n≤ 30
輸出描述:
輸出一行,包括第n個數字的位數和這個數字。 位數和數字之間用空格隔開。
輸入
222 2
輸出
2 32
說明
第一次念出的數字是222,第二次就會念3個2,形成的數字就是32,位數是兩位數。
解題思路
類似於伯爵說序列,類似題:https://blog.csdn.net/lzyws739307453/article/details/83582870,都是一些關於字串處理的題。
#include <stdio.h> #include <string.h> char str[10010], tmp[10010]; void edge() { int cnt = 1, idx = 0; int len = strlen(str); for (int i = 1; i < len; i++) { if (str[i] != str[i - 1]) { tmp[idx++] = cnt + '0'; tmp[idx++] = str[i - 1]; cnt = 1; } else cnt++; } tmp[idx++] = cnt + '0'; tmp[idx++] = str[len - 1]; tmp[idx] = '\0'; } int main() { int n; while (~scanf("%s%d", str, &n)) { for (int i = 1; i < n; i++) { edge(); strcpy(str, tmp); } printf("%d %s\n", strlen(str), str); } return 0; }