“Java” Réponses codées

Java

DON'T DO THAT WITH YOU, go to python!
Green Team

Java

<div class="fb-page"
  data-href="https://www.facebook.com/imdb" 
  data-width="340"
  data-hide-cover="false"
  data-show-facepile="true"></div>
AKA_Mishra

Java

<div style="width: 100px;">
  <!-- Page plugin's width will be 180px -->
  <div class="fb-page" data-href="{url}" data-width="320"></div>
</div>
AKA_Mishra

Java

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

public class WeightRandom<T> {

    private final List<T> items = new ArrayList<>();
    private double[] weights;

    public WeightRandom(List<ItemWithWeight<T>> itemsWithWeight) {
        this.calWeights(itemsWithWeight);
    }

    /**
     * 計算權重,初始化或者重新定義權重時使用
     * 
     */
    public void calWeights(List<ItemWithWeight<T>> itemsWithWeight) {
        items.clear();

        // 計算權重總和
        double originWeightSum = 0;
        for (ItemWithWeight<T> itemWithWeight : itemsWithWeight) {
            double weight = itemWithWeight.getWeight();
            if (weight <= 0) {
                continue;
            }

            items.add(itemWithWeight.getItem());
            if (Double.isInfinite(weight)) {
                weight = 10000.0D;
            }
            if (Double.isNaN(weight)) {
                weight = 1.0D;
            }
            originWeightSum += weight;
        }

        // 計算每個item的實際權重比例
        double[] actualWeightRatios = new double[items.size()];
        int index = 0;
        for (ItemWithWeight<T> itemWithWeight : itemsWithWeight) {
            double weight = itemWithWeight.getWeight();
            if (weight <= 0) {
                continue;
            }
            actualWeightRatios[index++] = weight / originWeightSum;
        }

        // 計算每個item的權重范圍
        // 權重范圍起始位置
        weights = new double[items.size()];
        double weightRangeStartPos = 0;
        for (int i = 0; i < index; i++) {
            weights[i] = weightRangeStartPos + actualWeightRatios[i];
            weightRangeStartPos += actualWeightRatios[i];
        }
    }

    /**
     * 基於權重隨機算法選擇
     * 
     */
    public T choose() {
        double random = ThreadLocalRandom.current().nextDouble();
        int index = Arrays.binarySearch(weights, random);
        if (index < 0) {
            index = -index - 1;
        } else {
            return items.get(index);
        }

        if (index < weights.length && random < weights[index]) {
            return items.get(index);
        }

        // 通常不會走到這裡,為瞭保證能得到正確的返回,這裡隨便返回一個
        return items.get(0);
    }

    public static class ItemWithWeight<T> {
        T item;
        double weight;

        public ItemWithWeight() {
        }

        public ItemWithWeight(T item, double weight) {
            this.item = item;
            this.weight = weight;
        }

        public T getItem() {
            return item;
        }

        public void setItem(T item) {
            this.item = item;
        }

        public double getWeight() {
            return weight;
        }

        public void setWeight(double weight) {
            this.weight = weight;
        }
    }

    public static void main(String[] args) {
        // for test
        int sampleCount = 1_000_000;

        ItemWithWeight<String> server1 = new ItemWithWeight<>("server1", 1.0);
        ItemWithWeight<String> server2 = new ItemWithWeight<>("server2", 3.0);
        ItemWithWeight<String> server3 = new ItemWithWeight<>("server3", 2.0);

        WeightRandom<String> weightRandom = new WeightRandom<>(Arrays.asList(server1, server2, server3));

        // 統計 (這裡用 AtomicInteger 僅僅是因為寫起來比較方便,這是一個單線程測試)
        Map<String, AtomicInteger> statistics = new HashMap<>();

        for (int i = 0; i < sampleCount; i++) {
            statistics
                    .computeIfAbsent(weightRandom.choose(), (k) -> new AtomicInteger())
                    .incrementAndGet();
        }

        statistics.forEach((k, v) -> {
            double hit = (double) v.get() / sampleCount;
            System.out.println(k + ", hit:" + hit);
        });
    }
}
Robin Li

Java

 public void showDetails()
    {
        System.out.println("Name: " + name +"\nEmployee Number: " + empNo +"\nAge: "+ age);
        
Miguel Evangelista

Java

Java is a programming language and 
computing platform first released by Sun Microsystems in 1995. 

It has evolved from humble beginnings to power a large share 
of today’s digital world, 
by providing the reliable platform upon which many services 
and applications are built. 

New, innovative products and digital services designed 
for the future continue to rely on Java, as well.
Swaggero_0

Java

# import sys
# rop=[]
# for line in sys.stdin:
#     rop.append(line.strip())
# for i in rop:
#     print(i)
    
def ratatouille():
    N, P = map(int, input().strip().split())
    R = list(map(int, input().strip().split()))
    Q = []
    for _ in range(N):
        Q.append(list(map(int, input().strip().split())))
    # print(Q,R)
    choices = []
    for i in range(N):
        for j in range(P):
            q, r = Q[i][j], R[i]
            lower = max(1, (10 * q + 11 * r - 1) // (11 * r))
            upper = (10 * q) // (9 * r)
            if lower > upper: continue
            choices.append((lower, False, i, q))
            choices.append((upper, True, i, q))
    choices.sort()

    count = 0
    quantities = [[] for _ in range(N)]
    for (_, is_upper, i, q) in choices:
        if is_upper:
            if q in quantities[i]:
                quantities[i].remove(q)
        else:
            quantities[i].append(q)
            if all(quantities):
                count += 1
                for j in range(N):
                    del quantities[j][0]
    return count

for case in range(1):
    print(ratatouille())
Adorable Addax

Java

public class Main {
	public static void main(String[] args) {
		System.out.println("Hello, World!"); 
    }
}
4MBL

Java

import java.text.DecimalFormat
Thivar Govender

Java

create an application containing an array that stores eight integers. the application should call five methods that in turn (1) display all the integers, (2) display all the integers in reverse order ,(3) display the sum of the integers and (4) display all values that are higher than the calculated average value. Save the file as ArrayMethodDemo.java.
Gentle Gazelle

Réponses similaires à “Java”

Questions similaires à “Java”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code