COAD01 - Editorial

Problem Code: COAD01

Problem Link is here

Problem Name: Beautiful Function

Contest Name: CodeAdda Practice Contest – 2 (CPC2016)

Author: Yatharth Oza (username: yatharth100)

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{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int aa=Integer.parseInt(br.readLine());
        for(int i=0;i<aa;i++){
            String n=br.readLine();
           int ans=0;
           char c[]=n.toCharArray();
           for(int j=0;j<c.length;j++){
               int ret=(add(c[j]));
               int cc=c[j];
ans+=ret;
           }
          System.out.println(ans);
        }
   }
        static int add(char c){
            int ret=0;
            int a=c;//Character.getNumericValue(c);
            if(a>=48 && a<=57)a=a-48;
            while(a>=1){
                ret=Math.max(ret,a%10);
                a=a/10;
            }
            return ret;
        }
    }
//