2017/3/29

NetBeans開發Java EE發生Deployment error: Starting of Tomcat failed. See the server log for details...因素

因常遇到Netbeans在mac上跑Tomcat常常會出現這個錯誤,但原因到現在我還不明,但今天爬到一篇關掉Proxy就可以run了,這次是唯一一次關掉它就可run,過去都要開機等一段時間,不然就是Tomcat已經啟動了,有看到Tomcat歡迎畫面,但NetBeans還在啟動卡死狀態,不代表每次關掉都可以解決。

10038 - Jolly Jumpers

/*
482 - Permutation Arrays http://bit.ly/2o2i80g
10038 - Jolly Jumpers http://bit.ly/2mMH3Fn
 */
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
    int N=4;
    while (cin>>N){
        int s[3001],check[3001]={0};
        bool ok=1;
        cin>>s[0];
        for (int i=1 ; i<N ; i++) {
            cin>>s[i];
            int temp = abs(s[i]-s[i-1]);
            if (temp <= 3000) {
                check[temp] = check[temp] +1;
            }
        }
        for (int i=1;i<N;i++){
            cout<<check[i];//0120,check[0]=0,check[1]=0,check[2]=1,check[3]=2,check[4]=0
            if (check[i]==0) { 
                ok = 0; 
                break; 
            }
            //cout<<check[i];//i從0開始到N,印出0111
        }
        if (ok) cout<<"Jolly\n";
        else cout<<"Not jolly\n";
    }//while結尾
    return 0;
}
題目網址:http://bit.ly/2mMH3Fn
這題不難,只是要知道切入點。
設輸入測資為4 1 4 2 3,輸出Jolly
第一個for迴圈做的事是,找到相鄰兩數的絕對值,並把絕對值當索引,並且+1
1、4的絕對值是3
4、2的絕對值是2
2、3的絕對值是1
每次把它們放到temp暫存,且必定小於3000,
最後check[3]、check[2]、check[1]的值都等於1

第二個for迴圈,走訪check[1~N-1],只要其中一個值是0,就是Not jolly
很顯然,check[1~4-1]沒有一個是0,所以印出Jolly

歸納一些解題想法:
1.需仔細觀察會Jolly和Not jolly與測資的情況。
2.藉由測資特性去思考,通常會Jolly的,每個相鄰兩數的絕對值,一定是有順序性且不重複,藉由這特性把絕對值當索引,存入一次記錄為1。而Not jolly,通常絕對值會跳很大,或都相等,如1 3 5 7,會變成每次都把同一個索引+1,其他索引都是0,必定就產生0,就一定是Not jolly。

2017/3/21

10420:List of Conquests

//http://bit.ly/2nzLF1m
#include <iostream>
#include <map>
using namespace std;

int main() {
    int n;
    map<string, int> record;
    map<string, int>::iterator i;
    
    while (cin>>n && n!=1) {
        string country, tmp;
        while (n--) {
            cin >> country;
            record[country]++;
            getline(cin, tmp);
        }
        for ( i = record.begin(); i != record.end(); i++)
            cout << i->first << " " << i->second << endl;
    }
    return 0;
}
題目網址:http://bit.ly/2nzLF1m

2017/3/19

10101 - Bangla Numbers

#include<iostream>
using namespace std;
void printNumber( long long n ){
   if( n == 0 ) return;

   if( n/10000000 ){
     printNumber( n/10000000 );
     cout<<" kuti";
     n %= 10000000;
   }

   if( n/100000 ){
     printNumber( n/100000 );
     cout<<" lakh";
     n %= 100000;
   }

   if( n/1000 ){
     printNumber( n/1000 );
     cout<<" hajar";
     n %= 1000;
   }

  if( n/100 ){  //764
    printNumber( n/100 );
    cout<<" shata";
    n %= 100;
  }

  if( n ) cout<<" "<<n;
}

int main(){
    long long n;
    int serialNumber = 1;//serialNumber是每筆測試資料的編號
    while(cin >> n && n != 0){
        cout<<serialNumber<<".";
        if( n ) printNumber( n );
        serialNumber++;
    }
    return 0;
}
上面程式碼原作者在這http://knightzone.org/?p=1789,我稍微修改,功能也完整
來追蹤一次,設n=764
第一次在main中printNumber(n)呼叫,先做if( n/100 )的敘述,
第二次在if( n/100 )中的printNumber(n/100)呼叫遞迴,764/100=7,丟進7這參數進去,
最後會執行第30行的if,印出7,這次呼叫完畢。
會回到第一次繼續執行,故印出 shata,接著再執行n %= 100; n計算後為64
再做30行敘述,印出64
而其他同理,因數字大就不追蹤了。

