2016年7月31日 星期日

[C_GM16-易] 堆積木

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=13352

這題先取三個數的最大公因數例如:500 15 5 3
gcd(15 5 3)=15
15/15*15/5*15/3=1*3*5=15<=500
30/15*30/5*30/3=2*6*10=120<=500
45/15*45/5*45/3=3*9*15=405 (下一筆超過500所以取到這裡)

[C_MM273-易] 鉛筆的分法

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=19939

這題利用雙迴圈和判斷就可以了

[C_MM271-易] 所有公因數計算

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=19933

這題每個數字後面都有一個空格包括最後一筆資料也是

2016年7月28日 星期四

2016年7月26日 星期二

[C_MM081-易] 10進位及16進位

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1130

十進位轉16進位Integer.toHexString(i)
十進位轉8進位Integer.toOctalString(i)
任何進位轉十進位Integer.parseInt(str,16)

[C_ST91-易] 英文斷詞

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=20150

第一組斷辭不比對先印出之後的每個單字逐一往前比對若j=-1代表無重複直接印出
注意忽略大小寫例如: Hello I  am a boy a young BOY=>hello i  am a boy young

Q11059: Maximum Product

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2000


困難度 
注意輸出要多一行,還有必須使用long才會過

2016年7月25日 星期一

[C_GM10-易] 小孩玩耍-繩子篇

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2245

一條線所圍成的圖形,其邊數越多周長越長相對的面積也越大,所以想像成圓內接正多邊形,邊數越多趨近於圓
num除以2的兩倍除以PI

[C_SO51-易] 排程問題

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30216

這題就是把數列排序再累加到n-1並把所有累加的數相加就是答案了

2016年7月22日 星期五

2016年7月20日 星期三

Q10018: Reverse and Add

https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&category=&problem=959&mosmsg=Submission+received+with+ID+17705978

這題數字翻轉相加會爆所以要用long型態
還要注意1.當只輸入長度為1的數字印出相加然後跳出迴圈2.第一筆資料不做回文(palindrome)判

Q11369: Shopaholic

https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&category=&problem=2354&mosmsg=Submission+received+with+ID+17702604

這題很簡單就使是把商品價格存入陣列然後作排序最後從尾開始遞減3相加

[C_AR81-易] 求Array元素最大值的和

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7066

這題然是3*3但輸入資料只有三行每行個別字串切割放入一個大小為九的陣列
最後最排序,取末三個相加

[C_AR70-易] 不要在太歲爺頭上動土

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3684

正沖就是對沖-6,假如為負數加12對應到陣列中就是答案了

Q11541: Decoding

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2536

困難度 
本題較為麻煩要利用StringTokenizer將字串中的英文和數字拆開

2016年7月19日 星期二

[C_AR85-易] 反矩陣

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=3505

二階反矩陣的公式是主對角線交換另一個對角線加上負號然後每一個數字乘以1/兩對角線相乘的差
還必須注意當是2.0時要輸出2若有小數則輸出到小數點第一位

[C_AR74-中] 學生資料搜尋程式

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1679

這題不需要什麼困難技巧只需要把內容放入二維陣列,依指定的行下去逐一搜尋是否有吻合的資料然後印出

[C_AR59-易] 好數問題

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1623

這題判斷好數,先存入字串中判斷長度若大於4就Failure Imput
好數的判斷就是把四個數拆開放入num[10]的陣列中分別為0~9以索引值當作數字內容存次數
1122,3113這些數字也包含在測資中他也不是好數所以建立temp儲存第2多的數
若輸入1122 =>max=2 temp=2(2==2)就是上述的狀態
若輸入1213 =>max=2 temp=1 就是好數

[C_AR67-易] 隨意九九乘法

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3674

這題沒什麼困難,利用print印出3位數不足補零=>%03d

[C_AR62-中] 矩陣相乘

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3532

2個陣列 a*b 以及 x*y 是列乘行
矩陣相乘用三迴圈實作,最外迴圈a控制 第二個y控制 第三個b控制

[C_AR66-易] 撲克牌13點

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3671

這題讀取字元當A時=>14 J=>11 Q=>12 K=>13 因為是讀取字元只有一個字可是可能會出現10這個數字排裡面沒有1這個數字(被A取代)所以當讀到1時就代表是10

[C_MM253-易] 3x+1數列產生

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=7869

這題就只是當偶數除二奇數乘以二加一直到一跳出迴圈

