Microsoft MVP성태의 닷넷 이야기
글쓴 사람
spowner (spowner at naver.com)
홈페이지
첨부 파일
 
부모글 보이기/감추기

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            // 등록된 범위는 중복되지 않다고 가정
            var list = new List<Item>
            {
                new Item { Min = 0, Max = 1000, Rate = 0.9d },
                new Item { Min = 1001, Max = 2000, Rate = 1.1d },
                new Item { Min = 2001, Max = 3000, Rate = 1.2d },
                new Item { Min = 3001, Max = 4000, Rate = 1.2d },
                new Item { Min = 4001, Max = 5000, Rate = 1.0d }
            };

            // 목록은 항상 정렬되었다고 가정
            list.Sort((x, y) =>
            {
                return x.Max.CompareTo(y.Min);
            });

            // 목록 출력
            foreach (var i in list)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine();

            // 정렬되어 있으므로 이진검색 가능
            var value = 4500;
            var item = new Item(value);
            var index = list.BinarySearch(item, item);

            var rate = list[index].Rate;

            Console.WriteLine($"{value} => {rate}");
        }
    }

    public class Item : IComparer<Item>
    {
        public double Min { get; set; }
        public double Max { get; set; }
        public double Rate { get; set; }

        public Item()
        {
        }

        public Item(double value)
        {
            Max = value;
        }

        public override string ToString()
        {
            return $"{Min} ~ {Max} : {Rate}";
        }

        public int Compare(Item x, Item y)
        {
            var value = y.Max;

            if (value >= x.Min && value <= x.Max)
                return 0;
            else if (value < x.Min)
                return 1;
            else // else if (x.Max > value)
                return -1;
        }
    }
}








[최초 등록일: ]
[최종 수정일: 2/7/2017]


비밀번호

댓글 작성자
 




... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1258최세정4/28/201420632안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
1252popo4/21/201419994바인딩 질문입니다. [2]
1251(non...4/20/201424149(글쓴이의 요청으로 삭제합니다.) [11]
1249홍용규4/17/201424360app.config 파일 관련 질문 있습니다. [2]
1246(non...3/30/201420131(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201419499Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201419158(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201422522(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201425421인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201419560(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201419434(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201420315(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201418810(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201423389(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201421124(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201419867(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201420807(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201421213쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201422466(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201420052안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201430279안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201426052(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201420069아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201420166(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201420807(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201419297질문드립니다. [1]
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...