JSONObject obj = new JSONObject();

obj.put("id", result.getId());

obj.put("theme", result.getTheme());

obj.put("title", result.getTitle());

obj.put("address", result.getAddress());

obj.put("memo", result.getMemo());

obj.put("imgpath", result.getImgpath());


{"address":"서울 종로구 우정국로2길 21",

 "imgpath":"d:\\mapstarImg\\testId_20171229075929343.jpg",

 "memo":"눈치안보임",

 "theme":"스터디까페",

 "id":"testId",

 "title":"마이크임팩트스퀘어"}


이런식으로 {"키:벨류", "키":"벨류"} 로 담깁니다. 


JSONArray arr = new JSONArray();

arr.add(obj); 


[{"address":"서울 종로구 우정국로2길 21",

 "imgpath":"d:\\mapstarImg\\testId_20171229075929343.jpg",

 "memo":"눈치안보임",

 "theme":"스터디까페",

 "id":"testId",

 "title":"마이크임팩트스퀘어"}]


담아논 JSONObject를 JSONArray 안에 넣어줍니다. 

[ {"키:벨류", "키":"벨류"} ]  

//Ajax JSON response

response.setContentType("text/html; charset=utf-8"); 

PrintWriter writer = response.getWriter();

writer.print(arr.toString());

writer.flush();

writer.close()


response로 보냅니다.


Ajax에서 받아 JSON.parse해서 받습니다.
entry.키 값으로 벨류를 가져와서 각자 활용 합니다.

success: function (datas) {
var dataResult = JSON.parse(datas);
$.each(dataResult, function(index, entry) {
alert("entry::"+entry.id);
alert("entry::"+entry.title);
alert("entry::"+entry.address);
alert("entry::"+entry.theme);
alert("entry::"+entry.memo);
alert("entry::"+entry.imgpath);
});
},




Not allowed to load local resource


