반응형
<template>
  <div>
    <div>
      {{ fruits }}
    </div>

    <div>
      <ul>
        <li v-for="(fruit, index) of fruits" :key="index">
          {{ index * index * index }} {{ fruit }}
        </li>
      </ul>

      <h2 v-for="(value, key, index) in user" :key="key">
        {{ index }}, {{ key }} : {{ value }}
      </h2>

      <p v-for="n in 5" :key="n">{{ n }}</p>

      <!-- 중첩반복문 -->
      <div v-for="(animal, animalIndex) in animals" :key="animalIndex">
        <h2>Animal ===> {{ animal.name }}</h2>
        <h3>food</h3>
        <ul>
          <li v-for="(food, foodIndex) in animal.food" :key="foodIndex">
            {{ animal.name }} has {{ foodIndex }} : {{ food }}
          </li>
        </ul>
      </div>

      <div v-for="i in 9" :key="i">
        <div v-for="j in 9" :key="j">i:{{ i }}, j:{{ j }} = > {{ i * j }}</div>
        <div>---</div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      // list
      fruits: ["banana", "strawberry", "apple", "melon"],

      //object
      user: {
        name: "scalper",
        age: 100,
        job: "programmer",
      },

      //object list
      animals: [
        { name: "monkey", size: "medium", food: ["banana", "apple"] },
        { name: "lion", size: "big", food: ["deer", "cow"] },
        { name: "rat", size: "small", food: ["cheese", "rice"] },
      ],
    };
  },
};
</script>

<style></style>
반응형

'Web > Vue' 카테고리의 다른 글

[Vue] 메서드  (0) 2022.06.24
[Vue] 조건부 리스트 렌더링  (0) 2022.06.24
[Vue] 조건부 렌더링  (0) 2022.06.23
[Vue] v-if v-show 차이  (0) 2022.06.23
[Vue] 클래스 바인딩  (0) 2022.06.23
반응형
<template>
  <div>
    <!-- v-once -->
    <!-- <h1>Hello {{ user.name }}</h1> -->
    <!-- <h1 v-once v-text="user.name"></h1>
    <h1 v-text="user.name"></h1>
    <input type="text" v-model="user.name" /> -->

    <!-- if / else -->
    <!-- <h1 v-if="showname">My name is {{ user.name }}</h1>
    <h2 v-else>이름을 보여줄 수 없습니다.</h2>
    <h2 v-if="user.age > 20">당신은 성인입니다.</h2>
    <h2 v-else-if="user.age > 14 && user.age < 20">당신은 청소년 입니다.</h2>
    <h2 v-else>당신은 어린이 입니다.</h2> -->

    <h2 v-if="!showName">{{ user.name }} IF</h2>
    <h2 v-show="!showName">{{ user.name }} Show</h2>

    <ul>
      <template v-if="question === 'frontend'">
        <li>HTML은 재미있나요?</li>
        <li>CSS은 재미있나요?</li>
        <li>Javascript은 재미있나요?</li>
      </template>
      <template v-else>
        <li>Java은 재미있나요?</li>
        <li>Python은 재미있나요?</li>
        <li>C#은 재미있나요?</li>
      </template>
    </ul>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      question: "frontend",
      showName: true,
      user: {
        name: "scalper",
        age: 12,
        job: "programmer",
      },
    };
  },
};
</script>

<style></style>
반응형

'Web > Vue' 카테고리의 다른 글

[Vue] 메서드  (0) 2022.06.24
[Vue] 조건부 리스트 렌더링  (0) 2022.06.24
[Vue] 리스트 렌더링  (0) 2022.06.23
[Vue] v-if v-show 차이  (0) 2022.06.23
[Vue] 클래스 바인딩  (0) 2022.06.23
반응형
<h2 v-if="!showName">{{ user.name }} IF</h2>
<h2 v-show="!showName">{{ user.name }} Show</h2>

v-show

 

