題目連結: https://zerojudge.tw/ShowProblem?problemid=d574
# 解題思路
開個陣列紀錄連續的次數及符號,最後再比較總數就好啦!!
(記得,記錄下的數字可能不只是一位數喔!)
# 程式碼
#include<stdio.h> | |
#include<string.h> | |
char string[10000005]; | |
int array[2][1000005]; | |
int main(){ | |
int l; | |
while(scanf("%d",&l)==1){ | |
scanf("%s",&string); | |
int i,j=0,sum=0; | |
for(i=0;i<l;i++){ | |
if(string[i]==string[i+1]) | |
sum++; | |
else{ | |
array[0][j]=string[i]; | |
array[1][j]=sum+1; | |
sum=0,j++; | |
} | |
} | |
int count=0,tmp; | |
for(i=0;i<j;i++){ | |
tmp=array[1][i]; | |
while(tmp) | |
tmp/=10,count++; | |
count++; | |
} | |
if(count>=l) | |
printf("%s\n",string); | |
else { | |
for(i=0;i<j;i++) | |
printf("%d%c",array[1][i],array[0][i]); | |
puts(""); | |
} | |
} | |
return 0; | |
} |