以下是我寫失敗的程式碼:
//http://bit.ly/2nDnxrI
//764   7 shata 64
#include<iostream>
using namespace std;
int howManyPlace(int n){//回傳這個數字有幾位數
    int count = 0;
    while (n != 0) {
        n = n / 10;
        count++;
    }
    return count;
}
int main() {
    int serialNumber = 0;
    int n = 0;
    while (cin >> n && n != 0) {
        serialNumber++;
        if(howManyPlace(n) == 3){
            cout<<serialNumber<<". "<<n/100<<" shata "<<n%100;
        }
        if(howManyPlace(n) == 5){
            int a,b,c;
            a = n/1000;
            b = (n%1000)/100;
            c = n%100;
            cout<<serialNumber<<". "<<a<<" hajar "<<b<<" shata "<<c;
        }
    }
    return 0;
}
題目網址:http://bit.ly/2nDnxrI

2017/3/18

10929 - You can say 11

#include<iostream>
using namespace std;
int main(){
    int n = 0;
    while(cin>>n && n!=0){
        if(n%11 == 0){
            cout<<n<<" is a multiple of 11.";
        }
        else{
            cout<<n<<" is not a multiple of 11.";
        }
    }
    return 0;
}
題目網址:http://bit.ly/2nyjwYG

10055 - Hashmat the Brave Warrior

#include<iostream>
using namespace std;
int main(){
    int firstNumber = 0;
    int EndNumber = 0;
    while(cin>>firstNumber>>EndNumber){
        int temp = 0;
        if(firstNumber > EndNumber){
            temp=EndNumber;
            EndNumber=firstNumber;
            firstNumber=temp;
        }
        cout<<EndNumber-firstNumber;
    }
    return 0;
}
題目網址:http://bit.ly/2nQ1nS2

11332 - Summing Digits

#include<iostream>
using namespace std;

int main(){
    int n;
    while( cin>>n && n!=0 ){
        while( n/10 ) n = n/10 + n%10;
        cout<<n;
    }
    return 0;
}
假設輸入是47
while迴圈第一次n=47,回傳11
while迴圈第二次n=11,回傳2
while迴圈第三次n=2,因2/10,無法除,因此while判定為false,停止迴圈
並且印出2
題目網址:http://bit.ly/2mCUJyr

2017/3/17

11764: Cricket Field

#include <iostream>
using namespace std;
int main() {
    int testCase = 0;
    cin>>testCase;//輸入有幾筆測試資料
    int caseNumber = 0;
    while(testCase--){
        int wallQuantity = 0;//高牆的數量
        int wallHeight = 0;//牆的高度
        int wallFirst = 0;//第一個高牆
        int highJumps = 0;
        int lowJumps = 0;
        cin>>wallQuantity;
        cin>>wallFirst;
        for(int i = 0 ; i<wallQuantity-1 ; i++){
            cin>>wallHeight;
            if(wallHeight > wallFirst){
                highJumps ++;
            }
            if(wallHeight < wallFirst){
                lowJumps ++;
            }
            wallFirst = wallHeight;
        }
        caseNumber++;
        cout<<"Case "<<caseNumber<<": "<<highJumps<<" "<<lowJumps;
    }
    return 0;
}
這題的重點是23行,不管有沒有大於或小於,都要走訪到下一個數字,並且取代目前的wallFirst。一開始我是把這行寫在if內,但這樣是錯的
題目網址:http://bit.ly/2mAas0n

11192 - Group Reverse

#include <iostream>
using namespace std;
 
int main() {
    int textLength = 0;
    while ((cin >> textLength) && (textLength != 0)) {
        string word = "";
        cin>>word;
        int group = word.length() / textLength;
        for (int i = 0; i < word.length(); i += group) {
            for (int j = i + group - 1; j >= i; j--) {
                cout << word[j];
            }
        }
    }
    return 0;
}
題目網址 http://bit.ly/2mTqO7Y 補充: 字串翻轉
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
    
    string str1 = "Xanxus";
    for(int i = str1.length();i>=0;i--){
        cout<<str1[i];
    }
    
    return 0;
}

2017/3/13

11364: Parking

#include <iostream>
using namespace std;

int main() {
    int testCase; //測資數目
    int storeNum; //商店的數量
    cin>>testCase;

    while (testCase--) {
        int minNumber = 9999; //最小值
        int maxNumber = 0; //最大值
        int number; //商店號碼
        cin>>storeNum;
        for (int i = 0; i < storeNum; i++) {
            cin>>number;
            if (number > maxNumber) {
                maxNumber = number;
            }
            if (number < minNumber) {
                minNumber = number;
            }

        }
        cout << (maxNumber - minNumber)*2;
    }
    return 0;


}
UVA題目網址http://bit.ly/2miHPEJ

2017/3/9

10783 - Odd Sum

#include <iostream>
using namespace std;
int main() {
    int testCase = 0, a = 0, b = 0,count=0;
    cin>>testCase;
    while (testCase--) {
        int total = 0;
        cin >> a>>b;
        for (int i = a; i <= b; i++) {
            if (i % 2 != 0) {
                total += i;
            }
            
        }
        count++;
        cout<<"Case"<<" "<<count<<": "<<total<<"\n";
        
    }
    
    return 0;
}
UVA此題目網址http://bit.ly/2m374Lm