v-if는 해당 명령을 실행할 때마다 보여줄 값을 렌더링하는 반면

v-show는 최초에 한번만 렌더링하고 보여주는 여부만 바뀐다.

 

만약 보여주어야 하는 양이 방대하고 크기가 클 경우에

v-if는 보여줄 때마다 렌더링을 해야하지만,

v-show를 이용하면 한 번만 렌더링하기 때문에 유리하다.

 

반응형

'Web > Vue' 카테고리의 다른 글

[Vue] 메서드  (0) 2022.06.24
[Vue] 조건부 리스트 렌더링  (0) 2022.06.24
[Vue] 리스트 렌더링  (0) 2022.06.23
[Vue] 조건부 렌더링  (0) 2022.06.23
[Vue] 클래스 바인딩  (0) 2022.06.23
반응형
<template>
  <div>
    <h1>Hello Vue!</h1>
    <h2 class="line-through">line-through</h2>
    <h2 v-bind:class="textDecroation" class="text-red">line-through</h2>
    <h2 :class="isDone === true ? 'line-through' : 'highlight'">
      line-through
    </h2>

    <h2
      :class="{
        highlight: isDone === false,
        'text-red': username === 'scalper',
      }"
    >
      Object형태의 동적 클래스
    </h2>

    <h2
      :class="[
        isDone === true ? 'line-through' : 'highlight',
        username === 'scalper' ? 'text-red' : 'text-green',
      ]"
    >
      Array 형태의 동적 클래스 부여
    </h2>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      username: "scaalper",
      isDone: true,
      textDecroation: "line-through",
    };
  },
};
</script>

<style>
.text-red {
  color: red;
}

.text-green {
  color: green;
}

.highlight {
  font-weight: bold;
  background: pink;
}

.line-through {
  text-decoration: line-through;
}
</style>
반응형

'Web > Vue' 카테고리의 다른 글

[Vue] 메서드  (0) 2022.06.24
[Vue] 조건부 리스트 렌더링  (0) 2022.06.24
[Vue] 리스트 렌더링  (0) 2022.06.23
[Vue] 조건부 렌더링  (0) 2022.06.23
[Vue] v-if v-show 차이  (0) 2022.06.23
반응형

https://www.acmicpc.net/problem/2512

 

2512번: 예산

첫째 줄에는 지방의 수를 의미하는 정수 N이 주어진다. N은 3 이상 10,000 이하이다. 다음 줄에는 각 지방의 예산요청을 표현하는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 값들은 모두 1 이상

www.acmicpc.net

 

#include <bits/stdc++.h>
using namespace std;

int n,m, sum, maxx;
vector<int> v;


bool decide(int c){
    long long cnt = 0;

    for(int i=0; i<n; i++){
        if(v[i] < c){
            cnt += v[i];
        }
        else cnt += c;
    }

    return cnt <= m;
}

void Input(){
    cin >> n;
    for(int i=0; i<n; i++){
        int t;
        cin >> t;
        v.push_back(t);
        sum+=t;
        maxx = max(maxx, t);
    }
    cin >> m;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    Input();


    if(sum<=m){
        cout << maxx <<'\n';
    }
    else {
        int lo = 1;
        int hi = 1e9;

        while (lo <= hi) {
            int mid = (lo + hi) / 2;
            if (decide(mid)) {
                lo = mid + 1;
            } else hi = mid - 1;
        }

        cout << hi << '\n';
    }
    return 0;
}
반응형
반응형

https://www.acmicpc.net/problem/2110

 

2110번: 공유기 설치

첫째 줄에 집의 개수 N (2 ≤ N ≤ 200,000)과 공유기의 개수 C (2 ≤ C ≤ N)이 하나 이상의 빈 칸을 사이에 두고 주어진다. 둘째 줄부터 N개의 줄에는 집의 좌표를 나타내는 xi (0 ≤ xi ≤ 1,000,000,000)가

www.acmicpc.net

1. 문제설명

