2017-01-31 30 views
-1

问题可见衣服的数量:计数可见寻找绳索

衣服

一位女士最近聘请女佣她的家务劳动,照顾,使她可以集中精力建立自己的新业务。作为日常工作的一部分,女仆每天清理房子并洗衣服。

但是,绳子上的衣服干燥存在问题。由于绳子很小,所有的衣服都不能正常展开,因此女佣将一块布放在另一块布的顶部。所以有些衣服是部分或全部被其他人覆盖的。了解衣服被挂起的顺序和位置,确定从正面看时可以看到多少衣服(部分或完全)。

考虑绳子长度为N米,分为N等分,从0开始到N。宽度P的每块布料完全占据一个或多个部分。 (1 < = P < = N & P是+ ve整数)。

(注:忽略的布另一维度针对此问题的目的) 输入规格 你的程序必须读三个参数RopeLength,CountofClothes,ClothesPosition []其中 RopeLength是以米为单位的绳索的长度(1 < = RopeLength < = 10000) CountofClothes是放置在绳子上的衣服的数量(1 < = CountofClothes < = 10000) ClothesPosition是一个给出挂衣服位置的数组。布的位置由两个整数L和W来描述,其中L表示布悬挂的起始位置(0 < = L < = 10000),W是布的宽度(1 < = W < = W000) 。

接收输入的顺序是衣服放置在绳索上的顺序。 输出规格 您的函数GetVisibleCount应将输出变量'output1'设置为衣服可见的计数完全或部分。

例 样品输入:

10:5:{{0,4},{6,3},{1,5},{6,4},{7,2}} 

这里10为以米绳的长度。 5是挂在绳子上的衣服数量。从观察时

4 

的衣服可见总数:所述第一布从0开始并包括4个部分,从0。第二布开始于图6和从6覆盖3个部分等.. 样本输出前面是4.

+3

做好准备,以获得最大的没有。对今天的反对票。 提示:修改您的帖子以显示您尝试的内容。 – P0W

回答

0
  1. 我试过的逻辑是假设有2件衣服在同一个位置开始,宽度大于另一个;那么这将覆盖一块布,因此该布将不可见。

  2. 如果假设有两件衣服,一件是位置1,另一件是位置2,但是如果位置1处的布料宽度大于位置2处布料的宽度,那么在这种情况下布料将不再是布料可见。

因此,我们将循环运行n次,其中n =衣服数量,每次看到这种情况时,可见衣服的数量= n-1。

但是这里的问题是程序的用户输入应该按照每个位置以递增的方式发生。那是在2个位置之后,我可以给3或4个位置的输入,而不是第5个位置,然后是第3个位置。

其次,我想保留一个变量作为类变量= numberofclothes,每当我遇到上述条件时就会减少 但是我没有足够的测试数据来检查这个。


package main; 

import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.concurrent.SynchronousQueue; 

import javax.swing.plaf.synth.SynthScrollBarUI; 

public class Ropecalculation { 

    static int numberofvisibleclothes; 
    static int ropelenth; 
    static int numberofclother; 
    static int clothwidth; 
    static int startposition; 
    static int[] startpoint; 
    static int[] width; 
    public static void main(String args[]){ 

     Scanner scan=new Scanner(System.in); 
     System.out.println("Enter the rope length"); 
     ropelenth=scan.nextInt(); 
     System.out.println("Enter the number of clothes"); 
     numberofclother=scan.nextInt(); 
     for(int i=0;i<numberofclother;i++){ 
      startpoint=new int[numberofclother]; 
      width=new int[numberofclother]; 
      System.out.println("Enter start position"); 
      startposition=scan.nextInt(); 
      startpoint[i]=startposition; 
      System.out.println("Enter width"); 
      clothwidth=scan.nextInt(); 
      width[i]=clothwidth; 
      //System.out.println(startpoint.length); 
     } 
     Ropecalculation rp=new Ropecalculation(); 
     rp.checkvisibleclothes(startpoint,width); 
    } 
    public void checkvisibleclothes(int[] startpoint, int[] width) { 

     for(int j=0;j<startpoint.length-1;j++){ 
      int x=startpoint[j]; 
      int c=startpoint[j+1]; 
      if(x==c){ 
       int wide=width[j]; 
       int wideagain=width[j+1]; 
       if(wide<=wideagain){ 
        numberofvisibleclothes=numberofclother-1; 
       } 
      } 
      else if(c==x+1){ 
       int wide1=width[j]; 
       int wideagain1=width[j+1]; 
       if(wide1>wideagain1){ 
        numberofvisibleclothes=numberofclother-1; 
       } 
      } 
     } 
     System.out.println(numberofvisibleclothes); 
    } 
} 
+0

如果这不是一个答案重新格式化你原来的帖子(问题),并将其添加到那里。 – P0W

0
public class DryingClothes { 

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    //System.out.println("Enter rope length"); 
    //int ropeLength = scan.nextInt(); 
    System.out.println("Enter number of clothes"); 
    int clothesCount = scan.nextInt(); 
    int[][] dimensions = new int[clothesCount][2]; 
    Map<Integer, Integer> visibility = new HashMap<>(); 
    for(int i=0;i<clothesCount;i++) { 
     dimensions[i][0] = scan.nextInt(); 
     dimensions[i][1] = scan.nextInt(); 
     for(int j=dimensions[i][0];j<dimensions[i][0]+dimensions[i][1];j++) { 
      visibility.put(j, i); 
     } 
    } 
    Set<Integer> clothesRemaining = new HashSet<>(); 
    for(int key : visibility.keySet()) { 
     clothesRemaining.add(visibility.get(key)); 
    } 
    System.out.println(clothesRemaining.size()); 
    scan.close(); 
    } 
}