Phase V C++ Special Selection Competition for Primary and Secondary Schools in Putian City – Round 1: Digital Energy Converter

Problem Description

Given a positive integer n, process each digit from low to high (if the input number has less than3 digits, pad with leading zeros to3 digits, for example, the input8 is treated as008, and the input26 is treated as026), generate the energy value according to the following rules:

  • Even Position: If the current digit is even, add the square of that digit to the energy value;
  • High-Risk Odd: If the current digit is odd and greater than 5, add the cube of that digit to the energy value;
  • Normal Odd: In other cases, simply add that digit to the energy value.

Input Format

A positive integer n.

Output Format

A positive integer representing the generated energy value

Examples

Input Data 1

508

Output Data 1

069

Input Data 2

99

Output Data 2

1458

Data Range

For 60%% of the data, 1n10^9;

For 100%% of the data, 1n10^15.

Problem Analysis

This problem is also considered simple, with the main difficulty being how to loop through each digit of n and then determine the energy value based on the extracted digits.

#include<bits/stdc++.h>using namespace std;int main(){    long long n,m,k;    cin>>n;    m=0;    while(n>0)    {        k=n%10;        if(k%2==0)        {            m+=k*k;        }        else        {            if(k>5)            {                m+=k*k*k;            }            else            {                m+=k;            }        }        n=n/10;    }    if(m<10)    {        cout<<"00"<<m<<endl;    }    else if(m<100)    {        cout<<"0"<<m<<endl;    }    else    {        cout<<m<<endl;    }    return 0;}

Phase V C++ Special Selection Competition for Primary and Secondary Schools in Putian City - Round 1: Digital Energy Converter

Leave a Comment