공유기를 설치해야하는 최대 거리를 구하기 위해

이분 탐색법으로 가장 작은 거리와 가장 먼 거리를 확인하면서

주어진 조건으로 공유기를 설치할 수 있는 지 확인해야 하는 문제이다.

 

binary search를 활용한 Decide problem 문제.

 

 

 

2.문제풀이코드

#include <bits/stdc++.h>
using namespace std;

int n,c;
vector<int> house;


//O(N)
bool decide(int dist){
    int cnt = 1;
    int x = house[0];

    for(int i=1; i<n; i++){
        if(house[i] - x >= dist){
            x = house[i];
            cnt++;
        }
    }
    return cnt >= c;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> c;

    for(int i=0; i<n; i++){
        int t;
        cin >> t;
        house.push_back(t);
    }
    //O(NlogN)
    sort(house.begin(), house.end());

    
    int lo = 0;
    int hi = 1e9;

    //O(NlogN)
    while(lo<=hi){
        int mid = (lo+hi)/2;
        if(!decide(mid)){
            hi = mid-1;
        }
        else{
            lo = mid+1;
        }
    }

    cout << hi << '\n';

    return 0;
}

백준 2110번 공유기 설치 C++

반응형
반응형

https://leetcode.com/problems/best-position-for-a-service-centre/submissions/

 

Best Position for a Service Centre - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

const double EPSILON = 1e-6;

class Solution {
public:
    double distance(double cx, double cy, int x, int y){
        return sqrt((cx-x)*(cx-x) + (cy-y)*(cy-y));
    }
    
    
    double distanceSum(const vector<vector<int>>& positions, double x, double y){
        double res = 0;
        
        for(auto& v : positions)
            res += distance(x, y, v[0], v[1]);
        
        return res;
    }
    
    
    double findMax(const vector<vector<int>>& positions, double x){
        double y1 = 0, y2 = 100;
        
        while(abs(y2-y1)>EPSILON){
            double midy1 = (2*y1 + y2)/3;
            double midy2 = (y1 + 2*y2)/3;
            double z1 = distanceSum(positions, x, midy1);
            double z2 = distanceSum(positions, x, midy2);
            
            if(z1 > z2) y1 = midy1;
            else y2 = midy2;
        }
        
        return distanceSum(positions, x, (y1+y2)/2);
        
    }
    
    double getMinDistSum(vector<vector<int>>& positions) {
        double x1 = 0, x2 = 100;
        
        while(abs(x1-x2)>EPSILON){
            double midx1 = (2*x1+x2)/3;
            double midx2 = (x1+2*x2)/3;
            double z1 = findMax(positions, midx1);
            double z2 = findMax(positions, midx2);
            
            if(z1 > z2){
                x1 = midx1;
            }
            else{
                x2 = midx2;
            }
        }
        return findMax(positions,(x1 + x2) / 2);
    }
};
반응형
반응형

https://leetcode.com/problems/split-array-largest-sum/submissions/

 

Split Array Largest Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
public:
    bool decide(vector<int>& nums, int k, int m){
        int cnt = 0;
        
        int add = 0;
        for(int i=0; i<nums.size(); i++){
            add += nums[i];
            
            if(add==k){
                add = 0;
                cnt++;
            }
            else if(add>k){
                add = nums[i];
                cnt++;
            }
        }
        
        if(add>0) cnt++;
        
        
        return cnt<=m;
    }
    
    
    
    int splitArray(vector<int>& nums, int m) {
        int lo = 0;
        int hi = 0;
        int maxx = 0;
        for(int i=0; i<nums.size();i++){
            hi += nums[i];
            maxx = max(maxx, nums[i]);
        }
        
        
        while(lo<=hi){
            int mid = (lo+hi)/2;
            
            if(mid>=maxx && decide(nums, mid, m)){
                hi = mid-1;
            }
            else{
                lo = mid+1;
            }
        }
        
        return lo;
    }
};
반응형

+ Recent posts