//You are given a non-negative integer number represented as a string of digits, and a positive integer k. Your task is to perform the following algorithm:
//If the length of the number is less than or equal to k, end the algorithm. Otherwise proceed to the next steps;
// Split the number into groups of k digits in each. The last group may contain less than k digits;
//Add up the digits in each group, and concatenate these results in the same order;
//Replace the number with the result of step 3 and go back to step 1.
Return the resulting string number after the algorithm ends.
It is guaranteed that the algorithm will end at step 1 at some point.
Example
For number = "1111122222" and k = 3, the output should be reduceTheNumber(number, k) = "132".
The number has length 10 > k, so we proceed to the next steps;
We split number into groups of size k = 3: [111, 112, 222, 2]. Note that the last group is not full and has only one digit;
We add up the digits in each group, and concatenate the results. Thus, the new value of number becomes "3462";
We start again with step 1: number has length 4 > k, so we proceed to the next steps;
We split number into groups of size k = 3: [346, 2];
We add up the digits in each group, and concatenate the results, the new number is "132";
The length of the new number is 3 = k, so the algorithm ends with "132" as a result.
For number = "1111122222" and k = 5, the output should be reduceTheNumber(number, k) = "510".
We successfully proceed step 1 and split the number into groups of k = 5 digits on step 2: [11111, 22222];
We add up the digits in each group, and concatenate the results, and the number becomes "510";
The new number has length 3 < k, so we finish the algorithm and return the string "510".
Input/Output
[execution time limit] 4 seconds (js)
[input] string number
A string of digits representing the initial number. It is guaranteed that the given string contains only digits and doesn’t contain leading zeros (unless the number is 0).
Guaranteed constraints:
1 ≤ number.length ≤ 1000.
[input] integer k
The size of the groups number should be split into.
Guaranteed constraints:
3 ≤ k ≤ 1000.
[output] string

Respuesta :

Answer:

ALGORITHM READ_NUMBER()

1.       SET NUM := 0 [INITIALLY SET THE VALUE OF NUM TO 0]

2.       WRITE : “Input a Number” [DISPLAY A MESSAGETO USER]

3. READ NUM [STORE THE VALUE TO NUM]

4.       IF NUM<0 [IF VALUE OF NUM IS -VE THEN MULTIPLY IT BY -1]

5.       NUM := NUM * (-1)

6.       [END OF IF]

7.       RETURN NUM [RETURN THE NUMBER]

1. ALGORITHM DIGIT_COUNT(NUM)

SET COUNT := 0

SET R:=0

REPEAT WHILE(NUM != 0)

R := NUM MOD 10

COUNT : = COUNT+1

NUM := NUM/10

[END OF LOOP]

RETURN COUNT

2. ALGORITHM DIGIT_ODDCOUNT(NUM)

SET COUNT := 0

SET R:=0

REPEAT WHILE(NUM != 0)

R := NUM MOD 10

IF (R MOD 2 != 0)

COUNT : = COUNT+1

[END OF IF]

NUM := NUM/10

[END OF LOOP]

RETURN COUNT

3. ALGORITHM SUM_OF_DIGITS(NUM)

SET SUM := 0

SET R:=0

REPEAT WHILE(NUM != 0)

R := NUM MOD 10

SUM : = SUM+R

NUM := NUM/10

[END OF LOOP]

RETURN SUM

C++ PROGRAM

#include<iostream>

using namespace std;

//method to count the number of digits

int digit_count(int num)

{

  int count=0; //set count to 0

  int r;

  //loop will continue till number !=0

  while(num!=0)

  {

     r=num%10; //find the individual digits

     count++; //increase the count

     num=num/10; //reduce the number

 }

 return count; //return the count

}

int odd_digit_count(int num)

{

  int count=0;//set count to 0

  int r;

  //loop will continue till number !=0

  while(num!=0)

  {

     r=num%10;//find the individual digits

     if(r%2!=0) //check for odd digits

     count++;//increase the count

     num=num/10;//reduce the number

 }

 return count;//return the count

}

int sum_of_digits(int num)

{

  int sum=0;

  int r;

  //loop will continue till number !=0

  while(num!=0)

  {

     r=num%10;//find the individual digits

     sum=sum+r;//find the sum of digits

     num=num/10;//reduce the number

 }

 return sum;//return sum

}

//input method

int input()

{

 int x;

 cout<<endl<<"Enter a number";

 cin>>x;//read the number

 if(x<0) //if -ve then turn it to +ve

 x=x*-1;

 return x; //return the number

}

//driver program

int main()

{

 int n,dc,odc,sod;

n=input(); //call to input()

 dc=digit_count(n); //call to digit_count()

 odc=odd_digit_count(n); //call to odd_digit_count()

 sod=sum_of_digits(n);//call to sum_of_digits()

 //display the details

 cout<<endl<<"The positive number is : "<<n;

 cout<<endl<<"The number of digits in "<<n<<" is "<<dc;

 cout<<endl<<"The number of odd digits in "<<n<<" is "<<odc;

 cout<<endl<<"The sum of digits of "<<n<<" is "<<sod;

}Explanation: