2017年11月28日 星期二

Q272 : TEX Quotes

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int count =0;
while(scn.hasNext()) {
char str[]=scn.nextLine().toCharArray();
for(int i=0;i<str.length;i++) {
if(str[i]=='\"') {
if(count%2==0)
System.out.print("``");
else
System.out.print("''");
count++;
}else
System.out.print(str[i]);
}
System.out.println();
}
}
}

這題別想太複雜把獨到的每列字串轉成字元陣列再一個一個下去找雙引號,記住每行 ` ' 的順序延續上一行,所以我把count放在最外層

範例測資:
Input:

"""

"""
Output:

``''``

''``''

2017年10月17日 星期二

ITSA第58次月賽 Problem 3. 完整二元樹

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

import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
int t=Integer.parseInt(scn.nextLine());
while(t--!=0) {
int n=Integer.parseInt(scn.nextLine()),index=1,x=0,b=0;
StringTokenizer tokens = new StringTokenizer(scn.nextLine(),"(),");
int arr[]=new int [tokens.countTokens()/2];
while(tokens.hasMoreTokens()) {
if(index%2==0)
arr[x++]=Integer.parseInt(tokens.nextToken());
else
tokens.nextToken();
index++;
}
for(int i=1;i<=Math.ceil(arr.length/2);i++){
if(Math.abs(arr[i-1]-arr[2*i-1])<=n) {
if(b!=0)
System.out.print(" ");
System.out.printf("%c%c",i+64,i*2+64);
b=1;
}
if(Math.abs(arr[i-1]-arr[2*i])<=n) {
if(b!=0)
System.out.print(" ");
System.out.printf("%c%c",i+64,i*2+65);
b=1;
}
}
System.out.println();
}
}
/*題目:ITSA第58次月賽 Problem 3. 完整二元樹
作者:1010
時間:西元 2017 年10 月 */
}
這題不要看到是樹就想得很害怕又複雜,其實他一點跟樹的演算法都無關仔細看看可以發現規律並用簡單數學就能推出答案囉!


ITSA第58次月賽 Problem 2. 道路修補

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

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 t=scn.nextInt(),max=0,tot=0;
Set<Integer> set = new HashSet<Integer>();
for(int i=0;i<t;i++) {
int x=scn.nextInt(),y=scn.nextInt();
for(int j=x+1;j<=y;j++) {
set.add(j);
}
}
System.out.println(set.size());
}
}
/*題目:ITSA第58次月賽 Problem 2. 道路修補
作者:1010
時間:西元 2017 年10 月 */
}

這題有兩種做法第一種最直覺建立長度10000的陣列把需要修補的道路依序塞入數值,另外找出裡面數值最大的數最後算修補的道路就從0~amx就好囉
第二種方法就是利用java中的set容器囉,他會自動地把重複數字砍掉最後算set的總長度就是答案囉!

ITSA第58次月賽 Problem 1. 計算正整數被3整除之數值之總和

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

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0) {
int num=scn.nextInt(),tot=0;
for(int i=1;i<=num/3;i++) {
tot+=3*i;
}
System.out.println(tot);
}
}
/*題目:ITSA第58次月賽 Problem 1. 計算正整數被3整除之數值之總和
作者:1010
時間:西元 2017 年10 月 */
}

這題可以用模擬的方式單迴圈每次+3依序把整除數相加就是答案了

2017年10月9日 星期一

ITSA 第57次月賽 Problem 1. QWERTY

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

import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int n=Integer.parseInt(scn.nextLine());
while(n--!=0) {
String s[]=scn.nextLine().split(" ");
int arr[]=new int [Integer.parseInt(s[0])],tot=0,num=0,pre=0;
for(int i=0;i<arr.length;i++)
arr[i]=Integer.parseInt(s[i+1]);
Arrays.sort(arr);
for(int i=0;i<arr.length;i++) {
num+=pre;
tot+=num;
pre=arr[i];
}
System.out.println(tot);
}
}
/*題目:Problem 5. The Job Scheduling Problem
作者:1010
時間:西元 2017 年10 月 */
}

這題鍵盤還包含標點符號

ITSA 第57次月賽 Problem 5. The Job Scheduling Problem

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

import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int n=Integer.parseInt(scn.nextLine());
while(n--!=0) {
String s[]=scn.nextLine().split(" ");
int arr[]=new int [Integer.parseInt(s[0])],tot=0,num=0,pre=0;
for(int i=0;i<arr.length;i++)
arr[i]=Integer.parseInt(s[i+1]);
Arrays.sort(arr);
for(int i=0;i<arr.length;i++) {
num+=pre;
tot+=num;
pre=arr[i];
}
System.out.println(tot);
}
}
/*題目:Problem 5. The Job Scheduling Problem
作者:1010
時間:西元 2017 年10 月 */
}

這題要先暸解Shortest jop first(sjf)
意思是花費最小的工作時間先執行
所以這題必須要排序,之後陣列走訪依序加上等待時間

2017年10月8日 星期日

Q263: Number Chains

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
while(true) {
int n=scn.nextInt(),tot=0,count=1;
if(n==0)
break;
String arr[]=Integer.toString(n).split(""),x="",y="";
Set<Integer> set = new HashSet<Integer>();
System.out.println("Original number was "+n);
Z:
while(true) {
Arrays.sort(arr);
x="";y="";
for(int i=0;i<arr.length;i++) {
y+=arr[i];
x+=arr[arr.length-1-i];
}
tot=Integer.parseInt(x)-Integer.parseInt(y);
for(int i:set) {
if(tot==i) {
System.out.println(Integer.parseInt(x)+ " - " + Integer.parseInt(y) +" = "+tot);
System.out.println("Chain length "+count);
System.out.println();
break Z;
}
}
System.out.println(Integer.parseInt(x)+ " - " + Integer.parseInt(y) +" = "+tot);
arr=Integer.toString(tot).split("");
count++;
set.add(tot);
}
}
}
/*題目:Q263: Number Chains
作者:1010
時間:西元 2017 年10 月 */
}

這題就把該字串由大到小剪掉小到大的數串得出的解看是否有重複
ex: 12345
54321-12345=41976
這裡要注意輸出有01234要把頭的0去掉
為了避免TL這邊我用set來儲存不重複的數值

2017年9月28日 星期四

Q299: Train Swapping

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

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t--!=0) {
int n=scn.nextInt(),arr[]=new int [n],count=0;
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
}
//泡沫排序
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++){
if(arr[i]>arr[j]) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
count++;
}
}
}
System.out.printf("Optimal train swapping takes %d swaps.\n",count);
}
}
/*題目:Q299: Train Swapping
作者:1010
時間:西元 2017 年9 月 */
}

106.09.26 CPE第二題
這題就用你喜歡的排序法實作並用一個count計算交換次數就行囉!

Q11743: Credit Check

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

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine());
while (n-- != 0) {
String arr[] = scn.nextLine().split(" ");
int tot = 0;
for (int i = 0; i < arr.length; i++) {
char ary[] = arr[i].toCharArray();
for (int j = 0; j < ary.length; j++) {
if (j % 2 == 0)
tot = tot + (ary[j] - '0') * 2 % 10 + (ary[j] - '0') * 2 / 10;
else
tot += ary[j] - '0';
}
}
if (tot % 10 == 0)
System.out.println("Valid");
else
System.out.println("Invalid");
}
}
/*題目:Q11743 : Credit Check
作者:1010
時間:西元 2017 年9 月 */
}

106.09.26 CPE第一題
這題很簡單只是要驗證此信用卡數字串是否合法
他有說到奇數位乘以2再把每位數字加起來,當偶數位時直接該數加起來不做任何加成
最後算出來有個加總值當最後一位為0時就是合法

最後判斷直接用%10看看能否被整除就行拉~

2017年7月24日 星期一

Android Studio Git簡單上傳到GitHub

上篇教學Android Studio初始化設定教到如何建置Git環境
本篇教學會教您如何把畚箕端的專案上Git並儲存到遠端資料庫GitHub當中

前置作業:
1.到GitHub申請一組個人帳號
2.切記要安裝Git套件(在上篇教學最後有提到)

首先:
在搜尋列開啟Git Bash若沒看到請安裝Git套件哦,並非系統內建cmd
開啟後把先前所建好的Android專案的資料夾打開找到路徑在Git Bash中輸入
cd *** (***為你的專案實際位置,這邊有個小技巧,您可以使用拖拉方式到Git Bash路徑就會自動跑出來囉)



















第二步:
初始化Git專案指令=>git init
之後在Android Studio介面中就會自動偵測到你有VCS root






git status
可以查詢您目前的專案中Git狀態,像圖中尚未commit所以偵測到下列檔案未
Git




















第三步:
把資料上索引,這邊你把它想成我要提交一次Git要寫申請書告知有哪些檔案
git add . (注意後面有一點哦)



















第四步:
提交commit此步驟還只是在您的本機中建立Git紀錄哦
git commit –m ‘第一次上傳’  (單引號內為備註使用者可自行說明此次上傳說明)




















之後你會發現它會要求您輸入資訊,誰上傳誰留下痕跡當然要留下你的資訊才知道這份文件是誰修改的呀!
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
最後在下一次Git commit –m ‘第一次上傳就成功囉!
到上述步驟其實就已間簡單完成版本控制了,但還尚未結束先前不是有提到GitHub嗎?
這部份我們使用GitHub作為我們專案儲存的地方當然你也可以使用其他的例如BitBucket等...
GitHub是什麼?你可以簡單想成它是一個遠端的數據庫也就是一個讓你可以儲存Code的空間
有一種Google drive的概念,不過他的特點是你啥時修改上傳平台上一一的做紀錄,所以哪天某位合作夥伴不小心寫壞了專案第一時間可以知道是誰搞的鬼,另一方面也可以及時調回前一版本的檔案。


第五步:
首先到GitHub註冊帳號登入進去後新增第一個Repositories輸入你的專案名稱這裡寫GitTest





















第六步:
Creat之後會給你一段Git程式碼把它複製貼放你的Git bash就可以把本地端的檔案上傳到據庫囉
git remote add origin https://github.com/andy6804tw/GitTest.git

git push -u origin master






















最後上傳成功回到網頁上重新整理就會發先檔案已經全部上傳囉!
是不是很快速又方便,雖然網路上有許多Git的GUI圖形介面化的軟體,簡單按個幾下也可以
做出上步驟,所以下篇教學打算來完整說明Git的運作流程與圖形化界面的操作。

2017年7月18日 星期二

Android Studio初始化設定

哈囉大家好!
今天要來教各位基本的Android環境設定與如何建置版本控制環境
首先,到Android Studio的官網下載開發環境(容量蠻大的)
https://developer.android.com/studio/index.html

1.安裝好後就直接執行它吧!首次打開可以新建一個專案,在這部分鍵入專案名稱,company domain視情況命名,這跟之後上架App比較有關連,選好儲存路徑後就持續下一步開啟一個Empty的專案吧。


2.接下來我們來做簡單環境設定,個人喜歡黑色版面(顧眼睛)
    File->Settings->Appearance 選取主題Theme選取Darcula






















3.有時候貼上程式碼或是自己寫Code時常常要引入函式庫,選取自動匯入系統就會偵測依照您目前狀態匯入適合的library
 File->Settings->Editor->General->Auto Import 選取All自動匯入lib






















4.版本控制是製作專案的最重要部分,這這片文章中先教各位檢察環境
建議安裝Android Studio之前先到以下網站下載Git : https://git-scm.com/
之後再Android StudioFile->Settings->Version Control->Git 點選Test測試看看有沒有連動萬一沒有的話請選擇當初安裝Git的磁碟機底下的位置,基本上安裝預設都會在這C:\Program Files\Git\cmd\git.exe























下篇教學再教各位如何使用Git bash命令提示字元方式做版本控制,且上傳到遠端GitHub中 !

2017年6月13日 星期二

[C_AR154-易] 感染被包圍的人

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String arr[][][] = new String[7][7][2];
for (int i = 0; i < 7; i++) {
String str[] = scn.nextLine().split(" ");
for (int j = 0; j < 7; j++) {
arr[i][j][0] = str[j];
arr[i][j][1] = "0";
if (i == 0 || i == 6) {
if (arr[i][j][0].equals("0"))
arr[i][j][1] = "1";
}
}
if (arr[i][0][0].equals("0"))
arr[i][0][1] = "1";
if (arr[i][6][0].equals("0"))
arr[i][6][1] = "1";
}
for (int i = 1; i < 6; i++) {
for (int j = 1; j < 6; j++) {
if ((j != 0 || j != 6) && arr[i][j][0].equals("0")) {
if (arr[i][j - 1][1].equals("1") || arr[i + 1][j][1].equals("1") || arr[i][j + 1][1].equals("1")
|| arr[i - 1][j][1].equals("1")) {
arr[i][j][1] = "1";
}
}
}
}
for (int i = 5; i > 0; i--) {
for (int j = 5; j > 0; j--) {
if ((j != 0 || j != 6) && arr[i][j][0].equals("0")) {
if (arr[i][j - 1][1].equals("1") || arr[i + 1][j][1].equals("1") || arr[i][j + 1][1].equals("1")
|| arr[i - 1][j][1].equals("1")) {
arr[i][j][1] = "1";
}
}
}
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (j != 0)
System.out.print(" ");
if (arr[i][j][0].equals("X"))
System.out.print("X");
else if (arr[i][j][1].equals("1"))
System.out.print("0");
else
System.out.print("I");
}
System.out.println();
}
}
/*題目:[C_AR154-易] 感染被包圍的人
作者:1010
時間:西元 2017 年6 月 */
}
這題思考一下只要沾到邊是0的就表示不會感染

範例1
輸入:
X X X X X X X
X X X X X X X
X X X X X X X
X X X X X X X
X X X X X X X
X X 0 0 X X X
X X 0 X X X X
輸出:
X X X X X X X
X X X X X X X
X X X X X X X
X X X X X X X
X X X X X X X
X X 0 0 X X X
X X 0 X X X X

範例2
輸入:
X X X X X X X
X X X X X X X
X X X X X X X
X X 0
X X X X X X X
X X X X X X X
X X X X X X X
輸出:
X X X X X X X
X X X X X X X
X X X X X X X
X X 0
X X X X X X X
X X X X X X X
X X X X X X X

範例3
輸入:
X X X X 0 X X
0 0 0 X 0 X X
X X 0 0 X 0 X
X X X X X X X
X X X X X X X
X 0 0 0 0 0 X
X X X 0 X X X
輸出:
X X X X 0 X X
0 0 0 X 0 X X
X X 0 0 X I X
X X X X X X X
X X X X X X X
X 0 0 0 0 0 X
X X X 0 X X X


[C_AR201-易] 城市地圖

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

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 k=scn.nextInt(),max=0,arr[][]=new int[k][3];
for(int i=0;i<k;i++){
arr[i][0]=scn.nextInt();arr[i][1]=scn.nextInt();arr[i][2]=scn.nextInt();
if(arr[i][1]>max)
max=arr[i][1];
}
int ary[]=new int[max+1];
for(int i=0;i<k;i++){
for(int j=arr[i][0];j<=arr[i][1];j++){
if(ary[j]<arr[i][2])
ary[j]=arr[i][2];
}
}
for(int i=0;i<=max;i++){
if(i!=0)
System.out.print(" ");
System.out.print(ary[i]);
}
System.out.println();
}
}
/*題目:[C_AR201-易] 城市地圖
作者:1010
時間:西元 2017 年6 月 */
}

這題就往陣列去想把左邊起點和右邊起點想像成一個一為陣列的範圍
在這之中先把陣列大小找出來max
最後依序把範圍內的值若大於原本的舊覆蓋過去
arr[ 12 ]
0~8=>5
6~8=>10
10~12=>10
5 5 5 5 5 5 10 10 10 0 10 10 10

2017年5月6日 星期六

[C_SO42-易] 宵夜

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

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[4];
while(n--!=0){
for(int i=0;i<4;i++)
arr[i]=scn.nextInt();
Arrays.sort(arr);//排序
int x=arr[0]*2+arr[1]+arr[2]+arr[3],y=arr[1]*3+arr[0]+arr[3];
System.out.printf("%d\n",x>y?y:x);
}
}
/*題目:[C_SO42-易] 宵夜 JAVA寫法
作者:1010
時間:西元 2017 年 5 月 */
}
#include<stdlib.h>
int compare(const void *arg1, const void *arg2) {
return (*(int *)arg1 - *(int *)arg2);
}
int main(){
int n=0,arr[4],i=0;
scanf("%d",&n);
while(n--){
for(i=0;i<4;i++)
scanf("%d",&arr[i]);
qsort((void *)arr, 4, sizeof(int), compare);//排序
int x=arr[0]*2+arr[1]+arr[2]+arr[3],y=arr[1]*3+arr[0]+arr[3];
printf("%d\n",x>y?y:x);
}
/*題目:[C_SO42-易] 宵夜 C語言寫法
作者:1010
時間:西元 2017 年 5 月 */
}

這題是考智力測驗= ( 網路搜尋提燈龍過河或是傳教士與食人族過河都是很經典的題目
這題考法也類似,若以很直覺的每次跟最小權重過去勢必不是最佳解,如下圖:

切記前提記得要排序,而且若單純case1下去做也不一定是最佳解
ex:
    1  98  99  100
這時要以case2下去做,也就是最直覺方法才是最佳解

2017年5月3日 星期三

948 - Fibonaccimal Base

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
ArrayList<Integer>fabList=new ArrayList<Integer>();
fabList.add(1);fabList.add(2);
int f=3;
while(f<=100000000){
fabList.add(f);
int i=fabList.size();
f=fabList.get(i-2)+fabList.get(i-1);
}
int n=scn.nextInt();
while(n--!=0){
int num=scn.nextInt(),lab=0,tot=0;
String ans="";
for(int i=fabList.size()-1;i>=0;i--){
if(fabList.get(i)<=num){
lab=i;
break;
}
}
for(int i=lab;i>=0;i--){
if(tot+fabList.get(i)>num){
ans+="0";
}else{
tot+=fabList.get(i);
ans+="1";
}
}
System.out.printf("%d = %s (fib)\n",num,ans);
}
}
/*題目:948 - Fibonaccimal Base
作者:1010
時間:西元 2017 年 5 月 */
}

這題簡單來說10
費事數列: 1 2 3 5 8

               0 1 0 0 1
Ans:10010
題目有說過最大值為
100000000所以建立該區間的費事數列

2017年4月28日 星期五

[C_AR168-易] 最大方塊區域

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

#include<stdlib.h>
#include<stdio.h>
int main(){
int n,arr[20][20],i,j,m,p,q,num;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&arr[i][j]);
}
}
scanf("%d",&num);
for(m=n;m>0;m--){
for(i=0;i<=n-m;i++){
for(j=0;j<=n-m;j++){
int b=1;
for(p=0;p<m;p++){
for(q=0;q<m;q++){
if(arr[p+i][q+j]!=num){
b=0;
break;
}
}
}
if(b){
goto z ;
}
}
}
}
z:
printf("%d\n",m);
}
/*題目:[C_AR168-易] 最大方塊區域
作者:1010
時間:西元 2017 年 4 月 */
}

這題簡單來說就是利用一個mask去罩,從最大值n開始逐一遞減

2017年4月10日 星期一

[C_AR151-易] 井字遊戲判斷

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
int arr[][]=new int[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j]=scn.nextInt();
String s="";
for(int i=0;i<3;i++){
if(arr[i][0]==arr[i][1]&&arr[i][1]==arr[i][2]&&arr[i][0]==n){
s+="All "+n+"'s on row "+i+"\n";
}
}
for(int i=0;i<3;i++){
if(arr[0][i]==arr[1][i]&&arr[1][i]==arr[2][i]&&arr[0][i]==n){
s+="All "+n+"'s on column "+i+"\n";
}
}
if(arr[0][0]==arr[1][1]&&arr[1][1]==arr[2][2]&&arr[0][0]==n)
s+="All "+n+"'s on diagonal\n";
if(arr[0][2]==arr[1][1]&&arr[1][1]==arr[2][0]&&arr[0][2]==n)
s+="All "+n+"'s on subdiagonal\n";
if(s.length()>0)
System.out.print(s);
else
System.out.println("There is no line with all "+n);
}
/*題目:[C_AR151-易] 井字遊戲判斷
作者:1010
時間:西元 2017 年 4 月 */
}

這題就是把八種連線可能方式一一去檢查,若完全無則輸出There is no line with all ?
(?為該數)

[C_AR130-易] 拉彩金

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

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 num=scn.nextInt(),arr[]=new int[num],max=0;
for(int i=0;i<num;i++){
arr[i]=scn.nextInt();
}
int sum=0,max_sum=0;
for(int j=0;j<num;j++)
{
sum+=arr[j];
sum=Math.max(0,sum);
max_sum=Math.max(max_sum,sum);
}
System.out.println(max_sum);
}
}
/*題目:[C_AR130-易] 拉彩金
作者:1010
時間:西元 2017 年 4 月 */
}

這題陣列走訪,取得最少時間複雜度用單迴圈就行了,每走訪一個就加到sum,並每次判斷sum是否小於0若成立歸零Math.max()就是比較大小函式,比對確認後最後再比目前最大的數字並存max_sum

2017年4月9日 星期日

[C_MM246-易] 矩陣反轉

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
int x=scn.nextInt(),y=scn.nextInt(),arr[][]=new int[y][x];
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
arr[j][i]=scn.nextInt();
for(int i=0;i<y;i++){
for(int j=0;j<x;j++){
if(j!=0)
System.out.print(" ");
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}
/*題目:[C_MM246-易] 矩陣反轉
作者:1010
時間:西元 2017 年 4 月 */
}

這題矩陣反轉就是建立二維陣列下去實作,輸入時索引值顛倒

[C_SO37-易] 不誠實不給獎

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

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],count=0;
for(int i=0;i<n;i++)
arr[i]=scn.nextInt();
Arrays.sort(arr);
for(int i=n-1;i>=0;i--){
if(count==2)
break;
else if(arr[i]<90){
System.out.println("x");
count++;
break;
}
else if(arr[i]%2!=0){
continue;
}
else{
System.out.println(arr[i]);
count++;
}
}
if(count==1)
System.out.println("x");
}
/*題目:[C_SO37-易] 不誠實不給獎
作者:1010
時間:西元 2017 年 4 月*/
}

這題就是找出前兩名必須要90分以上若無就x注意以下測資
3
98 97 97
必須要輸出
98
x
所以要有一個計數器count紀錄印出幾個了

2017年4月8日 星期六

[C_SO45-易] 混合數列

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr[]=new int [50],count=0;
for(int i=1;i<=25;i++){
arr[count++]=(int)Math.pow(3, i);
arr[count++]=(int)Math.pow(4, i);
}
Arrays.sort(arr);
int j=0,tot=0,n=scn.nextInt();
for(;j<n;j++)
tot+=arr[j];
System.out.println(arr[j-1]+","+tot);
}
/*題目:C_SO45-易 混合數列
作者:1010
時間:西元 2017 年 4 月*/
}

這題就依序地將3與4的次方數1~25算出來存入陣列中(題目設定最大50代表各25個次方)
最後一定要再做排序

[C_ST22-易] 迴文字串 II

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
char s[]=scn.nextLine().toCharArray();
for(int i=s.length-1;i>=0;i--){
if(s[i]>64&&s[i]<91)
System.out.printf("%c",s[i]+32);
else if(s[i]>96&&s[i]<123)
System.out.printf("%c",s[i]-32);
else
System.out.print(s[i]);
}
System.out.println();
}
/*題目:C_ST22-易 迴文字串 II
作者:1010
時間:西元 2017 年 4 月*/
}

我第一種寫法是利用字串去實作並利用toCharArray然後在利用字串toUpperCase、toLowerCase系統竟然回傳值1
所以最後只好乖乖地用成字串陣列依序判斷翻轉,那一樣也是利用ASCII碼判斷大小寫
A~Z是65~90
a~z是97~122

[C_ST03-易] 萬國碼轉成對應字元

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

#include<stdlib.h>
int main(){
int n;
scanf("%d",&n);
printf("%c\n",n);
/*題目:C_ST03-易 萬國碼轉成對應字元
作者:1010
時間:西元 2017 年 4 月*/
}

這題應該是ITSA所有題目程式碼最少行的題目吧...
這題很直覺的直接把獨到的整數用%c字元他就會自動轉成相對應的ASCII碼符號了

[C_MM261-易] 數字的出現次數

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt(), tot = n / 100 * 20;
Boolean b = true;
if (n / 100 == 8) {
tot += n % 100 + 1;
}
n %= 100;
tot += n / 10;
if (n > 88 && n / 10 != 8)
b = false;
if (n >= 80 && n < 90)
tot += n - 79;
else if (n >= 90)
tot += 10;
if (n >= 98)
tot++;
n %= 10;
if (n >= 8 && n <= 9 && b)
tot++;
System.out.println(tot);
}
/*題目:C_MM261-易 數字的出現次數
作者:1010
時間:西元 2017 年 4 月*/
}
這題就是算有幾個8注意當800多時是例外每增加一個數字就加一

2017年4月7日 星期五

[C_AR90-易] 天堂島居留證

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

#include<stdlib.h>
int main(){
int n;
scanf("%d",&n);
while(n--){
char arr[13];
scanf("%s",arr);
int num=0,weight[]={1,3,5,2,4,6,1,3,5,2,4,6},i=0;
for(;i<12;i++){
num+=(arr[i]-'0')*weight[i];
}
if(10-(num%10)==arr[12]-'0')
printf("yes\n");
else
printf("no\n");
}
/*題目:C_AR90-易 天堂島居留證
作者:1010
時間:西元 2017 年 4 月*/
}

這題用java交了21次始終回傳值1我也搞不懂問題在哪(但有人java繳交AC),最後索性用C寫竟然一次就過= =
這題就跟身分證檢驗類似,依照他的說明權重運算與最後一位相同就輸出yes反之

[C_AR92-易] 和尚端湯上塔

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
String arr[]=scn.nextLine().split(" ");
int count=0;
for(int i=1;i<=10;i++){
if(Integer.parseInt(arr[i-1])%i!=0)
count++;
}
if(count<=3)
System.out.println("yes");
else
System.out.println("no");
}
}
/*題目:C_AR92-易 和尚端湯上塔
作者:1010
時間:西元 2017 年 4 月*/
}

這題就是把每位數去除若無法整除就表示滑倒,滑倒3次以內(包含3)就輸出yes反之
總共十層所以被除數依序從1~10

[C_AR94-易] 洗刷刷

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

#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
char str1[100],str2[100];
scanf("%s%s",str1,str2);
int i=0;
for(i=0;i<strlen(str1);i++){
if(str1[i]==(str2[i])||(abs((str1[i]-'0')-(str2[i]-'0'))>1&&abs((str1[i]-'0')-(str2[i]-'0'))!=4))
printf("和");
else if(abs((str1[i]-'0')-(str2[i]-'0'))==4){
if(str1[i]-'0'>str2[i]-'0')
printf("輸");
else
printf("贏");
}
else{
if(str1[i]-'0'>str2[i]-'0')
printf("贏");
else
printf("輸");
}
}
printf("\n");
/*題目:C_AR94-易 洗刷刷
作者:1010
時間:西元 2017 年 4 月 7日*/
}

依照題意兩數相差大於1就是和還有一個例外就是1比5大其餘就按照比大小去比
注意!ITSA似乎不能接受JAVA國字的題目(使用萬國碼也無法AC)

[C_AR73-易] 兔子生育計算

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

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 num=scn.nextInt(),i=1,a=1,b=1,fab;
while(i<=num){
if(i!=1)
System.out.print(" ");
if(i==1|i==2)
System.out.print("1");
else{
fab=a+b;
a=b;
b=fab;
System.out.print(fab);
}
i++;
}
System.out.println();
}
}
/*題目:C_AR73-易 兔子生育計算
作者:1010
時間:西元 2017 年 4 月 7日*/
}
這題就只是單存的費事數列

2017年4月6日 星期四

[C_AR61-易] 配對

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

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 arr[][]=new int[4][4];
for(int i=0;i<4;i++){
scn.next();
while(true){
int num=scn.nextInt();
if(num==0)
break;
else
arr[i][num-1]=1;
}
}
int count=0;
for(int i=0;i<4;i++){
if(arr[0][i]==1)
continue;
for(int j=0;j<4;j++){
if(arr[1][j]==1)
continue;
for(int k=0;k<4;k++){
if(arr[2][k]==1)
continue;
for(int l=0;l<4;l++){
if(arr[3][l]==1)
continue;
else{
int temp[]=new int[4];
temp[i]=1;temp[j]=1;temp[k]=1;temp[l]=1;
if(temp[0]+temp[1]+temp[2]+temp[3]==4){
if(count==0)
System.out.println("ABCD");
System.out.println((i+1)+""+(j+1)+""+(k+1)+""+(l+1));
count++;
}
}
}
}
}
}
if(count==0)
System.out.println("No Solution");
System.out.println();
}
}
/*題目:C_AR61-易 配對
作者:1010
時間:西元 2017 年 4 月 7日*/
}
這題就只是考陣列而已簡單來說找出每位地差集1代表沒興趣0表示有興趣,4個迴圈下去跑組合,每次還需要盤段是否為"愉快"的狀態(表1234每位都有分配到沒重複搶人狀況發生),結束時必須在換一行

Q10093: An Easy Problem!

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
char arr[]=scn.nextLine().trim().toCharArray();
int tot=0,max=1;
for(int i=0;i<arr.length;i++){
if(arr[i]>='0'&&arr[i]<='9'){
if(arr[i]-'0'>max){
max=arr[i]-'0';
}
tot+=arr[i]-'0';
}
else if(arr[i]>='A'&&arr[i]<='Z'){
if(arr[i]-'A'+10>max){
max=arr[i]-'A'+10;
}
tot+=arr[i]-'A'+10;
}
else if(arr[i]>='a'&&arr[i]<='z'){
if(arr[i]-'a'+36>max){
max=arr[i]-'a'+36;
}
tot+=arr[i]-'a'+36;
}
}
int i=0;
for(i=max;i<62;i++){
if(tot%i==0){
System.out.println(i+1);
break;
}
}
if(i==62)
System.out.println("such number is impossible!");
}
}
/*題目:Q10093: An Easy Problem!
作者:1010
時間:西元 2017 年 4 月 7日*/
}

這題主要是找該數的每位數最大的基底,最後再查看該數能夠被最小基底的數整除的基底數

2017年3月28日 星期二

Q496 : Simply Subsets

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
String str1[] = scn.nextLine().split(" "), str2[] = scn.nextLine().split(" ");
int arr1[] = new int[str1.length], arr2[] = new int[str2.length];
for (int i = 0; i < arr1.length; i++)
arr1[i] = Integer.parseInt(str1[i]);
for (int i = 0; i < arr2.length; i++)
arr2[i] = Integer.parseInt(str2[i]);
Arrays.sort(arr1);
Arrays.sort(arr2);
if (arr1.length == arr2.length) { // 先判斷兩串列依樣長時
int i = 0;
int count = 0;
for (i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
count++;
}
}
}
if (count == arr1.length)
System.out.println("A equals B");
else if (count == 0)
System.out.println("A and B are disjoint");
else
System.out.println("I'm confused!");
} else if (arr1.length > arr2.length) {// 當arr1(A)長度比arr2(B)長時
int count = 0;
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
count++;
break;
}
}
}
if (count == arr2.length)
System.out.println("B is a proper subset of A");
else if (count > 0)
System.out.println("I'm confused!");
else
System.out.println("A and B are disjoint");
} else if (arr1.length < arr2.length) {// 當arr1(A)長度比arr2(B)短時
int count = 0;
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
count++;
break;
}
}
}
if (count == arr1.length)
System.out.println("A is a proper subset of B");
else if (count > 0)
System.out.println("I'm confused!");
else
System.out.println("A and B are disjoint");
}
}
}
/*
題目:Q496 : Simply Subsets
作者:1010
時間:西元 2017 年 3 月 28 日*/
}

這題只是數值比對有四種情況
1.A equals B (兩串列相等)
2.A is a proper subset of B(A是B的子集)
3.B is a proper subset of A(B是A的子集)
4.A and B are disjoint(兩串列完全不同,沒交集)
5.I'm confused!(有相等的數字但不構成子集,如{1,2} {2,3} 或是 {1,2,3} {3,4} )

這題建議不要使用uDebug的測資因為內有例外測資,簡單來說就是沒有重複數字的集合
The judge's input most likely does not contain any negative integers and sets like so
3 3 3 2
3 3 3 3 3 3 2
However, both cases are handled correctly by the code for this problem on uDebug.

Q482: Permutation Arrays

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine());
while (n-- != 0) {
scn.nextLine();
String arr[] = scn.nextLine().split(" "), arr2[] = scn.nextLine().split(" "),
ans[] = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
ans[Integer.parseInt(arr[i]) - 1] = arr2[i];
}
for (int i = 0; i < ans.length; i++) {
System.out.println(ans[i]);
}
if (n != 0)
System.out.println();
}
}
/*題目:Q482: Permutation Arrays
作者:1010
時間:西元 2017 年 3 月 28日*/
}

Permutation顧名思義就是要交換排列,第一行n是接下來有幾筆測資,每筆測資第一行有一行空白要讀,輸出最後一個數字記得多加一個換行,但最後依筆測資不換行(這題光排版就被擺了一道,不過題目有說明)
每筆次資中有兩行,其中第一行以例子來說3 1 2是代表第二行測資每個數字的擺放位置,最簡單的實作方法就是再新增一個陣列依序擺放這些交換的數字囉,記得索引值index是從0開始




Q993: Product of digits

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

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
int b=9;
string ans="";
if(n==0)
{
cout << 0 << endl;
continue;
}
else if(n==1)
{
cout << 1 << endl;
continue;
}
while(b>1 && n>1)
{
if(n%b==0)
{
ans+=(b+'0');
n/=b;
}
else
{
b--;
}
}
vector<char> v;
for(int i=0; i<(int)ans.length(); i++)
{
v.push_back(ans[i]);
}
sort(v.begin(), v.end());
ans="";
for(int i=0; i<(int)v.size(); i++)
{
ans+=v[i];
}
if(ans=="" || n>1)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
}
return 0;
}
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 num=scn.nextInt(),i=9,tot=1,temp=num;
StringBuilder str=new StringBuilder("");
if(num==1){
System.out.println("1");
continue;
}else if(num==0){
System.out.println("0");
continue;
}
while(i!=1){
if(num%i==0){
str.append(i);
tot*=i;
num/=i;
}else{
i--;
}
}
if(temp==tot)
System.out.println(str.reverse());
else
System.out.println("-1");
}
}
/*題目:Q993: Product of digits
作者:1010
時間:西元 2017 年 3 月 28日*/
}

這題其實不難,我當初想太複雜還用質數求公因數,但其實不是這樣的,題目只是要每個位數相乘等於你輸入的數目
舉例:90=>2*5*9 所以輸出259就好啦~很簡單吧
至於我是怎麼實作的,首先建立一個StringBuilder他類似於String型態但她可以呼叫reverse()字串翻轉很方便,那我們就從9開始除囉直到不等於1,若能整除依序append()到字串變數中,最後輸出reverse()就是最小數啦,那還要檢查最後如果每個位數相乘不等於輸入的數就輸出-1囉!
還有還有0跟1要個別判斷哦

2017年3月27日 星期一

Q11349: Symmetric Matrix

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t=Integer.parseInt(scn.nextLine()),count=1;;
while(t--!=0){
scn.next();scn.next();
int n=scn.nextInt();
String arr[]=new String [n*n];
Boolean b=true;
for(int i=0;i<n*n;i++)
arr[i]=scn.next();
for(int i=0,j=arr.length-1;i<n*n;i++,j--){
if(!arr[i].equals(arr[j])||arr[i].contains("-")){
b=false;
break;
}
}
if(b)
System.out.printf("Test #%d: Symmetric.\n",count++);
else
System.out.printf("Test #%d: Non-symmetric.\n",count++);
}
}
/*題目:Q11349:- Symmetric Matrix
作者:1010
時間:西元 2017 年 3 月 */
}

這題主要是要搞清楚對稱矩陣(Symmetric matrix)的特性以下是名詞解釋

若矩陣A與其對換矩陣AT相等,則A稱為對稱矩陣,對稱矩陣恆為一方陣。   對換矩陣AT是依序交換A中行與列後所得矩陣,亦即:(AT)ij=Aji(對所有i與j而言)

寫了這麼多看不懂的話聽我解釋~簡單來說低一個數字跟最後個數字要一樣第二個數字要跟倒數第二個數字一樣
我們再把二維陣列概念想成簡單的一維去時做就行了
記註記住對稱矩陣內不能有負數


Q1644: Prime Gap

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

/* package whatever; // don't place package name! */
import java.util.*;
class Main
{
public static void main (String[] args)
{
ArrayList<Integer> primeTable = new ArrayList<Integer>();
primeTable.add(2);
for(int i = 3;i<1000000 ; i += 2)
{
boolean check = true;
for(int j = 3; j * j <= i; j += 2)
{
if(i % j == 0)
{
check=false;
break;
}
}
if(check)
primeTable.add(i);
}
for(int i = 0; i < primeTable.size(); ++i)
System.out.println(primeTable.get(i));
}
/*題目:PrimeTable質數建表
作者:1010
時間:西元 2017 年 3 月 */
}
view raw PrimeTable.java hosted with ❤ by GitHub
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
ArrayList<Integer> primeTable = new ArrayList<Integer>();
primeTable.add(2);
for(int i = 3;i<=1299709 ; i += 2)
{
boolean check = true;
for(int j = 3; j * j <= i; j += 2)
{
if(i % j == 0)
{
check=false;
break;
}
}
if(check)
primeTable.add(i);
}
while(true){
int num=scn.nextInt(),i;
Boolean b=true;
if(num==0)
break;
for(i=0;i<primeTable.size();i++){
if(primeTable.get(i)==num){
b=false;
break;
}
else if(primeTable.get(i)>num){
break;
}
}
if(b){
int count=0;
for(int j=primeTable.get(i-1);j<primeTable.get(i);j++)
count++;
System.out.println(count);
}else{
System.out.println("0");
}
}
}
/*題目:Q1644: Prime Gap
作者:1010
時間:西元 2017 年 3 月 */
}
這題是2016/12/10CPE第四題,並不難主要是要先建立質數表才不會LTE
至於怎麼快速建表呢?簡單來說質數一定是奇數,不多說下面有質數建表範例

這題主要是找尋該數在質數的Gap當中有幾個非質數數目,若本身為質數就輸出零

[C_AR118-易] 硬幣組合

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr1[]=new int[10],arr2[]=new int [10];
Set<Integer> set = new TreeSet<Integer>();
for(int i=0;i<10;i++)
arr1[i]=scn.nextInt();
for(int i=0;i<10;i++)
arr2[i]=scn.nextInt();
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
set.add(arr1[i]+arr2[j]);
}
}
ArrayList<Integer>list=new ArrayList<Integer>(set);
for(int i=0;i<list.size();i++){
if((i)%10==0&&i!=0)
System.out.println();
if((i)%10!=0)
System.out.print(" ");
System.out.print(list.get(i));
}
System.out.println();
}
/*題目:[C_AR118-易] 硬幣組合方法一
作者:1010
時間:西元 2017 年 3 月 */
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int c, count = 0, num1[] = new int[10], num2[] = new int[10], ans[] = new int[100];
for (int i = 0; i < 10; i++)
num1[i] = scn.nextInt();
for (int i = 0; i < 10; i++)
num2[i] = scn.nextInt();
Arrays.sort(num1);
Arrays.sort(num2);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ans[i * 10 + j] = num1[i] + num2[j];
}
}
Arrays.sort(ans);
boolean b = false;
for (int i = 0; i < 100; i++) {
if (count == 10 && b) {
System.out.println();
count = 0;
}
for (c = i + 1; c < 100; c++)
if (ans[i] == ans[c])
break;
if (c == 100) {
if (count != 0)
System.out.print(" ");
System.out.print(ans[i]);
count++;
b = true;
} else
b = false;
}
System.out.println();
}
/*題目:[C_AR118-易] 硬幣組合方法二
作者:1010
時間:西元 2017 年 3 月 */
}
這題的組合簡單來說就是兩個串列中各挑一個數字相加,那他的時間複雜度就是O(10*10)
用雙迴圈就能算出所有組合了,但這邊要記住相加結果重複就不用再印出來了
Java有個很好用的容器較TreeSet,他不但可以做排序而且若遇到相同的數值會直接合併(捨去),簡單來說TreeSet是個集合它裡面幫你做好排序而且所有值不會重複具有唯一性
至於要怎麼把集合Set的值呼叫出來,這時就需要一個容器ArrayList的方式儲存集合裡的值才能去取得集合裡的資料
最後注意輸出是每十個為一行,每行最後一個不空白,最後結束還要換行!
ps.至於第二種寫法是大二時寫的單純只用到陣列輸出時再逐一判斷是否有重複(非常浪費時間,屬於笨方法)


2017年3月15日 星期三

Q11461:Square Numbers

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
ArrayList<Integer> list=new ArrayList<Integer>();
int i=1,num=1;
while(num<=100000){
list.add(num);
i++;
num=i*i;
}
while(true){
int a=scn.nextInt(),b=scn.nextInt(),count=0;
if(a==0&&b==0)
break;
if(a==b)
b++;
for(int k=0;k<list.size();k++){
if(list.get(k)>=a&&list.get(k)<=b)
count++;
}
System.out.println(count);
}
}
/*題目:Q11461:Square Numbers
作者:1010
時間:西元 2017 年 3 月 */
}

這題用ArrayList先建表,計算輸入a、b序列中有幾個是符合平方的數

2017年2月22日 星期三

[C_AR42-易] 過半元素

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
String arr[]=scn.nextLine().split(" ");
int i=0;
for(i=0;i<arr.length;i++){
int count=0;
for(int j=0;j<arr.length;j++){
if(arr[i].equals(arr[j]))
count++;
}
if(arr.length/2<count){
System.out.println(arr[i]);
break;
}
}
if(i==arr.length)
System.out.println("NO");
}
}
/*題目:[C_AR42-易] 過半元素
作者:1010
時間:西元 2017 年 2 月 */
}

這題由於不知有多少數列故用字串切割去比對過半數

ITSA 第50次月賽 [Problem 2] The Typhoon

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

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(),c=scn.nextInt(),d=scn.nextInt(),e=scn.nextInt(),f=scn.nextInt(),g=scn.nextInt()
,h=scn.nextInt(),i=scn.nextInt(),tot=0;
tot=a*e*i+d*h*c+g*b*f-c*e*g-b*d*i-a*f*h;
System.out.println(tot);
}
}
/*題目:ITSA 第50次月賽 [Problem 2] The Typhoon
作者:1010
時間:西元 2017 年 2 月 */
}

這題按照公式照打就行了~

ITSA 第50次月賽 [Problem 4] 偽造的金幣!!

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

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 num=scn.nextInt(),count=0;
while(num!=1){
num=(int)Math.ceil(num/3.);
count++;
}
System.out.println(count);
}
}
/*題目:ITSA 第50次月賽 [Problem 4] 偽造的金幣!!
作者:1010
時間:西元 2017 年 2 月 */
}
#include<stdio.h>
#include<math.h>
int main()
{
int N,n,i;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&n);
printf("%.0lf\n",ceil(log(n)/log(3)));
}
return 0;
/*題目:ITSA 第50次月賽 [Problem 4] 偽造的金幣!!
作者:1010
時間:西元 2017 年 2 月 */
}

這題背後原理是Divide&Conquer演算法,可以推導出只用一行程式的公式
 (1)假設N = 8袋, 先分成3、3、2 三大袋(A、B、C),先拿A、B秤,若A、B平衡,代表C有偽幣;反之A或B裡輕的那一大袋有偽幣
(2)若A是輕的有偽幣,將A袋分成1、1、1三大袋(D、E、F),先拿D、E秤,假如天秤不穩,代表輕的那一代就是偽幣;反之,F是偽幣

從這過程中,得知不斷將問題兩半切下(Divide)去比對(Conquer),正是這演算法的精神
至於怎變成公式,跟BigO(演算法複雜度)有關係

這種方法叫做prune & search,典型的演算法就是binary search,每比對一次,就可以去除一半不可能的,留下一半可能的繼續搜尋下去。而這一題,就是希望每秤一次,不管運氣好壞,留下來的越少越好

至於為什麼要取三堆呢?
分兩堆只能砍掉一半;分四堆以上,秤其中兩堆,運氣不好,只能砍掉這兩堆,其他留著繼續秤;分三堆,運氣不管好壞,都只會留下1/3

2017年2月21日 星期二

ITSA 第53次月賽 [Problem 3] 號碼鎖最少轉動幾次

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

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){
char str1[]=scn.next().toCharArray(),str2[]=scn.next().toCharArray();
int tot=0;
for(int i=0;i<str1.length;i++){
double a=str1[i]-'0',b=str2[i]-'0';
if(a<b){
double temp=b;
b=a;
a=temp;
}
tot+=Math.abs(a-b)>Math.abs(10-a)+b?(int)Math.abs(10-a)+b:(int)Math.abs(a-b);
}
System.out.println(tot);
}
}
/*題目:ITSA 第53次月賽 [Problem 3] 號碼鎖最少轉動幾次
作者:1010
時間:西元 2017 年 2 月 */
}

這題就使用貪婪比較從前面轉或從後面轉比較少次
大小比對這部分我是使用問號運算式當為真執行冒號前面,反之

ITSA 第53次月賽 [Problem 2] 洞穴裡的人

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

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 N=scn.nextInt(),M=scn.nextInt();
while(true){
N=(int)(N*(1/4.));
if(N<M)
break;
N+=M;
}
System.out.println(N);
}
}
/*題目:ITSA 第53次月賽[Problem 2] 洞穴裡的人
作者:1010
時間:西元 2017 年 2 月 */
}

這題就用迴圈每次乘以1/4.若進入人數大於裡面剩餘人數就退出,並印出最後剩餘人數

Q256: Quirksome Squares

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

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(),count=0;
int halfBound = (int)Math.pow(10, n / 2);
for(int i=0;i<halfBound;i++){
int num=i*i;
if(Math.pow(num/halfBound+num%halfBound,2)==num)
System.out.printf("%0"+n+"d\n",num);
}
}
}
/*題目:Q256: Quirksome Squares
作者:1010
時間:西元 2017 年 2 月 */
}

這題你會發現quirksome number是一個完全平方數例如81=9*9,n=4假如有四位數0~9999依序判斷肯定會TL超時
所以我們只要判斷1、4、9、16....到(10^(n/2)-1)*(10^(n/2)-1)
簡單來說當n=4時迴圈從0開始直到100結束,求0~99的完全平方來判斷是否為quirksome number

[C_AR023-易] 字根與子字串

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String s=scn.next(),t=scn.next();
if(t.contains(s))
System.out.println("YES");
else
System.out.println("NO");
}
/*題目:[C_AR023-易] 字根與子字串
作者:1010
時間:西元 2017 年 2 月 */
}

這題對java來說很簡單直接呼叫字串函式contains就能判別是否為子字串了

[C_AR40-易] 螺旋矩陣

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str[] = scn.nextLine().split(",");
int n = Integer.parseInt(str[0]), index = Integer.parseInt(str[1]), count = 1, arr[][] = new int[n][n];
if (index == 1) {//順時鐘
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - i - 1; j++)
arr[i][j] = count++;
for (int j = i; j < n - i - 1; j++)
arr[j][n - i - 1] = count++;
for (int j = n - i - 1; j > i; j--)
arr[n - i - 1][j] = count++;
for (int j = n - i - 1; j > i; j--)
arr[j][i] = count++;
}
} else {//逆時鐘
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - i - 1; j++)
arr[j][i] = count++;
for (int j = i; j < n - i - 1; j++)
arr[n - 1 - i][j] = count++;
for (int j = n - i - 1; j > i; j--)
arr[j][n - i - 1] = count++;
for (int j = n - i - 1; j > i; j--)
arr[i][j] = count++;
}
}
if (n % 2 != 0)//遇到基數層要補上中心數
arr[n / 2][n / 2] = count;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%03d", arr[i][j]);
if (j != n - 1)
System.out.print(",");
}
System.out.println();
}
}
/*題目:[C_AR40-易] 螺旋矩陣
作者:1010
時間:西元 2017 年 2 月 */
}
這題螺旋矩陣最直覺方法就是模擬,那如何做呢?首先不管順(逆)時鐘都有個規律,建議各位還是在紙上寫下每個位置的索引值如下:

[0,0] [0,1] [0,2] [0,3]
[1,0] [1,1] [1,2] [1,3]
[2,0] [2,1] [2,2] [2,3]
[3,0] [3,1] [3,2] [3,3]

看到這可能還是很模糊,我們在看下面這張圖搭配索引值發現他其實是有規律的,以順時鐘來說我們依序由上排佐至右,接下來又半部由上而下,然後下半部由右至左,最後左邊由下而上內層一樣按照此規律
那在這各位可能會問我們怎知道有幾層呢?把它當作洋蔥n=4時他有4/2=2層(迴圈跑兩次的意思,觀察索引值),那當遇到偶數時該怎麼辦呢?n=5,時就有5/2=2層,你可以發現他其實還有一層但他只有一個數字,可以把它當作成是圓的中心點個別處理arr[n / 2][n / 2] = count;




2017年2月20日 星期一

[C_MM86-易] 公因數問題

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int a=scn.nextInt(),b=scn.nextInt(),c=scn.nextInt();
while((a%=b)!=0&&(b%=a)!=0);
int d=a+b;
while((d%=c)!=0&&(c%=d)!=0);
System.out.print("Common factor in ascending order: ");
for(int i=2;i<=d+c;i++){
if((d+c)%i==0){
System.out.print(i);
if(i!=d+c)
System.out.print(" ");
}
}
System.out.println();
}
/*題目:[C_MM86-易] 公因數問題
作者:1010
時間:西元 2017 年 2 月 */
}

這題就是做兩次最大公因數(因為有三個數字),最後出來的數再做質因數分解就是答案了!
這是最件簡單的最大公因數公式(輾轉相除法)
while((a%=b)!=0&&(b%=a)!=0);

[C_ST84-易] 差集

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String arr1[]=scn.nextLine().split(","),arr2[]=scn.nextLine().split(",");
ArrayList<Integer> list=new ArrayList<Integer>();
int j=0;
for(int i=0;i<arr1.length;i++){
for(j=0;j<arr2.length;j++){
if(arr1[i].equals(arr2[j])){
break;
}
}
if(j==arr2.length)
list.add(Integer.parseInt(arr1[i]));
}
if(list.size()>0){
Collections.sort(list);
for(int i=0;i<list.size();i++){
if(i!=0)
System.out.print(" ");
System.out.print(list.get(i));
}
System.out.println();
}else{
System.out.println("null");
}
}
/*題目:[C_ST84-易] 差集
作者:1010
時間:西元 2017 年 2 月 */
}

差集就跟交集相反,程式碼也類似

[C_ST83-易] 聯集

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String arr1[]=scn.nextLine().split(","),arr2[]=scn.nextLine().split(",");
Set<Integer> set = new TreeSet<Integer>();
for(int i=0;i<arr1.length;i++)
set.add(Integer.parseInt(arr1[i]));
for(int i=0;i<arr2.length;i++)
set.add(Integer.parseInt(arr2[i]));
int count=0;
for(Integer s : set){
if(count++!=0)
System.out.print(" ");
System.out.print(s);
}
System.out.println();
}
/*題目:[C_ST83-易] 聯集
作者:1010
時間:西元 2017 年 2 月 */
}

這題使用Set集合來完成聯集,至於為什麼要用Set呢?原因是它是唯一實作SortedSet介面的類別,因此可針對Set中的元素進行排序

[C_ST82-易] 交集

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String arr1[]=scn.nextLine().split(","),arr2[]=scn.nextLine().split(",");
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
if(arr1[i].equals(arr2[j])){
list.add(Integer.parseInt(arr1[i]));
break;
}
}
}
if(list.size()>0){
Collections.sort(list);
for(int i=0;i<list.size();i++){
if(i!=0)
System.out.print(" ");
System.out.print(list.get(i));
}
System.out.println();
}else{
System.out.println("null");
}
}
/*題目:[C_ST82-易] 交集
作者:1010
時間:西元 2017 年 2 月 */
}

這題使用ArrayList動態配置交集數,並且使用Collections.sort做排序

2017年2月17日 星期五

[C_MM353-易] 找出一串輸入數字的中位數

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

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];
for(int i=0;i<n;i++)
arr[i]=scn.nextInt();
Arrays.sort(arr);
System.out.println(arr[n/2]);
}
/*題目:[C_MM353-易] 找出一串輸入數字的中位數
作者:1010
時間:西元 2017 年 2 月 */
}

這題利用Arrays.sort()做排序很快地就能找出中位數