toj 2971 Rotating Numbers

2971.   Rotating Numbers
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 262    Accepted Runs: 86



As CSE students you are already familiar with the binary rotation. Now you should try to do something different like decimal number rotation. It is simple to rotate a decimal number for a single digit to the left to perform left rotation - just place the rightmost digit to the leftmost position as in the following:

12345 => 51234

If we gradually perform the left rotation operation for more and more times we have

51234 => 45123

45123 => 34512 and this way.

But you should be careful to remember that leading zeros in decimal numbers does not carry any significance. Therefore, you need not to add leading zeros.

 

Input

Each input line consist of an integer M and another integer N. M denotes the number to be left rotated and N indicates how many times to rotate. Both M and N will fit 32-bit integers. Input is terminated by end of file.

 

Output

The output line contains the number after left rotation is performed on M for N times.

 

Sample Input

12345 3

1800576 5

10002 5

Sample Output

34512

57618

12

_____________________________________________________________________

Samina Azad (CSE-03)



Source: CUET Easy Contest
Submit   List    Runs   Forum   Statistics

#include  < iostream >
#include 
< string >
#include 
< cmath >
using   namespace  std;
int  Get( string  str)
{
    
int  sum = 0 ;
    
int  i,len = str.length(),k = 0 ;
    
for (i = len - 1 ;i >= 0 ;i -- )
    {
        sum
+= ( int )((str[i] - ' 0 ' ) * pow( 10.0 ,k ++ ));
    }
    
return  sum;
}
int  main()
{
    
string  str;
    
int  n,i,len;
    
while (cin >> str)
    {
        cin
>> n;
        len
= str.length();
        
int  num = 0 ;
        
for (i = 0 ;i < len;i ++ )
            
if (str[i] == ' 0 ' )
                num
++ ;
            
int  ss  =   0  , kk;
        
int  j = len - 1 ,k;
        
int   t  =  len  -  num;
        
bool  mark = false ;
        
for (i = 0 ;i < n;i ++ )
        {
            
if (num  ==   0 )
            {
                ss
= i;
                mark
= true ;
                
break ;
            }
            
char  ch = str.at(j);
            str.erase(j);
            
if (ch != ' 0 ' )
                str
= ch + str;
            
else
            {
                j
-- ;
                num
-- ;
            }
        }
        
if  ( ! mark)
          printf(
" %d\n " ,Get(str));
        
else
        {
          kk 
=  n  -  ss;
          ss 
=  kk  %  t;
          
for  (i  =   1  ; i  <= ss ;  ++  i)
          {
              
char  temp = str.at(t - 1 );
              str.erase(t
- 1 );
              str
= temp + str;
          }
          printf(
" %d\n " ,Get(str));
        }
    }
    
return   0 ;
}

你可能感兴趣的:(number)