1. 程式人生 > >牛客題解day03

牛客題解day03

在一個長度為n的數組裡的所有數字都在0到n-1的範圍內。 陣列中某些數字是重複的,但不知道有幾個數字是重複的。也不知道每個數字重複幾次。請找出陣列中任意一個重複的數字。 例如,如果輸入長度為7的陣列{2,3,1,0,2,5,3},那麼對應的輸出是第一個重複的數字2。

public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    這裡要特別注意~返回任意重複的一個,賦值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        for(int i=0;i<length;i++){
    		for(int j=i+1;j<length;j++) {
    			if(numbers[i]==numbers[j]) {
    				duplication[0]=numbers[i];
    				return true;
    			}
    		}
    		
    	}
		return false;
    
    
    }
}

NowCoder的老家住在工業區,日耗電量非常大。是政府的眼中釘肉中刺,但又沒辦法,這裡頭住的可都是納稅大戶呀。
今年7月,又傳來了不幸的訊息,政府要在7、8月對該區進行拉閘限電。但迫於壓力,限電制度規則不會太摳門,政府決定從7月1日停電,然後隔一天到7月3日再停電,再隔兩天到7月6日停電,一次下去,每次都比上一次晚一天。
NowCoder可是軟體專業的學生,怎麼離得開計算機。如果停電,就“英雄無用武之地”了。呵呵。
所以他開始盤算起自己回家的日子了,他想知道自己到家後到底要經歷多少天倒黴的停電。你能幫他算一算嗎?

輸入描述:

輸入包括多組資料。
每組資料包括一行:redraiment到家的日期。
輸入以0/0結束。

輸出描述:

對應每個輸入包括一個輸出。
為redraiment回家後停電的天數(包括到家那天)。
import java.util.Scanner;

public class Main {
       public static void main(String[] args) {
    	   Scanner scanner=new Scanner(System.in);
    	   while(scanner.hasNext()) {
    		   String input=scanner.nextLine();
                  if(input.equals("0/0")) break;
    		   maths(input);
               System.out.println(maths(input));
    		   
    	   }
	
}
       private static int maths(String input) {
    	   String[] strarry=input.split("/");
    	   int month=Integer.valueOf(strarry[0]);
    	   int day=Integer.valueOf(strarry[1]);
    	   int days=10;
    	   int count=2;
    	   int count2=0;
    	   if(0<month&&month<7) {
    		   days=10;
    	   }
    	   else if(month==7) {
    		   for(int i=1;i<day;) {
    			   i=i+count;
    			   count++;
    			   count2++;
    		   }
    		   days=days-count2;
    	   }
    	   else if(month==8) {
    		   for(int i=1;i<(31+day);) {
    			   i=i+count;
    			   count++;
    			   count2++;
    		   }
    		   days=days-count2;
    	   }
    	   else if(8<month&&month<=12) {
    		   days=0;
               
    	   }
    	  
		return days;
    	   
       }
}