[C_OT06-易] 英文句子字之倒轉

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2554

這題就是讀入字串陣列然後從尾巴讀回頭

[C_RU23-易] 遞迴練習2f(n)=f(n-1)+2

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29334

這題就是考遞迴的概 2f(n)=f(n-1)+2 勢必f(n)=(f(n-1)+2)/2

C_RU08-易 彈力球

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=8254

這題大一寫的時候也是一筆測資過不了最近用java來寫就AC了,這題解法式每次高度為上一次的一半每次彈跳下去和起來瞬間高度一樣(除了第一次球掉地面,先加入tot)

[C_RU07-易] 電路板溫度升高問題

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=3545

這題是系上鍾主任所出的題目使用遞迴做運算,我當初用c顯示記憶體引用錯誤不知道是什麼原因後還用java就AC了

[C_RU01-易] 矩陣練習

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=468

這題比long還要大所以要使用大數運算BigInteger才會過

[C_SO25-易] 判斷序列是否經過排序

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=13651

[C_SO35-易] 排序數字

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=7431

把輸入的新值放入最後,並利用插入排序法逐一比對

[C_SO29-易] 藝人選秀

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=6841

題目第一個測資有錯誤排序後前三應該是 4 2 3

[C_SO22-易] 你們很強!

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=11197

[C_SO14-中] 求中位數

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7907

這題不難先把5個數字存入陣列裡然後作排序最後讀出arr[2]的就是中位數了

[C_ST47-易] 跳格字串解密

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=7003

這題就只是設定迴圈間格為三去讀取字元陣列的內容

[C_ST31-易] 移除字串中的空白鍵和定位鍵

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2176

這題用字串切割空白有split(只能切一個東西)StringTokenizer(能切許多特定的東西)

2016年7月18日 星期一

[C_ST37-中] 字串比對

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2688

這題速解法直接利用java的indexOf()函式~

[C_MM210-易] 道路綠美化

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=11199

這題先算出要種幾棵樹並存入陣列(有種樹為1)並且變數x紀錄次數
然後再從尾開始建樹燈假如重疊就x--減去樹換成建立樹燈然後y++

[C_MM226-易] 薪水問題

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=6429

規定工時等於15*12=180若小於180扣錢大於算加班費,最後另外算開會的工錢

[C_MM214-易] 賣麵包

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=6280

這題你會發現用小盒子裝的數量會比較多所以迴圈從最大數量(盒子總數量)開始遞減族依判斷若大小盒子乘以各個可裝的數量等於總麵包數量就是我們要的答案了

[C_MM224-易] 我要九十九

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=6416

這題就是進入迴圈每次開根號乘以10判斷是否為99每次的結果要用地板函數floor()它會自動省略小數成整數 
例如: 99.1234 => 99

[C_MM212-易] 路上的東西不要亂撿 !

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=5127

這題就是所謂的約瑟夫問題因為測資的資料量大勢必不能用模擬的方法,會超時
所以有時間複雜度O(1)的方法,詳細內容這個網頁有說得很清楚
https://maskray.me/blog/2013-08-27-josephus-problem-two-log-n-solutions

[C_MM205-易] 爬樓梯

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=3532

爬樓梯的問題就是求費是數列

[C_MM201-易] 立方和魔數

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=8192

這題我用java都返回值1不知道問題在哪用c就過了,不過字元陣列大小需設4不然會無效記憶體引用

Q10370: Above Average

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1311

困難度 
這題就是取平均再依一下去做計數去除以總個數再乘以0.01就是答案了

UTSA Basic Problem 2. 編碼器

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=13952

這題除了英文字母大小寫之外其餘照輸出要考慮到字母越界的問題所以當超出大小寫z時要回到a故-26反之

2016年7月17日 星期日

[C_ST58-易] 字串旋轉

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=8311

這題字串旋轉就是把輸入的五個數字加起來就是我們所想要的位移數,需要考慮兩個地方
1.假如位移數超過26就要取她的餘數
2.若小於0就要進入迴圈每次加26到大於0為止

ITSA Basic 題目1. 矩陣數字顯示

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30746

這題要注意每個數字都是有5個空間,包含1題目的圖片可能被喀掉所以會覺得1只占兩格
還要注意每行尾無空格並且換行
此題作法是先把各個數字存入二維陣列然後每行讀出,num[s[j]-'0']就是s[j]ASCii減去字元0就是我們所輸入的數字了

ITSA Basic 題目24. 計算複利

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15915

這題必須要用Python才能AC用其他語言都不能過,我也不知道原因測試了很久,這題是要你算出複利所以每次都要乘上1+月利率
raw_input() 是標準輸入(存入字串變數)
n1=int(n)  
r1=float(r) 這兩個是轉換資料型別並存入新變數

ITSA Basic 題目13. 撲克牌大小

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30764

這題因為不知每筆的長度為何故先存入字串再做切割,然後再將切割後的字串以子字串subsring拆開字母和數字存入二維陣列
本題是用氣泡排序法,先判斷字母是否一樣若一樣則再比對數字大者在前,因為S>H>D>C剛好符合ASCii順序就直接toCharArray()轉成字元陣列去做比對

ITSA Basic 題目35. 買獎品

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15948

這題直接利用Arrays.sort()排序輸入的陣列然後再依序從小開始選購禮物最後在驗證是否超出預算

Q12289 : One-Two-Three

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3710

困難度 
這題輸入只有1,2,3三種可能所以主要是判斷1和因為這兩個數字的英文字母都只有三個
所以利用indexOf(' ')下去做搜尋,>=0代表字串中有該字母

Q11479: Is this the easiest problem?

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2474

困難度 
三角形的構成條件:
1.任兩邊相加大於第三邊
2.沒有零邊
另外這題大於2的32次方所以形態要使用long

[C_AR188-易] 陣列元素

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29920

依題意找出重複字元並且大於n/2的元素,因為不知有多少個字元所以存入字串再切割做比對

[C_AR176-易] 任意個分數分佈統計

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15374

這題因為不知每一筆有幾個成績所以要放入字串中然後做切割

[C_AR156-易] 迴文

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29264

這題非常險惡測了第28次才AC,必須注意題目敘述3個限制
1.字元必須都不是包含0的數字(不用考慮省略)
2.字元必須是奇數(我是用字串下去讀然後檢查長度是否為奇數個)
3.其數值不大於INT正值上限(這個其實包含在項2項了,因為測資中大於INT上限的值似乎是偶數個字母)
4.另外我發現測資第2個一直不過的原因是字串內不能包含0~9以外的數字

[C_MM117-易] 少了一個數

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3057

這題就宣告n個陣列,輸入完n-1個之後下去搜尋 ps.以輸入的值-1當作索引值

[C_AR116-易] 到底有幾隻兔子?

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=20731

這題就是算費式數列要考慮到益位的問題所以要使用long long

[C_AR115-易] 梅花座

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=20727

這題算用途法煉鋼法逐一檢視八方是否有重複的,java8*8要建立10*10的陣列(考慮到牆)否則會跳出超出陣列大小的例外

[C_MM116-易] 買東西

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2972

這題先把綠茶跟麵包金額先算出來,然後再判斷有送幾個餅乾最後相減加上餅乾錢就是答案了!

[C_MM110-中] Ugly number

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2843

這題用迴圈依序除以2,3,5,若都不能整除則跳出迴圈當x=1時i就是uglu number了!

2016年7月16日 星期六

[C_MM142-易] 分數最大值

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3205

這題輸出結果還要必須考慮到是否最簡分數,求兩者最大公因數再相除

[C_MM108-易] 位元計數器

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2679

這題是輸入時進位整數顯示二進位中有幾個一,Integer.bitCount()剛好符合要求

[C_MM104-易] 約瑟夫問題

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2658

約瑟夫的問題是很經典的題目,跟另一題心得報數很類似

[C_MM103-易] 費式數列

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2654

費式數列是很經典的題目每一個數目為前面兩個加總

[C_MM101-易] 糖果紙兌換

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2648

這題就只是每3張糖果紙換一顆糖果每次除以三然後再加除以三的餘數直到除以三為零

[C_MM118-易] 數字反向排列

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=3060

這題利用StringBuffer()呈現反轉

[C_MM109-易] 計算密碼


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1362

這題利用一個技巧就是先把浮點數轉成字串然後利用substring取得答案,然後題目給的測資有誤答案應該是1810

2016年7月14日 星期四

ITSA Basic 題目28. 統一發票對獎


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15933

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  String s1 = scn.next(), s2 = scn.next(), s3 = scn.next(), s4 = scn.next();
  int ans[] = new int[7], n = scn.nextInt(), tot = 0, m[] = { 200000, 40000, 10000, 4000, 1000, 200 };
  while (n-- != 0) {
   String str = scn.next();
   if (s1.equals(str)) {
    ans[0]++;
    tot += 2000000;
    continue;
   }
   for (int i = 0; i &lt= 5; i++) {
    if (str.indexOf(s2.substring(i), i) &gt= 0) {
     ans[i + 1]++;
     tot += m[i];
     break;
    } else if (str.indexOf(s3.substring(i), i) &gt= 0) {
     ans[i + 1]++;
     tot += m[i];
     break;
    } else if (str.indexOf(s4.substring(i), i) &gt= 0) {
     ans[i + 1]++;
     tot += m[i];
     break;
    }
   }
  }
  for (int i = 0; i &lt ans.length; i++) {
   if (i != 0)
    System.out.print(" ");
   System.out.print(ans[i]);
  }
  System.out.printf("\n%d\n", tot);

 }

 /* 
    題目:題目28. 統一發票對獎
    作者:1010
    時間:西元 2016 年 7 月 */
}

這題利用.substring(i)求得子字串來擷取各位數的獎項然後利用.indexOf()來判斷是否中獎 
注意因為兌獎是找尋末*碼所以.indexOf()要設定搜尋開始數字例如找三獎是查詢末六位數那開始的索引值就是2


import java.util.*;

public class Main {

 public static void main(String[] args) {
  String str="654321";
  String s=str.substring(2);//從第index[2]數字開始擷取
  System.out.println(s);
 }

 /* 
    題目:substring()求得子字串
    作者:1010
    時間:西元 2016 年 7 月 */
}

2016年7月12日 星期二

ITSA 41 [Problem 2] 心得報數


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=25163

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  int n = scn.nextInt();
  while (n-- != 0) {
   int a = scn.nextInt(), b = scn.nextInt(), arr[] = new int[a], count = 0, i = 0, j = 0;
   while (true) {
    if (j >= a)
     j = 0;
    if (arr[j] == 0)
     i++;
    if (i == b) {
     arr[j] = 1;
     i = 0;
     count++;
    }
    if (count == a - 1)
     break;
    j++;
   }
   for (i = 0; i < a; i++)
    if (arr[i] == 0)
     break;
   System.out.println(i + 1);

  }
 }
 /* 
    題目:[Problem 2] 心得報數
    作者:1010
    時間:西元 2016 年 7 月 */
}

這題就以模擬方式下去做判斷,利用迴圈假如成立就1直到最後只有1個0

ITSA 41 [Problem 3] Distance Sorting


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=25165

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  int n = scn.nextInt(), arr[][] = new int[n][3], ary[] = new int[n];
  for (int i = 0; i < n; i++) {
   arr[i][0] = scn.nextInt();
   arr[i][1] = scn.nextInt();
   arr[i][2] = scn.nextInt();
   ary[i] = arr[i][2];
  }
  Arrays.sort(ary);
  for (int i = 0; i < n; i++) {
   for (int j = 0; j < n; j++) {
    if (arr[j][2] == ary[i]) {
     System.out.println(arr[j][0] + " " + arr[j][1] + " " + arr[j][2]);
     break;
    }
   }
  }

 }
 /* 
    題目:[Problem 3] Distance Sorting
    作者:1010
    時間:西元 2016 年 7 月 */
}


這題就是很簡單的距離d的排序然後相對印出x,y

ITSA 41 [Problem 1] 和絃組成音


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=25161

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  String str[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E",
    "F", "F#", "G", "G#", "A", "A#", "B" };
  int n = Integer.parseInt(scn.nextLine()), count = 0, x;
  for (int i = 0; i &lt n; i++) {
   String s = scn.nextLine();
   char c[] = s.toCharArray();
   if (s.indexOf("m") &gt 0)
    count = 1;
   else
    count = 0;
   String s1[] = s.split("m");
   x = call(s1);
   if (count == 1)
    System.out.println(str[x] + " " + str[x + 3] + " " + str[x + 4 + 3]);
   else
    System.out.println(str[x] + " " + str[x + 4] + " " + str[x + 4 + 3]);
  }
 }

 public static int call(String[] s) {
  int x = 0;
  if (s[0].equals("C"))
   x = 0;
  else if (s[0].equals("C#"))
   x = 1;
  else if (s[0].equals("D"))
   x = 2;
  else if (s[0].equals("D#"))
   x = 3;
  else if (s[0].equals("E"))
   x = 4;
  else if (s[0].equals("F"))
   x = 5;
  else if (s[0].equals("F#"))
   x = 6;
  else if (s[0].equals("G"))
   x = 7;
  else if (s[0].equals("G#"))
   x = 8;
  else if (s[0].equals("A"))
   x = 9;
  else if (s[0].equals("A#"))
   x = 10;
  else if (s[0].equals("B"))
   x = 11;
  return x;
 }
 /* 
    題目:[Problem 1] 和絃組成音
    作者:1010
    時間:西元 2016 年 7 月 */
}