100 - The 3n + 1 problem

#include <iostream>
using namespace std;
 
int calc_cycle_length(int n) {
    int cycle_length = 1;
    while (n != 1) {
        if (n == 1) {
            return false;
        } else if (n % 2 != 0) {
            n = 3 * n + 1;
        } else {
            n = n / 2;
        }
        cycle_length++;
    }
    return cycle_length;
}
 
int main() {
    int firstNum, endNum;
    int temp = 0;
    int cycleLength = 0;
    int maxLength ;
    while (cin >> firstNum >> endNum && (firstNum != 0 || endNum != 0)) {
        cout<<firstNum<<" "<<endNum<<" ";
        maxLength = 0;
        if (firstNum > endNum) {
            temp = endNum;
            endNum = firstNum;
            firstNum = temp;
        }
        for (int i = firstNum; i < endNum; i++) {
            cycleLength = calc_cycle_length(i);
            if (cycleLength > maxLength) {
                maxLength = cycleLength;
            }
        }
        cout<<maxLength<<"\n";
    }
 
    return 0;
 
}
拿到瘋狂程設run有一筆99999 99999這筆測資出不來,瘋狂程設這筆結果是要輸出227,但我只能輸出0..... 題目網址http://bit.ly/2eOC6q1

2017/3/7

C++ 字串

C++字串本質是由字元所組成的陣列,並在最後加上一空字元'\0'
char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
它是用空字元來識別一陣列是否為字串
輸出:cout << str << endl;

2017/3/4

在Tomcat部署Servlet

一、創造Servlet程式,檔名HelloWorld.java
// Filename : HelloWorld.java
// Description : This servlet merely says hello!
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

        public void doGet ( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException    {

                response.setContentType("text/html");
                PrintWriter out = response.getWriter();

                out.println("<html>");
                out.println("<head><title>Hello, Cruel World!</title></head>");
                out.println("<body>");
                out.println("<h1>Hello, Cruel World !</h1>");
                out.println("This is my first servlet.");
                out.println("</body>");
        }// end doGet
}///:~

手動編譯java

就嘗試看看自己編譯java,而不靠IDE,後面會有自己動手從Tomcat部署Servlet,這篇先打好基礎。

2017/3/3

讓AJAX動態內容支援瀏...的補充註解

為了解決我ajax回上一頁抓的資料不見的問題,而找到的暗黑大的這篇文章,雖然最後用了sessionStorage去解決,但還是把他那篇文章程式碼研究了一下,因此還是想記錄起來。

其實就是同樣的程式碼但我多加了幾行註解。
<!DOCTYPE html>
<html>
    <head>
        <title>AJAX GoBack</title>
        <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.ba-bbq/1.2.1/jquery.ba-bbq.min.js" type="text/javascript"></script>
        <script>
            $(function() {
                $("#s1").show();
                $("input.nav-btn").click(function() {
                    var $btn = $(this); //this為目前被點擊的btn物件
                    $btn.parent().hide(); //當點擊按鈕後,就把自己隱藏。如:點擊STEP1的按鈕,就把自己的父類別div隱藏
                    var nav = $btn.data("nav"); //取得現在這個按鈕的data-nav屬性的值,第一次點擊就取得s2,$btn.data("nav")就是印出s2
                    $("#" + nav).show(); //顯示下一個div
                    //將目前的步驟加註在location.hash
                    $.bbq.pushState({
                        step: nav
                    }); //pushState裡面的東西就是key:value形式
                });
                //hash變化時觸發hashchange事件
                $(window).bind('hashchange', function(e) { //控制上一頁的動作
                    //由hash取出step參數,決定要顯示哪一個div
                    var s = e.getState("step") || "s1"; //e.getState("step")回傳s2 or s3
                    //if (!$("#" + s + ":visible").length) {
                    $("#main > div").hide();
                    $("#" + s).show();
                    //}
                });
            });
        </script>
        <style>
            #main div {
            width: 300px;
            height: 200px;
            display: none;
            padding: 10px;
            }
            #s1 {
                background-color: #ff7777;
            }
            #s2 {
                background-color: #77ff77;
            }
            #s3 {
                background-color: #7777ff;
            }
        </style>
    </head>
    <body>
        <div id="main">
            <div id="s1">
                STEP1
                <input type="button" class="nav-btn" value="Next" data-nav="s2" />
            </div>
            <div id="s2">
                STEP2
                <input type="button" class="nav-btn" value="Next" data-nav="s3" />
            </div>
            <div id="s3">
                FINAL
                <input type="button" value="Submit" />
            </div>
        </div>
    </body>
</html>

參考:http://bit.ly/2lJqANm