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看看能否被整除就行拉~