這題用s.indexOf("m")>0判斷輸入字串裡面是否有m,然後進入call()判斷是在第幾個開始

ITSA 42 [Problem3] 聖經密碼

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=25962

#include<stdio.h>
int main()
{
 char arr[10][10],str[10];
 int n,a,b,i,k,j;
 for(i=0;i<10;i++){
  scanf("%s",str);
  for(j=0;j<10;j++){
   arr[i][j]=str[j];
  }
 }
 scanf("%d",&n);
 for(i=0;i<n;i++){
  scanf("%d",&k);
  for(j=0;j<k;j++){
   scanf("%d%d",&a,&b);
   printf("%c",arr[a][b]);
  }
  printf("\n");
 }
    return 0;
 /* 
    題目:[Problem3] 聖經密碼
    作者:1010
    時間:西元 2016 年 7 月 */
}


這題不難就是列出二維陣列的字母首先輸入10*10是先讀入10行字串然後每行拆開成字元放入陣列

ITSA 42 [Problem2] 迴文字串


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=25960

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  int n = scn.nextInt(), tot;
  for (int i = 0; i < n; i++) {
   String str = scn.next();
   char arr[] = str.toCharArray();
   for (int j = arr.length - 1; j >= 0; j--) {
    tot = arr[j];
    if (arr[j] >= 'A' && arr[j] <= 'Z') {
     tot = Character.toLowerCase(arr[j]);
    }
    if (arr[j] >= 'a' && arr[j] <= 'z') {
     tot = Character.toUpperCase(arr[j]);
    }
    System.out.printf("%c", tot);
   }
   System.out.println();
  }
 }
/* 
    題目:[Problem2] 迴文字串
    作者:1010
    時間:西元 2016 年 7 月 */

}




public class Main{
 public static void main(String [] argv){
  //非字母的字元不會做任何改變
  String str = "abcd+ef123";
  //轉換成大寫
  System.out.println(str.toUpperCase());
  //轉換成小寫
  System.out.println(str.toLowerCase());
 }
 /* 
    題目:字串轉大小寫
    作者:1010
    時間:西元 2016 年 7 月 */
}


import java.lang.Character;

public class Main{
 public static void main(String [] argv){
  //非字母的字元不會做任何改變
  char ch = 'a';
  //轉換成大寫
  System.out.println(Character.toUpperCase(ch));
  //轉換成小寫
  System.out.println(Character.toLowerCase(ch));
 }

 /* 
    題目:字元轉大小寫
    作者:1010
    時間:西元 2016 年 7 月 */
}


ITSA 42 [Problem1] 完美數


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=25957


古希臘數學家歐幾里得是通過 的表達式發現前四個完全數的。
  1. 6(1位)
  2. 28(2位)
  3. 496(3位)
  4. 8128(4位)
  5. 33550336(8位)
  6. 8589869056(10位)
  7. 137438691328(12位)
  8. 2305843008139952128(19位)
  9. 2658455991569831744654692615953842176(37位)
  10. 191561942608236107294793378084303638130997321548169216(54位)

2016年7月11日 星期一

Q10340: All in All


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1281

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   String str1 = scn.next(), str2 = scn.next();
   char s[] = str1.toCharArray(), t[] = str2.toCharArray();
   int i, j, n = 0;
   for (i = 0; i < s.length; i++) {
    boolean c = true;
    for (j = n; j < t.length; j++, n++) {
     if (s[i] == t[j]) {
      c = false;
      n++;
      break;
     }
    }
    if (c) {
     System.out.println("No");
     break;
    }
   }
   if (i == s.length)
    System.out.println("Yes");

  }
 }
 /* 
    題目:Q10340: All in All
    作者:1010
    時間:西元 2016 年 7 月 */
}


困難度 
這題要注意的是找到字後要n++不然當測資faa aceecbeeffaeeced 會Yes(答案No)

2016年7月9日 星期六

