?? 游程.txt
字號:
輸入序列{1,0,0,0,0,1,1},輸出為{11,04,12}.后面輸出結果的含義是,"11"表示1的個數為1,"04"表示后面連續0的個數為4,"11"表示最后面出現連續1的次數為2次
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
string itostring(int x)
{
std::ostringstream o;
if (o << x)
{
return o.str();// 把x轉換成1位字符輸出
}
return 0;
}
int strlen(char str[])
{
int i,leng;
i=0;
while(str[i]!='\0')
i=i+1;
leng=i;
return(leng);
}
void main()
{
int m=0,Len,j,n,count0=0,count1=0,b[100];
cout<<"請輸入一些二進制數據:\n";
char data[100];
cin >>data;
Len=strlen(data);
for(m=0;m<Len;m++)
{
if(data[m]=='1')
{
for(;data[m]=='1';m++)
{
count1++;
}
cout<<"S1"<<itostring(count1);
count1=0;
m--;
}
else
{
for(;data[m]=='0';m++)
{
count0++;
}
cout<<"S0"<<itostring(count0);
count0=0;
m--;
}
}
}
運行結果:
請輸入一些二進制數據:
111100001110011
S14S04S13S02S12 Press any key to continue
注意:
我們考慮0、1序列中連續的1或0的個數超過 10個,如果超過會得出這樣的結果:
請輸入一些二進制數據:
1111100000000000011111111111100000
S15S012S112S05 Press any key to contin
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -