"HR자바 Java Varargs - Simple Addition"의 두 판 사이의 차이

(새 문서: ==개요== ;{{subst:PAGENAME}} * {{HR자바 헤더}} {{HR자바 Advanced}} |} ---- <source lang='java'> </source> <source lang='java'> </source> <source lang='java'> </source>)
 
1번째 줄: 1번째 줄:
==개요==
==개요==
;HR자바 Java Varargs - Simple Addition
;HR자바 Java Varargs - Simple Addition
*  
* https://www.hackerrank.com/challenges/simple-addition-varargs/problem


{{HR자바 헤더}}
{{HR자바 헤더}}
10번째 줄: 10번째 줄:


<source lang='java'>
<source lang='java'>
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
</source>
</source>
<source lang='java'>
<source lang='java'>
//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 );
    }
}
</source>
</source>
<source lang='java'>
<source lang='java'>
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();
        }
    }
}
</source>
</source>

2018년 8월 15일 (수) 02:56 판

개요

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 }}