Q10082: WERTYU


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1023

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   String str = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./", s = scn.nextLine();
   char ary[] = str.toCharArray(), arr[] = s.toCharArray();
   for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < ary.length; j++) {
     if (arr[i] == ' ') {
      System.out.print(" ");
      break;
     }
     if (arr[i] == ary[j]) {
      System.out.print(ary[j - 1]);
      break;
     }
    }
   }
   System.out.println();
  }
 }


 /* 
    題目:Q10082: WERTYU
    作者:1010
    時間:西元 2016 年 7 月 */
}



困難度 
這題就把圖中鍵盤的所有按鍵放入陣列中然後再用迴圈去比對然後輸出前一個

Q10071: Back to High School Physics


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=12&problem=1012&mosmsg=Submission+received+with+ID+17641052

#include<stdio.h>
int main()
{
 int v,t;
 while(scanf("%d%d",&v,&t)!=EOF){ 
 printf("%d\n",2*v*t);
 } 
     return 0;

 /* 
    題目:Q10071: Back to High School Physics
    作者:1010
    時間:西元 2016 年 7 月 */
}


困難度 
這題應該是UVa裡面最簡單的題目了...

2016年7月7日 星期四

Q579 : Clock Hands


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=520


困難度 
這提示輸入時針和分針顯示出在時鐘上最小的角度
首先先計算時針角度h_hand=h*(360/12)+(m/60)*(360/12) //一圈360度除以12代表每個小時幾度還要再加分針影響時針的角度
再來計算分針的角度m_hand=m*(360/60)//一圈360度除以60代表每一分走幾度
然後再取時針減分針角度的絕對值,最後還沒完成哦!還要判斷假如最後出來的絕對值小於180要再用360減去才是最小角

Q10922: 2 the 9s


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1863

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   String str = scn.next(), temp = str;
   if (str.equals("0"))
    break;

   int count = 1, tot;
   while (true) {
    tot = 0;
    String arr[] = str.split("");
    for (int i = 0; i < arr.length; i++)
     tot += Integer.parseInt(arr[i]);
    if (tot % 9 != 0 || tot / 10 == 0) {
     break;
    }
    count++;
    str = Integer.toString(tot);
   }
   if (tot % 9 != 0)
    System.out.println(temp + " is not a multiple of 9.");
   else
    System.out.printf("%s is a multiple of 9 and has 9-degree %d.\n", temp, count);
  }
 }

 /* 
    題目:Q10922: 2 the 9s
    作者:1010
    時間:西元 2016 年 7 月 */
}


困難度 
1.判斷是否為九的倍數=>每位數字加總在除以九,若整除即為九的被數,反之
2.若為九的倍數則要做9-degree
ex:輸入 999999999999999999999
999999999999999999999 → 9+9+9+...+9 = 189
189                   → 1+8+9       = 18
18                    → 1+          = 9 
9-degree=3

2016年7月6日 星期三

Q11417: GCD


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2412

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while(scn.hasNext()){
   int n=scn.nextInt(),g=0;
   if(n==0)break;
   for(int i=1;i<n;i++){
    for(int j=i+1;j&lt=n;j++){
     g+=gcd(i,j);
    }
   }
   System.out.println(g);
  }
 }
 public static int gcd(int i,int j){
  while((i%=j)!=0&&(j%=i)!=0);
  return i+j;
 }
 /* 
    題目:Q11417: GCD
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題很簡單只是呼叫gcd()自己撰寫的求最大公因數,然後再依題意雙迴圈加起來
求最大公因數的方法有兩種:

public class Main {

 public static void main(String[] args) {
  int a=300,b=250;
  while((a%=b)!=0&&(b%=a)!=0);//注意要括號包起來!
  System.out.println("The GCD is:"+(a+b));
 }
 /* 
    題目:求最大公因數 方法一
    作者:1010
    時間:西元 2016 年 7 月 */
}


public class Main {

 public static void main(String[] args) {
  int a=300,b=250;
  while(b!=0){
   int temp=a%b;
   a=b;
   b=temp;
  }
  System.out.println("The GCD is:"+a);
 }
 /* 
    題目:求最大公因數 方法二
    作者:1010
    時間:西元 2016 年 7 月 */
}


public class Main {