브라우저에서는 사용자 로컬에 접근할 수 없음


  • 이클립스에서 JSP로 개발시 파일경로 설정방법 (http://blog.itthis.me/28) 

<img src="${pageContext.request.contextPath}/resources/img/profile.jpg">

<img src="/resources/img/profile.jpg">

http://localhost:8082/resources/img/profile.jpg

비교 http://localhost:8082/mapstar/insert

  •  resources mapping

  • <Context docBase="d:\test_img" path="/local_img" reloadable="true"></Context>    > 시스템 경로 사용 시 

  • java의 File에서 파일을 읽기/쓰기 할때는 제일 쉬운방법은 절대경로(웹경로 x)를 넣는 것이고,

html등의 웹페이지에서 이미지 출력시에는 context의  상대경로/절대경로를 넣어야 합니다.

그리고, 이미지의 경우, WEB-INF 하위로 두게 되면 일반 웹URL로 인식안되니

ROOT 하위로 빼놓으셔야 합니다  > 프로젝트 안으로 

(https://okky.kr/article/317040?note=1043455)

  • Web Browser를 통해서 이미지를 보고자 할때에는

Http 서비스가 가능한 위치여야 합니다. 

일반적으로 Http Server에서 이미지파일이나 스크립트, 또는 css 등의 파일에 접근하기 위해서는

해당 경로가 Http URL로 지정이 되어 있어야 하는거죠...

예로 Tomcat에서 개발을 한경우 Context 로 지정된 폴더및 그 하위의 모든 폴더 들은

Http URL로 접근이 가능하게 됩니다. 

님처럼 특정 폴더에 파일을 올리시고 그것을 Web을 통해서 서비스를 하시려면..

Servlet 단에서 response 로 파일을 보내시면 됩니다. > 아예 파일로 response

해당 예제들은 구글에서 검색하시면 쉽게 찾을수 있을 거예요..

(https://okky.kr/article/417238)

  • 접금권한 자체가 웹 루트부터 시작 하니까요.
    위 글처럼 하시려면직접 파일을 읽어서 스트림으로 내려주면 됩니다.

  (https://okky.kr/article/237812)

정렬(sort) 알고리즘 


1. 선택 정렬 : 가장 작은 데이터를 찾아 가장 앞의 데이터와 교환해 나가는 방식 

 

2. 삽입 정렬 : 앞에서 부터 이미 정렬된 부분의 적절한 위치에 삽입해 가며 정렬 

 

3. 버블 정렬 : 이웃한 데이터들을 비교하며 가장 큰 데이터를 가장 뒤로 보내며 정렬하는 방식

                     (맨 뒤가 정해지면 다시 처음부터)

 

버블 정렬 알고리즘 ↓

 

 

 

 

 

Kangaroo     

                           

There are two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location  and moves at a rate of  meters per jump. The second kangaroo starts at location  and moves at a rate of  meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time?

Input Format

A single line of four space-separated integers denoting the respective values of , and .

Constraints

Output Format

Print YES if they can land on the same location at the same time; otherwise, print NO.

Note: The two kangaroos must land at the same location after making the same number of jumps.

Sample Input 0

0 3 4 2

Sample Output 0

YES

Explanation 0

The two kangaroos jump through the following sequence of locations:

Thus, the kangaroos meet after  jumps and we print YES.

Sample Input 1

0 2 5 3

Sample Output 1

NO

Explanation 1

The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo's starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.

 

 



풀이 과정

"양의 x좌표 상에 두 시작점이 양의 방향으로 각각 일정한 값 만큼 움직일 때 두 점이 만나는 시점의 유무 찾기" 

방정식 해를 찾으려 괜한 반복문을 범위 설정도 명확히 하지 못한 채 돌렸다. 막연히 회전수를 키워서 답은 통과했지만 바람직한 해결책은 아니다.


풀이 비교 

* v1, v2 비교를 정수 값 체크와 한꺼번에 할 수 있었다.

* 만나는 시점 체크도 정확히 몇 번째에 만나는 가를 묻는 것이 아니기 때문에 방정식을 정리하여 정수 유무(나머지 0)만 체크해도 됬었다.

이렇게 간단한 것을!!! 



  

  public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int x1 = in.nextInt();

        int v1 = in.nextInt();

        int x2 = in.nextInt();

        int v2 = in.nextInt();

        

        if ( v1>v2 && (x2-x1)%(v1-v2)==0 )

            System.out.println("YES");

        else

            System.out.println("NO");

    }




Apple and Orange




Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where  is the start point and  is the end point. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point  and the orange tree is at point .

Apple and orange(2).png

When a fruit falls from its tree, it lands  units of distance from its tree of origin along the -axis. A negative value of  means the fruit fell  units to the tree's left, and a positive value of  means it falls  units to the tree's right.

Given the value of  for  apples and  oranges, can you determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )? Print the number of apples that fall on Sam's house as your first line of output, then print the number of oranges that fall on Sam's house as your second line of output.

Input Format

The first line contains two space-separated integers denoting the respective values of  and 
The second line contains two space-separated integers denoting the respective values of  and 
The third line contains two space-separated integers denoting the respective values of  and 
The fourth line contains  space-separated integers denoting the respective distances that each apple falls from point 
The fifth line contains  space-separated integers denoting the respective distances that each orange falls from point .

Constraints

Output Format

Print two lines of output:

  1. On the first line, print the number of apples that fall on Sam's house.
  2. On the second line, print the number of oranges that fall on Sam's house.

Sample Input 0

7 11
5 15
3 2
-2 2 1
5 -6

Sample Output 0

1
1

Explanation 0

The first apple falls at position 
The second apple falls at position 
The third apple falls at position 
The first orange falls at position 
The second orange falls at position 
Only one fruit (the second apple) falls within the region between  and , so we print  as our first line of output. 
Only the second orange falls within the region between  and , so we print  as our second line of output.


 

+ Recent posts