String compression algorithm
To input a string of characters, please write a string compression program to compress the repeated letters in the string and output the compressed string.
For example:
aac compressed to 1ac
xxxxyyyyyyzbbb compressed to 3x5yz2b
Code:
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int main(){
string str;
getline(cin,str);
for (int i = 0; i < str.size(); ++i) {
int cnt =0;
while (str[i] == str[i+1]){
++i;
++cnt;
}
if(cnt != 0)
{
cout<<cnt;
}
cout<<str[i];
}
return 0;
}
Find the number of 0 at the end of the result of the expression f(n)
Input a natural number n and find the expression f(n) = 1!2!3!..n! How many consecutive 0s are there at the end of the result?
Enter description:
Natural number n
Output description:
The number of consecutive 0s at the end of f(n)
Input example 1:
11
Output example 1:
9
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int main(){
string str;
getline(cin,str);
for (int i = 0; i < str.size(); ++i) {
int cnt =0;
while (str[i] == str[i+1]){
++i;
++cnt;
}
if(cnt != 0)
{
cout<<cnt;
}
cout<<str[i];
}
return 0;
}