 public static void main(String[] args) {
  int a=300,b=250;
  System.out.println("The LCM is:"+(a*b)/gcd(a,b));//最小公倍數=兩數相乘除以兩數的的最大公因數
 }
 public static int gcd(int i,int j){
  while((i%=j)!=0&&(j%=i)!=0);
  return i+j;
 }
 /* 
    題目:求最小公倍數(結合GCD方法一實現))
    作者:1010
    時間:西元 2016 年 7 月 */
}


Q10190: Divide, But Not Quite Conquer!


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1131

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   long a = scn.nextInt(), b = scn.nextInt(), tot = 1;
   while (tot < a && b >= 2)
    tot *= b;
   if (tot == a && b >= 2 && a >= b) {
    for (long i = a; i >= 1; i /= b) {
     System.out.print(i);
     if (i != 1)
      System.out.print(" ");
    }
    System.out.println();
   } else
    System.out.println("Boring!");
  }
 }
 /* 
    題目:Q10190: Divide, But Not Quite Conquer!
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題要注意的地方有很多
1.溢位的問題所以要使用long
2.第二個數字b >= 2 當輸入0或1時答案是Boring!
   第一個數字要大於第二個a >= b
   輸入 2 2=>2 1
          2 0=>Boring!
          2 1=>Boring!
3.每筆測資的最後一個數字輸出後面無空白

2016年7月5日 星期二

Q11150: Cola


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2091

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while(scn.hasNext()){
  int n=scn.nextInt(),tot=0;
  tot=n;
  if(n%2==0)n++;
  while(n!=1){
   n-=2;
   tot++;
  }
  System.out.println(tot);
  }
 }
/* 
    題目:Q11150: Cola
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題解法非常簡單當有偶數瓶時先+1(借一瓶),進入迴圈3瓶換一瓶所以是-3+1=-2 =>每次-2直到剩餘一瓶

Q10812: Beat the Spread!


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1753

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  int n = scn.nextInt();
  for (int i = 0; i < n; i++) {
   int a = scn.nextInt(), b = scn.nextInt();
   if (b > a || (b + a) / 2 - (a - (b + a) / 2) != b)
    System.out.println("impossible");
   else
    System.out.println((b + a) / 2 + " " + (a - (b + a) / 2));
  }
 }
 /* 
    題目:Q10812: Beat the Spread!
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題在校內程式競賽有出現過 第一個數a=(和+差)/2 b=和-a

Q10929: You can say 11


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1870

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
     while(scn.hasNext()){
      String str=scn.nextLine();
      if(str.equals("0"))break;
      char[]arr=str.toCharArray();
      int a=0,b=0;
      for(int i=0;i<arr.length;i++){
       if(i%2==0)a+=arr[i]-'0';
       else b+=arr[i]-'0';
      }
      if((a-b)%11==0)
       System.out.println(str+" is a multiple of 11.");
      else
       System.out.println(str+" is not a multiple of 11.");
     }
 }
 /* 
    題目:Q10929: You can say 11
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題測資過大所以要以字串存取然後再把偶數位的和和奇數位的和的差去判斷是否為11的倍數

2016年7月4日 星期一

Q11332: Summing Digits


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=2307&mosmsg=Submission+received+with+ID+17617814
方法一
import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while(scn.hasNext()){
   String str=scn.next();
   if(str.equals("0"))break;
   int tot=0;
   while(true){
    String arr[]=str.split("");
    if(arr.length==1){
     tot=Integer.parseInt(arr[0]);
     break;
    }
    tot=0;
    for(int i=0;i&ltarr.length;i++){
     tot+=Integer.parseInt(arr[i]);
    }
    str=Integer.toString(tot);
   }
   System.out.println(tot);
  }
 }

 /* 
    題目:Q11332: Summing Digits 
    作者:1010
    時間:西元 2016 年 7 月 */
}

方法二
import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   int n = scn.nextInt();
   if (n == 0)
    break;
   while (n / 10 != 0) {
    n = n % 10 + n / 10;
   }
   System.out.println(n);
  }
 }
 /* 
    題目:Q11332: Summing Digits
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題是我當初參加CPE的第一題,這題我是利用字串去解當長度為1時跳出迴圈,還要注意當數入個位數時要直接輸出

Q10931: Parity


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1872

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   int n = scn.nextInt();
   if (n == 0)
    break;
   System.out.printf("The parity of %s is %d (mod 2).\n",Integer.toBinaryString(n), Integer.bitCount(n));
  }
 }
}


困難度 
這題利用Java內建函示庫 Integer.toBinaryString()以及Integer.bitCount()