 Problem Name: Beautiful Function

Contest Name: CodeAdda Practice Contest – 2 (CPC2016)

Difficulty: Easy

PREREQUISITES: Math

Problem:

One string is given as input in each test case containing ‘a’ to ‘z’ and ‘0’ to ‘9’. Your task is to find all alphabet’s ASCII value and from that you need to find highest digit of it (e.g-> for ‘d’ ASCII is ‘100’ so ‘1’ is highest digit from all 3 digits) and take directly that number for ‘0’ to ‘9’. At last you need to print sum of all digits which you have found for all character as output.

Explanation:

For this question we can traverse whole string and abstract their ASCII one by one. Now we have to check if our ASCII is >= 48 and <=57 then we are taking directly 0 to 9 respectively. For all other ASCII value we need to find highest digit from it. We can do this by travelling one by one digit in value. For simplicity we can make one swich case in with we can return directly the highest number.

Solution:

``````/*Solution is in JAVA and file name is Main.java */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException{
for(int i=0;i<aa;i++){
int ans=0;
char c[]=n.toCharArray();
for(int j=0;j<c.length;j++){
int cc=c[j];
ans+=ret;
}
System.out.println(ans);
}
}