๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Etc

๋ฐฐ์—ด ๊ฐ€์šด๋ฐ ์ž๋ฆฌ ์ˆ˜์˜ ํ•ฉ

by yunamom 2022. 1. 27.
728x90
300x250

 

package G1_๋ฐฐ์—ด์‹ฌํ™”1_ํ‰๊ฐ€๋ฌธ์ œ;

import java.util.Arrays;

public class ๋ฐฐ์—ด์‹ฌํ™”1_๊ฐ€์šด๋ฐ์ˆ˜์˜ํ•ฉ_๋ฌธ์ œ {
	 /*
	  
	     array[] = {123,9876,2,43,15342};
		 [1] ์œ„ ๋ฐฐ์—ด๊ฐ๊ฐ’์˜ ๊ฐ€์šด๋ฐ ์ž๋ฆฌ์˜ ํ•ฉ์„ ๊ตฌํ•˜์‹œ์š”.	 
		 [2] ์ž๋ฆฌ์ˆ˜๊ฐ€ ์ง์ˆ˜์ธ๊ฒฝ์šฐ ์•ž๋’ค2์ž๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋”ํ•œ๋‹ค. 	
		 [3] ๊ฐ๊ฐ์˜ ํ•ฉ์„ result๋ฐฐ์—ด์— ์ €์žฅํ›„ ์ถœ๋ ฅ 	 
		  ์˜ˆ) 
		  123   ==> 2
		  9876  ==> 8 + 7
		  2     ==> 2
		  43    ==> 4 + 3
		  15342 ==> 3	 
	 */
	public static void main(String[] args) {
		 int array[] = {123,9876,2,43,15342};
		 int result[] = new int[array.length];
		 
		 for(int i=0; i<array.length; i++) {
			 int cnt = 0;
			 int a = array[i];
			 int token[] = new int[array.length];
			 while(a>0) {
				 token[cnt] = a%10;
				 a/=10; cnt++; 
			 }
			 if(cnt%2==0) {
				 result[i] = token[cnt/2]+token[(cnt/2)-1];
			 }else {
				 result[i] = token[cnt/2];
			 }
		 }
		 System.out.println(Arrays.toString(result));
	}
}
728x90
300x250

์ฝ”๋“œ