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

(새 문서: ==개요== ;{{subst:PAGENAME}} * {{HR자바 헤더}} {{HR자바 Advanced}} |} ---- <source lang='java'> </source> <source lang='java'> </source> <source lang='java'> </source>)
 
잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 하나는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;HR자바 Java Varargs - Simple Addition
;HR자바 Java Varargs - Simple Addition
*  
* https://www.hackerrank.com/challenges/simple-addition-varargs/problem


{{HR자바 헤더}}
{{HR자바 헤더}}
9번째 줄: 9번째 줄:
----
----


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

2020년 11월 2일 (월) 02:51 기준 최신판

개요[ | ]

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