HR자바 Java Varargs - Simple Addition

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:51 판 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

개요[ | ]

HR자바 Java Varargs - Simple Addition
해커랭크 Java
# 문제 비고
HR자바 Advanced e
53 HR자바 Java Varargs - Simple Addition
54 HR자바 Java Reflection - Attributes
55 HR자바 Can You Access?
56 HR자바 Prime Checker
57 HR자바 Java Factory Pattern
58 HR자바 Java Singleton Pattern
59 HR자바 Java Visitor Pattern
60 HR자바 Java Annotations
61 HR자바 Covariant Return Types
62 HR자바 Java Lambda Expressions
63 HR자바 Java MD5
64 HR자바 Java SHA-256

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
//Write your code here
class Add {
    void add(int... nums) {
        int sum = 0;
        for(int i=0; i<nums.length; i++) {
            if( i!=0 ) System.out.print( "+" );
            System.out.print( nums[i] );
            sum += nums[i];
        }
        System.out.println( "=" + sum );
    }
}
public class Solution {
    public static void main(String[] args) {
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int n1=Integer.parseInt(br.readLine());
            int n2=Integer.parseInt(br.readLine());
            int n3=Integer.parseInt(br.readLine());
            int n4=Integer.parseInt(br.readLine());
            int n5=Integer.parseInt(br.readLine());
            int n6=Integer.parseInt(br.readLine());
            Add ob=new Add();
            ob.add(n1,n2);
            ob.add(n1,n2,n3);
            ob.add(n1,n2,n3,n4,n5);	
            ob.add(n1,n2,n3,n4,n5,n6);
            Method[] methods=Add.class.getDeclaredMethods();
            Set<String> set=new HashSet<>();
            boolean overload=false;
            for(int i=0;i<methods.length;i++) {
                if(set.contains(methods[i].getName())) {
                    overload=true;
                    break;
                }
                set.add(methods[i].getName());
            }
            if(overload) {
                throw new Exception("Overloading not allowed");
            }
        }
	catch(Exception e) {
            e.printStackTrace();
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}