2016年7月31日 星期日

[C_GM16-易] 堆積木

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

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
while (count-- != 0) {
int n = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), i = 0, temp = 0, j = 0;
for (i = 2; i <= a * b * c; i++) {
if (i % a == 0 && i % b == 0 && i % c == 0)
break;
}
for (j = 1; j <= n; j++) {
int num = i * j, tot = 0;
tot = (num / a) * (num / b) * (num / c);
if (tot > n)
break;
temp = tot;
}
if (temp == 0)
System.out.println("No solution");
else
System.out.println(temp);
}
}
/* 題目:[C_GM16-易] 堆積木
作者:1010
時間:西元 2016 年 8 月 */
}
這題先取三個數的最大公因數例如: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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt(),num=0;
for(int i=1;i<=n-2;i++){
for(int j=1;j<=n-2;j++){
num=n-(i+j);
if(num>n||num<=0)
break;
else
System.out.println(i+" "+j+" "+num);
}
}
}
/*
題目:[C_MM273-易] 鉛筆的分法
作者:1010
時間:西元 2016 年 7 月 */
}
這題利用雙迴圈和判斷就可以了

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

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

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(),max=0;
if(a<b)
max=b;
else
max=a;
for(int i=1;i<max;i++){
if(a%i==0&&b%i==0){
System.out.print(i+" ");
}
}
System.out.println();
}
/*
題目:[C_MM271-易] 所有公因數計算
作者:1010
時間:西元 2016 年 7 月 */
}
這題每個數字後面都有一個空格包括最後一筆資料也是

2016年7月29日 星期五

[C_CH27-易] 回文結構數字

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
int num1=scn.nextInt(),num2=scn.nextInt(),c=0;
for(int k=num1;k<=num2;k++){
char arr[]=Integer.toString(k).toCharArray();
int i,j;
for(i=0,j=arr.length-1;i<j;i++,j--){
if(arr[i]!=arr[j])
break;
}
if(i>=j){
if(c!=0)
System.out.print(" ");
System.out.print(k);
c++;}
}
if(c==0)
System.out.println("0");
else
System.out.println();
}
}
/*
題目:[C_CH27-易] 回文結構數字
作者:1010
時間:西元 2016 年 7 月 */
}
注意當沒輸出東西時要輸出0

2016年7月28日 星期四

[C_ST54-中] 最長相同數字子數列

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str=scn.next();
char arr[]=str.toCharArray(),c=arr[0];
int a=0,b=0,count=1,max=0,i=1;
for(i=1;i<arr.length;i++){
if(arr[i]==c){
count++;
continue;
}
else{
if(max<count){
max=count;
a=i-count;
b=i;
}
count=1;
c=arr[i];
}
}
if(c==arr[i-1]){
if(max<count){
a=i-count;
b=i;
}
}
System.out.println(str.substring(a,b));
}
/*
題目:[C_ST54-中] 最長相同數字子數列
作者:1010
時間:西元 2016 年 7 月 */
}
這題要注意到尾部假如還是一樣的話跳出迴圈後還是要再判斷max
例如:122333  程式25行會進入判斷最後一個數字是否也是最長數列

[C_ST56-易] 字串轉換

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str=scn.next();
char arr[]=str.toCharArray();
int tot=0,num=0;
for(int i=0;i<arr.length;i++){
if(arr[i]>='a'){
num='z'-(arr[i]-'a');
tot+=arr[i]-'a'+1;
System.out.printf("%c",num);
}
else{
num='Z'-(arr[i]-'A');
tot+=arr[i]-'A'+1;
System.out.printf("%c",num);
}
}
System.out.println(" "+tot);
}
/*
題目:[C_ST56-易] 字串轉換
作者:1010
時間:西元 2016 年 7 月 */
}
這題利用ASCii去判斷大小寫然後每個字母順序相反印出來

2016年7月27日 星期三

[C_AR121-易] 吃麵包

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n1=scn.nextInt(),n2=scn.nextInt(),bread[]=new int [n1];
for(int i=0;i<n1;i++)
bread[i]=scn.nextInt();
for(int i=0;i<n2;i++){
int x=scn.nextInt(),y=scn.nextInt(),tot=0;
for(int j=x-1;j<y;j++)
tot+=bread[j];
System.out.println(tot);
}
}
/*
題目:[C_AR121-易] 吃麵包
作者:1010
時間:西元 2016 年 7 月 */
}
這題很簡單只是拿陣列裡面的值

2016年7月26日 星期二

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

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1130
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(), s;
if (str.contains("0x")) {
s = str.substring(2);
System.out.println(Long.parseLong(s, 16));
}
else {
s = str;
System.out.println("0x" + Long.toHexString(Long.parseLong(s)).toUpperCase());
}
}
}
/*
題目:[C_MM081-易] 10進位及16進位
作者:1010
時間:西元 2016 年 7 月 */
}

十進位轉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

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().toLowerCase(), arr[] = str.split(" ");
int i = 0, j = 0;
System.out.print(arr[0] + " ");
for (i = 1; i < arr.length; i++) {
for (j = i - 1; j >= 0; j--) {
if (arr[j].equals(arr[i]))
break;
}
if (j == -1) {
if (i != 1)
System.out.print(" ");
System.out.print(arr[i].toLowerCase());
}
}
System.out.println();
}
}
/*
題目:[C_ST91-易] 英文斷詞
作者:1010
時間:西元 2016 年 7 月 */
}
第一組斷辭不比對先印出之後的每個單字逐一往前比對若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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int count=0;
while(scn.hasNext()){
int n=scn.nextInt(),arr[]=new int [n];
long max=0;
for(int i=0;i<n;i++)
arr[i]=scn.nextInt();
for(int i=0;i<n;i++){
long temp=1;
for(int j=i;j<n;j++){
temp*=arr[j];
if(temp>max)
max=temp;
}
}
System.out.printf("Case #%d: The maximum product is %d.\n\n",++count,max);
}
}
/*
題目:Q11059: Maximum Product
作者:1010
時間:西元 2016 年 7 月 */
}

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

2016年7月25日 星期一

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
double num=scn.nextDouble();
num=Math.pow(num/2,2)/Math.PI;
System.out.printf("%.6f\n",num);
}
}
/*
題目:[C_GM10-易] 小孩玩耍-繩子篇
作者:1010
時間:西元 2016 年 7 月 */
}
一條線所圍成的圖形,其邊數越多周長越長相對的面積也越大,所以想像成圓內接正多邊形,邊數越多趨近於圓
num除以2的兩倍除以PI

[C_SO51-易] 排程問題

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

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(), arr[] = new int[n], tot = 0, num = 0;
for (int i = 0; i < n; i++)
arr[i] = scn.nextInt();
Arrays.sort(arr);
for (int i = 0; i < n - 1; i++) {
num += arr[i];
tot += num;
}
System.out.println(tot);
}
}
/*
題目:[C_SO51-易] 排程問題
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是把數列排序再累加到n-1並把所有累加的數相加就是答案了

2016年7月22日 星期五

[C_CH19-易] 解方程

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

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(), tot = 0;
for (int i = 2; i <= num; i++) {
tot += ((i - 1) * i * (i + 1));
}
System.out.println(tot);
}
}
/*
題目:[C_CH19-易] 解方程
作者:1010
時間:西元 2016 年 7 月 */
}

[C_CH10-中] The 3n + 1 problem

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int a=scn.nextInt(),b=scn.nextInt(),max=0,count=0,num=0;
System.out.print(a+" "+b+" ");
if(a>b){
int temp=a;
a=b;
b=temp;
}
for(int i=a;i<=b;i++){
count=1;num=i;
while(num!=1){
if(num%2==0)
num/=2;
else
num=3*num+1;
count++;
}
if(count>max)
max=count;
}
System.out.println(max);
}
}
/*
題目:[C_CH10-中] The 3n + 1 problem
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是取兩個值之間找出最大的cycle length

[C_CH06-易] 判斷輸入變數的形式

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

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();
char c[] = str.toCharArray();
if (c.length == 1)
System.out.println("char");
else if (c.length > 1 && c[0] > '9')
System.out.println("string");
else if (str.indexOf('.') > 0)
System.out.println("float");
else
System.out.println("int");
}
}
/*
題目:[C_CH06-易] 判斷輸入變數的形式
作者:1010
時間:西元 2016 年 7 月 */
}
這題輸入到字串然後轉成陣列判斷它的長度假如是1就是字元,大於1並且字元ASCii>'0'就是字串
str.contains(".")判斷是否有小數點若有則為浮點數,其餘皆為整數
這題測資並沒有個位數字0~9假如要個位數判斷就在第10行增加&& c[0] > '9'判斷就行了

2016年7月21日 星期四

Q11942 : Lumberjack Sequencing

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
System.out.println("Lumberjacks:");
while (n-- != 0) {
int arr[] = new int[10], ary[] = new int[10], count1 = 0,count2=0;
for (int i = 0; i < 10; i++) {
arr[i] = scn.nextInt();
ary[i] = arr[i];
}
Arrays.sort(arr);
for (int i = 0, j = 9; i < 10; i++, j--) {
if (arr[i] == ary[i])
count1++;
else if(arr[i] == ary[j])
count2++;
else
break;
}
if (count1 == 10||count2 == 10)
System.out.println("Ordered");
else
System.out.println("Unordered");
}
}
/*
題目:Q11942 : Lumberjack Sequencing
作者:1010
時間:西元 2016 年 7 月 */
}
困難度 
判斷數列是否遞增或遞減(同個迴圈判斷分開檢測)必須通過下列這項測資
1

1 2 3 4 6 5 7 8 9 10

Q11984: A Change in Thermal Unit

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

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=1;i<=n;i++){
double c=scn.nextDouble(),d=scn.nextDouble();
d=d*(5/9.);
System.out.printf("Case %d: %.2f\n",i,c+d);
}
}
/*
題目:Q11984: A Change in Thermal Unit
作者:1010
時間:西元 2016 年 7 月 */
}
困難度 
單位轉換

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

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 str = scn.next();
int i = 0, j = 1, count = 1;
long tot = 0;
while (true) {
char s[] = str.toCharArray();//拆開成字元陣列做回文(palindrome)判斷
if (s.length == 1 && count == 1) { //假如只有長度為一相加然後跳出迴圈
tot = Long.parseLong(str) + Long.parseLong(str);
break;
}
if (count > 1) {//假如是第一筆資料不做回文判斷
for (i = 0, j = s.length - 1; i < j; i++, j--) {
if (s[i] != s[j])
break;
}
}
if (i >= j) {//判斷回文成立跳出迴圈
count--;
break;
}
StringBuilder st = new StringBuilder();//字串翻轉
st.append(str).reverse();
String str2 = String.valueOf(st);//StringBuilder轉回String型態
tot = Long.parseLong(str2) + Long.parseLong(str);//兩字串轉成Long型態相加
str = String.valueOf(tot);//Long轉字串
count++;
}
System.out.println(count + " " + tot);
}
}
/*
題目:Q10018: Reverse and Add
作者:1010
時間:西元 2016 年 7 月 */
}
這題數字翻轉相加會爆所以要用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

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], tot = 0;
for (int i = 0; i < num; i++)
arr[i] = scn.nextInt();
Arrays.sort(arr);
for (int i = arr.length - 3; i >= 0; i -= 3)
tot += arr[i];
System.out.println(tot);
}
}
/*
題目:Q11369: Shopaholic
作者:1010
時間:西元 2016 年 7 月 */
}
這題很簡單就使是把商品價格存入陣列然後作排序最後從尾開始遞減3相加

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr[] = new int[9], count = 0;
for (int i = 0; i < 3; i++) {
String s[] = scn.next().split(",");
for (int j = 0; j < 3; j++)
arr[count++] = Integer.parseInt(s[j]);
}
Arrays.sort(arr);
System.out.println((arr[8] + arr[7] + arr[6]));
}
/*
題目:[C_AR81-易] 求Array元素最大值的和
作者:1010
時間:西元 2016 年 7 月 */
}
這題然是3*3但輸入資料只有三行每行個別字串切割放入一個大小為九的陣列
最後最排序,取末三個相加

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
String arr[]={"monkey","rooster","dog","pig","rat","ox","tiger","rabbit","dragon","snake","horse","goat"};
int num=scn.nextInt()%12,t=num-6;//對沖-(12/2)
if(t<0)t+=12;//若為負數時加12
System.out.println(arr[num]+"\n"+arr[t]);
}
}
/*
題目:[C_AR70-易] 不要在太歲爺頭上動土
作者:1010
時間:西元 2016 年 7 月 */
}
正沖就是對沖-6,假如為負數加12對應到陣列中就是答案了

Q11541: Decoding

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine());
for (int c = 1; c <= n; c++) {
String str = scn.nextLine();
StringTokenizer st = new StringTokenizer(str, "0123456789");//使用StringTokenizer將數字拆開
StringTokenizer st2 = new StringTokenizer(str, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");//使用StringTokenizer將英文拆開
String arr[] = new String[st.countTokens()], ary[] = new String[st2.countTokens()];//arr儲存英文字串 ary儲存次數
int i = 0;
while (st.hasMoreTokens()) { //hasMoreTokens()讀出並存入陣列
arr[i] = String.valueOf(st.nextToken());
ary[i++] = String.valueOf(st2.nextToken());
}
System.out.printf("Case %d: ", c);
for (i = 0; i < arr.length; i++) {
for (int j = Integer.parseInt(ary[i]); j > 0; j--) {//印出該次數字母
System.out.print(arr[i]);
}
}
System.out.println();
}
}
/*
題目:Q11541: Decoding
作者:1010
時間:西元 2016 年 7 月 */
}
困難度 
本題較為麻煩要利用StringTokenizer將字串中的英文和數字拆開

2016年7月19日 星期二

[C_AR85-易] 反矩陣

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=3505
#include<stdio.h>
#include<math.h>
int main(){
float arr[4],a,b,c,d;
int i=0;
scanf("%f%f%f%f",&a,&b,&c,&d);
arr[0]=d/((a*d)-(c*b));
arr[1]=-b/((a*d)-(c*b));
arr[2]=-c/((a*d)-(c*b));
arr[3]=a/((a*d)-(c*b));
for(i=0;i<4;i++){
if(arr[i]==floor(arr[i]))
printf("%.0f",floor(arr[i]));
else
printf("%.1f",arr[i]);
if(i%2==1)
printf("\n");
else
printf(" ");
}
/*
題目:[C_AR85-易] 反矩陣
作者:1010
時間:西元 2016 年 7 月 */
}

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

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
String str[][] = { { "123", "Tom", "DTGD" }, { "456", "Cat", "CSIE" }, { "789", "Nana", "ASIE" },
{ "321", "Lim", "DBA" }, { "654", "Won", "FDD" } };
while (n-- != 0) {
int num = scn.nextInt();
String s = scn.next();
for (int i = 0; i < 5; i++) {
if (str[i][num - 1].equals(s)) {
System.out.println(str[i][0] + " " + str[i][1] + " " + str[i][2]);
break;
}
}
}
}
/*
題目:[C_AR74-中] 學生資料搜尋程式
作者:1010
時間:西元 2016 年 7 月 */
}
這題不需要什麼困難技巧只需要把內容放入二維陣列,依指定的行下去逐一搜尋是否有吻合的資料然後印出

[C_AR59-易] 好數問題

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

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) {
String str = scn.next();
char c[] = str.toCharArray();//存入字串並拆開成字元陣列
if (c.length != 4)//若長度不等於4就是Failure Input
System.out.println("Failure Input");
else {
int num[] = new int[10], max = 0, temp = 0; //判斷好數
for (int i = 0; i < 4; i++)//num[10]的陣列中分別為0~9以索引值當作數字內容存次數
num[c[i] - '0']++;
for (int i = 0; i < 10; i++)//num全部檢查次數
if (num[i] >= max) {
temp = max;//若num[i] == max temp儲存第二大的個數 max儲存num[i]
max = num[i];
}
if (max == 2 && (max != temp))//判斷是否第一和第二數量是否一樣 ex:1122,1221 1和2的個數都是2個
System.out.println("Yes");
else
System.out.println("No");
}
}
}
/*
題目:[C_AR59-易] 好數問題
作者:1010
時間:西元 2016 年 7 月 */
}
這題判斷好數,先存入字串中判斷長度若大於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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int num[] = new int[6];
for (int i = 0; i < 6; i++)
num[i] = scn.nextInt();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (j != 0)
System.out.print(" ");
System.out.printf("%03d", num[i] * num[j]);
}
System.out.println();
}
}
}
/*
題目:[C_AR67-易] 隨意九九乘法
作者:1010
時間:西元 2016 年 7 月 */
}
這題沒什麼困難,利用print印出3位數不足補零=>%03d

[C_AR62-中] 矩陣相乘

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int a = scn.nextInt(), b = scn.nextInt(), arr[][] = new int[a][b];//輸入到第一個陣列
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++)
arr[i][j] = scn.nextInt();
}
int x = scn.nextInt(), y = scn.nextInt(), ary[][] = new int[x][y];//輸入到第二個陣列
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++)
ary[i][j] = scn.nextInt();
}
int ans[][] = new int[a][y];
for (int i = 0; i < a; i++) {//控制列(第一個陣列)
for (int j = 0; j < y; j++) {//控制行(第二個陣列)
int num = 0;
for (int k = 0; k < b; k++) {//每個元素(第一個陣列的左到右,第二個陣列上到下)
num += arr[i][k] * ary[k][j];
}
ans[i][j] = num;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < y; j++) {
if (j != 0)
System.out.print(" ");
System.out.print(ans[i][j]);
}
System.out.println();
}
}
}
/*
題目:[C_AR62-中] 矩陣相乘
作者:1010
時間:西元 2016 年 7 月 */
}
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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
char c;
int tot = 0;
for (int i = 0; i < 5; i++) {
c = scn.next().charAt(0);
if (c == 'A')
tot += 14;
else if (c == 'J')
tot += 11;
else if (c == 'Q')
tot += 12;
else if (c == 'K')
tot += 13;
else if (c == '1')//1這個數字(被A取代)所以當讀到1時就代表是10
tot += 10;
else
tot += c - '0';
}
System.out.println(tot);
}
}
/*
題目:[C_AR66-易] 撲克牌13點
作者:1010
時間:西元 2016 年 7 月 */
}
這題讀取字元當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

#include<stdio.h>
int main(){
int n;
while(scanf("%d",&n)!=EOF){
while(n!=1){
printf("%d\n",n);
if(n%2==0)
n/=2;
else
n=n*3+1;
}
printf("%d\n",n);
}
/*
題目:[C_MM253-易] 3x+1數列產生
作者:1010
時間:西元 2016 年 7 月 */
}
這題就只是當偶數除二奇數乘以二加一直到一跳出迴圈

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
String s[] = scn.nextLine().split(" ");
for (int i = s.length - 1; i >= 0; i--) {
if (i != s.length - 1)
System.out.print(" ");
System.out.print(s[i]);
}
System.out.println();
}
}
/*
題目:[C_OT06-易] 英文句子字之倒轉
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是讀入字串陣列然後從尾巴讀回頭

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
double n = scn.nextDouble();
System.out.printf("%.5f\n", f(n));
}
}
public static double f(double n) {
if (n == 1)
return 1;
else
return (f(n - 1) + 2) / 2;
}
/*
題目:[C_RU23-易] 遞迴練習2f(n)=f(n-1)+2
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是考遞迴的概 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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
double ball = scn.nextDouble(), tot = ball;//第一次彈到地面
while ((ball /= 2) >= 1) {
tot += ball * 2; //彈下去+反彈一樣高所以*2
}
System.out.printf("%.2f\n", tot);
}
}
/*
題目:[C_RU08-易] 彈力球
作者:1010
時間:西元 2016 年 7 月 */
}
這題大一寫的時候也是一筆測資過不了最近用java來寫就AC了,這題解法式每次高度為上一次的一半每次彈跳下去和起來瞬間高度一樣(除了第一次球掉地面,先加入tot)

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

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

#include<stdio.h>
#include<math.h>
#include <string.h>
double call(double t1,double t0){
if(t1==0)return t0;
else return call(t1-1,t0)+t1*2.71828;
}
int main(){
int n,i;
double t0,t1;
scanf("%d",&n);
for(i=0;i<n;i++){
int j=0;
char str[1000];
scanf("%s",str);
char *test=strtok(str,",");
while (test != NULL) {
if(j==0)t0=atof(test);
else t1=atof(test);
test = strtok(NULL, ",");
j++;
}
printf("%.4lf\n",call(t1,t0));}
/*
題目:[C_RU07-易] 電路板溫度升高問題.c
作者:1010
時間:西元 2016 年 7 月 */
}
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) {
String s[] = scn.next().split(",");
double t = Double.parseDouble(s[0]), time = Integer.parseInt(s[1]);
System.out.printf("%.4f\n", call(time, t));
}
}
public static double call(double t1, double t0) {
if (t1 == 0)
return t0;
else
return call(t1 - 1, t0) + t1 * 2.71828;
}
/*
題目:[C_RU07-易] 電路板溫度升高問題.java
作者:1010
時間:西元 2016 年 7 月 */
}
這題是系上鍾主任所出的題目使用遞迴做運算,我當初用c顯示記憶體引用錯誤不知道是什麼原因後還用java就AC了

[C_RU01-易] 矩陣練習

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

import java.math.BigInteger;
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) {
BigInteger a = scn.nextBigInteger(), tot = new BigInteger("1");
while (!a.equals(new BigInteger("1"))) {
tot = tot.multiply(a);
a = a.subtract(new BigInteger("1"));
}
System.out.println(tot);
}
}
/*
題目:[C_RU01-易] 矩陣練習
作者:1010
時間:西元 2016 年 7 月 */
}
這題比long還要大所以要使用大數運算BigInteger才會過

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

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

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) {
String str = scn.next(), s[] = str.split(",");//字串切割
int arr[] = new int[s.length], ary[] = new int[s.length], a = 0, b = 0;//建立兩個等長陣列比對用
for (int i = 0; i < s.length; i++) {
arr[i] = Integer.parseInt(s[i]);//存入要排序的陣列
ary[i] = Integer.parseInt(s[i]);//存入做原始比對的陣列
}
Arrays.sort(arr);//排序
for (int i = 0, j = arr.length - 1; i < arr.length; i++, j--) {
if (arr[i] == ary[i])//a紀錄遞增相同數量
a++;
if (arr[i] == ary[j])//b紀錄遞減相同數量
b++;
}
if (a == arr.length)//a等於長度時代表是遞增數列
System.out.println("ASC");
else if (b == arr.length)//b等於長度時代表是遞減數列
System.out.println("DESC");
else
System.out.println("NOON");
}
}
}

[C_SO35-易] 排序數字

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int num = scn.nextInt(), arr[] = { 1, 6, 9, 23, 56, 95, num };//把輸入的新值放入最後
for (int i = 0; i < arr.length - 1; i++) {//插入排序法逐一比對
if (arr[i] > num) {
int temp = arr[i];
arr[i] = arr[6];
arr[6] = temp;
}
}
for (int i = 0; i < 7; i++) {
if (i != 0)
System.out.print(",");
System.out.print(arr[i]);
}
System.out.println();
}
/* 題目:[C_SO35-易] 排序數字
作者:1010
時間:西元 2016 年 7 月 */
}
把輸入的新值放入最後,並利用插入排序法逐一比對

[C_SO29-易] 藝人選秀

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
double arr[][] = new double[n][2], temp;//用int也可以[][0]存號碼 [][1]存分數
for (int i = 0; i < n; i++) {
arr[i][0] = scn.nextDouble(); //序號
double tot = 0;
for (int j = 0; j < 4; j++)//長相、歌聲、談吐、演戲4個加總tot
tot += scn.nextDouble();
arr[i][1] = Math.ceil(tot / 4);//tot無條件進位存入arr[i][1];
}
for (int i = 0; i < n - 1; i++) {//氣泡排序法分數序號連同排序
for (int j = i + 1; j < n; j++) {
if (arr[i][1] == arr[j][1]) {
if (j < i) {
temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
} else if (arr[i][1] < arr[j][1]) {
temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
}
}
if (n <= 3)//大於三個就印出前三,無則印出一個
System.out.printf("%.0f\n", arr[0][0]);
else
System.out.printf("%.0f %.0f %.0f\n", arr[0][0], arr[1][0], arr[2][0]);
}
/* 題目:[C_SO29-易] 藝人選秀
作者:1010
時間:西元 2016 年 7 月 */
}
題目第一個測資有錯誤排序後前三應該是 4 2 3

[C_SO22-易] 你們很強!

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

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];
String s[][] = new String[n][2];
for (int i = 0; i < n; i++) {
s[i][0] = scn.next();
int tal = scn.nextInt(), tot = 1000, num = 0;//讀入身高
if (tal > 170)//若大於170加成
tot += (tal - 170) * 100;
else
tot -= (170 - tal) * 100;
for (int j = 0; j < 5; j++) {
num += scn.nextInt(); //讀取智力、耐力、力量、敏捷與精神加入num
}
tot += num * 20;//(智力、耐力、力量、敏捷與精神)*20
arr[i] = tot;//總分數(放入int型態陣列排序用)
s[i][1] = String.valueOf(tot); //s[i][0]存名字 s[i][1]存分數
}
Arrays.sort(arr);
for (int i = n - 1; i >= 0; i--) {//s[i][1]跟排序好的arr[i]做比對依序輸出
for (int j = 0; j < s.length; j++) {
if (Integer.parseInt(s[j][1]) == arr[i]) {
System.out.println(s[j][0]);
}
}
}
}
/* 題目:[C_SO22-易] 你們很強!
作者:1010
時間:西元 2016 年 7 月 */
}

[C_SO14-中] 求中位數

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int arr[] = new int[5], count = 0;
for (int i = 0; i < 5; i++) {
arr[i] = scn.nextInt();
if (arr[i] == 0)
count++;
}
if (count == 5)
break;
Arrays.sort(arr);
System.out.println(arr[2]);
}
}
/* 題目:[C_SO14-中] 求中位數
作者:1010
時間:西元 2016 年 7 月 */
}
這題不難先把5個數字存入陣列裡然後作排序最後讀出arr[2]的就是中位數了

[C_ST47-易] 跳格字串解密

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str=scn.nextLine();
char c[]=str.toCharArray();//轉成字元陣列
for(int i=0;i<c.length;i+=3){//讀取陣列間格為3
System.out.print(c[i]);
}
System.out.println();
}
/* 題目:[C_ST47-易] 跳格字串解密
作者:1010
時間:西元 2016 年 7 月 */
}
這題就只是設定迴圈間格為三去讀取字元陣列的內容

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str = scn.nextLine();
StringTokenizer st = new StringTokenizer(str, ",! ");
while (st.hasMoreElements()) {
System.out.print(st.nextToken());
}
System.out.println();
}
/* 題目:[C_ST31-易] 移除字串中的空白鍵和定位鍵
作者:1010
時間:西元 2016 年 7 月 */
}
這題用字串切割空白有split(只能切一個東西)StringTokenizer(能切許多特定的東西)

2016年7月18日 星期一

[C_ST37-中] 字串比對

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String a = scn.nextLine(), b = scn.next();
System.out.println(a.indexOf(b));
}
/* 題目:[C_ST37-中] 字串比對
作者:1010
時間:西元 2016 年 7 月 */
}
這題速解法直接利用java的indexOf()函式~

[C_MM210-易] 道路綠美化

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

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(), a = scn.nextInt(), b = scn.nextInt(), arr[] = new int[num + 1], x = 0, y = 0;
for (int i = 0; i <= num; i += a) {//建立樹存入陣列並給1,x存要種幾棵
arr[i] = 1;
x++;
}
for (int i = num; i >= 0; i -= b) {//建立路燈假如遇到陣列裡有1代表有種樹所以x--,建立路燈y++
if (arr[i] == 1)
x--;
y++;
}
System.out.println(x + " " + y);
}
}
/* 題目:[C_MM210-易] 道路綠美化
作者:1010
時間:西元 2016 年 7 月 */
}
這題先算出要種幾棵樹並存入陣列(有種樹為1)並且變數x紀錄次數
然後再從尾開始建樹燈假如重疊就x--減去樹換成建立樹燈然後y++

[C_MM226-易] 薪水問題

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

#include<stdio.h>
int main(){
int tot=36000,a,b;
scanf("%d%d",&a,&b);
if(a>=180) //工時大於12*15 算加班費
tot+=400*(a-180);
else
tot-=200*(180-a);//少於規定工時扣錢
tot+=300*b; //開會錢
printf("%d\n",tot);
/* 題目:[C_MM226-易] 薪水問題
作者:1010
時間:西元 2016 年 7 月 */
}
規定工時等於15*12=180若小於180扣錢大於算加班費,最後另外算開會的工錢

[C_MM214-易] 賣麵包

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

#include<stdio.h>
int main(){
int t,num,a,b,i,n;
scanf("%d",&n);
while(n--){
scanf("%d%d%d%d",&t,&num,&a,&b);
for(i=num;i>=0;i--){
if((i*a+(num-i)*b)==t)
break;
}
printf("%d %d\n",i,num-i);
}
/*
題目:[C_MM214-易] 賣麵包
作者:1010
時間:西元 2016 年 7 月 */
}
這題你會發現用小盒子裝的數量會比較多所以迴圈從最大數量(盒子總數量)開始遞減族依判斷若大小盒子乘以各個可裝的數量等於總麵包數量就是我們要的答案了

[C_MM224-易] 我要九十九

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

#include<stdio.h>
#include<math.h>
int main(){
double n=0;
while(1){
scanf("%lf",&n);
if(n==-1)break;
int count=0;
while(floor(n)!=99){
n=sqrt(n)*10;
count++;
}
printf("%d\n",count);
}
/*
題目:[C_MM224-易] 我要九十九
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是進入迴圈每次開根號乘以10判斷是否為99每次的結果要用地板函數floor()它會自動省略小數成整數 
例如: 99.1234 => 99

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

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

#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);
while(num--){
int s = 0,n,m,i;
scanf("%d%d",&n,&m);
for (i = 2; i <= n; i++)
s = (s + m) % i;
printf("%d\n",s+1);
}
/*
題目:[C_MM212-易] 路上的東西不要亂撿 !
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是所謂的約瑟夫問題因為測資的資料量大勢必不能用模擬的方法,會超時
所以有時間複雜度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

#include<stdio.h>
int main(){
int fib[20],i,n;
fib[0]=1,fib[1]=2;
for(i=2;i<20;i++)
fib[i]=fib[i-1]+fib[i-2];
while(scanf("%d",&n)!=EOF)
printf("%d\n",fib[n-1]);
/*
題目:C_MM205-易 爬樓梯
作者:1010
時間:西元 2016 年 7 月 */
}
#include<stdio.h>
unsigned long long cul(unsigned long long n){
unsigned long long i=2,tot=1;
for(;i<=n;i++)tot*=i;
return tot;}
int main(){
unsigned long long i,j,n,tot=0,num;
scanf("%llu",&n);
if(n%2==0){
i=0;
j=n/2;}
else {
i=1;
j=(n-1)/2; }
for(;i<=n;i+=2,j--)
tot+=cul(i+j)/(cul(i)*cul(j));
printf("%llu\n",tot);
/*
題目:[C_RU10-中] 爬樓梯
作者:1010
時間:西元 2016 年 7 月 */
}
爬樓梯的問題就是求費是數列

[C_MM201-易] 立方和魔數

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

#include<stdio.h>
#include<string.h>
int main(){
char s[4];
scanf("%s",s);
int num=atoi(s),i=0,n,tot=0;
for(i=0;i<strlen(s);i++){
n=s[i]-'0';
tot+=n*n*n;
}
if(num==tot)
printf("1\n");
else
printf("0\n");
/*
題目:[C_MM201-易] 立方和魔數
作者:1010
時間:西元 2016 年 7 月 */
}
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(), s[] = str.split("");
int tot = 0, num = Integer.parseInt(str);
for (int i = 0; i < s.length; i++) {
tot += Integer.parseInt(s[i]) * Integer.parseInt(s[i]) * Integer.parseInt(s[i]);
}
if (tot == num)
System.out.println("1");
else
System.out.println("0");
}
}
/*
題目:[C_MM201-易] 立方和魔數
作者:1010
時間:西元 2016 年 7 月 */
}
這題我用java都返回值1不知道問題在哪用c就過了,不過字元陣列大小需設4不然會無效記憶體引用

Q10370: Above Average

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

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 n1 = scn.nextInt(), count = 0;
double tot = 0.0, arr[] = new double[n1];
for (int i = 0; i < n1; i++) {
arr[i] = scn.nextDouble();
tot += arr[i];
}
tot /= n1;
for (int i = 0; i < n1; i++) {
if (arr[i] > tot)
count++;
}
tot = 100 / ((double) n1 / count);
System.out.printf("%.3f%%\n", tot);
}
}
/*
題目:Q10370: Above Average
作者:1010
時間:西元 2016 年 7 月 */
}
困難度 
這題就是取平均再依一下去做計數去除以總個數再乘以0.01就是答案了

UTSA Basic Problem 2. 編碼器

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine()), code = Integer.parseInt(scn.nextLine()), tot;
System.out.printf("%d\n%d\n",n,code);
if(n>26)n%=26;
while (n < 0) {
n += 26;
}
while (scn.hasNext()) {
String str = scn.nextLine();
char c[] = str.toCharArray();
for (int i = 0; i < c.length; i++) {
tot = c[i];
if (c[i] >= 97 && c[i] <= 122) {
if (code == 0)
tot = c[i] + n;
else
tot = c[i] - n;
if (tot > 122)
tot -= 26;
if (tot < 97)
tot += 26;
} else if (c[i] >= 65 && c[i] <= 90) {
if (code == 0)
tot = c[i] + n;
else
tot = c[i] - n;
if (tot > 90)
tot -= 26;
if (tot < 65)
tot += 26;
}
System.out.printf("%c", tot);
}
System.out.printf("\n");
}
}
/*
題目:UTSA Basic Problem 2. 編碼器
作者:1010
時間:西元 2016 年 7 月 */
}
這題除了英文字母大小寫之外其餘照輸出要考慮到字母越界的問題所以當超出大小寫z時要回到a故-26反之

2016年7月17日 星期日

[C_ST58-易] 字串旋轉

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; //先存入字串,防止益位故2次a~z
char arr[] = str.toCharArray();//拆開成字元陣列
while (scn.hasNext()) {
int tot = 0, n;
for (int i = 0; i < 5; i++) {
n = scn.nextInt();
tot += n;
}
while (tot < 0) {
tot += 26;
}
if (tot > 26)
tot %= 26;
for (int i = tot; i < tot + 26; i++)
System.out.print(arr[i]);
System.out.println();
}
}
/*
題目:[C_ST58-易] 字串旋轉
作者:1010
時間:西元 2016 年 7 月 */
}
這題字串旋轉就是把輸入的五個數字加起來就是我們所想要的位移數,需要考慮兩個地方
1.假如位移數超過26就要取她的餘數
2.若小於0就要進入迴圈每次加26到大於0為止

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

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String num[][] = new String[10][5];
//0
num[0][0] = "*****";
num[0][1] = "* *";
num[0][2] = "* *";
num[0][3] = "* *";
num[0][4] = "*****";
//1
num[1][0] = " *";
num[1][1] = " *";
num[1][2] = " *";
num[1][3] = " *";
num[1][4] = " *";
//2
num[2][0] = "*****";
num[2][1] = " *";
num[2][2] = "*****";
num[2][3] = "* ";
num[2][4] = "*****";
//3
num[3][0] = "*****";
num[3][1] = " *";
num[3][2] = "*****";
num[3][3] = " *";
num[3][4] = "*****";
//4
num[4][0] = "* *";
num[4][1] = "* *";
num[4][2] = "*****";
num[4][3] = " *";
num[4][4] = " *";
//5
num[5][0] = "*****";
num[5][1] = "* ";
num[5][2] = "*****";
num[5][3] = " *";
num[5][4] = "*****";
//6
num[6][0] = "*****";
num[6][1] = "* ";
num[6][2] = "*****";
num[6][3] = "* *";
num[6][4] = "*****";
//7
num[7][0] = "*****";
num[7][1] = " *";
num[7][2] = " *";
num[7][3] = " *";
num[7][4] = " *";
//8
num[8][0] = "*****";
num[8][1] = "* *";
num[8][2] = "*****";
num[8][3] = "* *";
num[8][4] = "*****";
//9
num[9][0] = "*****";
num[9][1] = "* *";
num[9][2] = "*****";
num[9][3] = " *";
num[9][4] = " *";
while (scn.hasNext()) {
String str = scn.next();
char s[] = str.toCharArray();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
if (j != 0)
System.out.print(" ");
System.out.print(num[s[j] - '0'][i]);
}
System.out.println();
}
}
}
/*
題目:ITSA Basic 題目1. 矩陣數字顯示
作者:1010
時間:西元 2016 年 7 月 */
}
這題要注意每個數字都是有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

r= raw_input()
n= raw_input()
p= raw_input()
n1=int(n)
r1=float(r)
p1=int(p)
tot=float(0.0)
for x in range(n1):
tot+=p1
tot*=(1.0+r1)
print(int(tot))
"""題目:ITSA Basic 題目24. 計算複利
作者:1010
時間:西元 2016 年 7 月 """
這題必須要用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

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 str = scn.nextLine(), s[] = str.split(" "), arr[][] = new String[s.length][2];
for (int i = 0; i < s.length; i++) {
arr[i][0] = s[i].substring(0, 1);
arr[i][1] = s[i].substring(1);
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
char c1[] = arr[i][0].toCharArray();
char c2[] = arr[j][0].toCharArray();
if (c1[0] == c2[0]) {
int n1 = Integer.parseInt(arr[i][1]), n2 = Integer.parseInt(arr[j][1]);
if (n1 < n2) {
String temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
} else if (c1[0] < c2[0]) {
String temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
if (i != 0)
System.out.print(" ");
System.out.print(arr[i][0] + arr[i][1]);
}
System.out.println();
}
}
/*
題目:ITSA Basic 題目13. 撲克牌大小
作者:1010
時間:西元 2016 年 7 月 */
}
這題因為不知每筆的長度為何故先存入字串再做切割,然後再將切割後的字串以子字串subsring拆開字母和數字存入二維陣列
本題是用氣泡排序法,先判斷字母是否一樣若一樣則再比對數字大者在前,因為S>H>D>C剛好符合ASCii順序就直接toCharArray()轉成字元陣列去做比對

ITSA Basic 題目35. 買獎品

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

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 b = scn.nextInt(), t = scn.nextInt(), s = scn.nextInt(), arr[] = new int[s], tot = 0;
for (int i = 0; i < s; i++)
arr[i] = scn.nextInt();
Arrays.sort(arr);
for (int i = 0; i < t; i++)
tot += arr[i];
if (b - tot >= 0)
System.out.println(tot);
else
System.out.println("Impossible");
}
}
/*
題目:ITSA Basic 題目35. 買獎品
作者:1010
時間:西元 2016 年 7 月 */
}
這題直接利用Arrays.sort()排序輸入的陣列然後再依序從小開始選購禮物最後在驗證是否超出預算

Q12289 : One-Two-Three

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

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) {
String str = scn.next();
char arr[] = str.toCharArray();
if (arr.length == 5)
System.out.println("3");
else if (str.indexOf('n') >= 0
|| (str.indexOf('e') >= 0 && (str.indexOf('o') >= 0 || str.indexOf('n') >= 0))
|| (str.indexOf('o') >= 0 && (str.indexOf('n') >= 0 || str.indexOf('e') >= 0)))
System.out.println("1");
else
System.out.println("2");
}
}
/*
題目:Q12289 : One-Two-Three
作者:1010
時間:西元 2016 年 7 月 */
}
困難度 
這題輸入只有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

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++) {
long x = scn.nextLong(), y = scn.nextLong(), z = scn.nextLong();
System.out.printf("Case %d: ", i + 1);
if ((x + y) <= z || (x + z) <= y || (y + z) <= x||x==0||y==0||z==0) {
System.out.println("Invalid");
continue;
}
if (x == y && y == z)
System.out.println("Equilateral");
else if (x == y || y == z || x == z)
System.out.println("Isosceles");
else
System.out.println("Scalene");
}
}
/*
題目:Q11479: Is this the easiest problem?
作者:1010
時間:西元 2016 年 7 月 */
}
困難度 
三角形的構成條件:
1.任兩邊相加大於第三邊
2.沒有零邊
另外這題大於2的32次方所以形態要使用long

[C_AR188-易] 陣列元素

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str = scn.nextLine(), s[] = str.split(" ");
int count = 1;
for (int i = 0; i < s.length; i++) {
count = 1;
for (int j = i + 1; j < s.length; j++) {
if (s[i].equals(s[j])) {
count++;
}
}
if (count > s.length / 2) {
System.out.println(s[i]);
break;
}
}
if (count == 1)
System.out.println("n/a");
}
/*
題目:F91
作者:1010
時間:西元 2016 年 7 月 */
}
依題意找出重複字元並且大於n/2的元素,因為不知有多少個字元所以存入字串再切割做比對

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

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

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) {
int arr[] = new int[11];
String str = scn.nextLine(), s[] = str.split(" ");
for (int j = 0; j < s.length; j++) {
int num = Integer.parseInt(s[j]);
if (num >= 0 && num <= 9)
arr[0]++;
else if (num >= 10 && num <= 19)
arr[1]++;
else if (num >= 20 && num <= 29)
arr[2]++;
else if (num >= 30 && num <= 39)
arr[3]++;
else if (num >= 40 && num <= 49)
arr[4]++;
else if (num >= 50 && num <= 59)
arr[5]++;
else if (num >= 60 && num <= 69)
arr[6]++;
else if (num >= 70 && num <= 79)
arr[7]++;
else if (num >= 80 && num <= 89)
arr[8]++;
else if (num >= 90 && num <= 99)
arr[9]++;
else if (num == 100)
arr[10]++;
}
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + ",");
System.out.println();
}
}
/*
題目:[C_AR176-易] 任意個分數分佈統計
作者:1010
時間:西元 2016 年 7 月 */
}
這題因為不知每一筆有幾個成績所以要放入字串中然後做切割

[C_AR156-易] 迴文

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str=scn.next();
char s[]=str.toCharArray();
int i=0,j=s.length-1;
boolean b=true;
while(i<j){
if((s[i]!=s[j])||(s[i]<='0'||s[i]>='9')){
b=true;
break;
}
else
b=false;
i++;j--;
}
if(b||s.length%2==0)
System.out.println("FALSE");
else
System.out.println("TRUE");
}
/*
題目:[C_AR156-易] 迴文
作者:1010
時間:西元 2016 年 7 月 */
}
這題非常險惡測了第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

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(), arr[] = new int[n];
for (int i = 0; i < n - 1; i++) {
arr[scn.nextInt() - 1] = 1;
}
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
System.out.println(i + 1);
break;
}
}
}
}
/*
題目:[C_MM117-易] 少了一個數
作者:1010
時間:西元 2016 年 7 月 */
}
這題就宣告n個陣列,輸入完n-1個之後下去搜尋 ps.以輸入的值-1當作索引值

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

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

#include<stdio.h>
int main(){
long long n,a=1,b=1,temp,i;
scanf("%lli",&n);
for(i=2;i<=n;i++){
temp=a;
a=b;
b=a+temp;
}
printf("%lli\n",a);
/*
題目:[C_AR116-易] 到底有幾隻兔子?
作者:1010
時間:西元 2016 年 7 月 */
}
這題就是算費式數列要考慮到益位的問題所以要使用long long

[C_AR115-易] 梅花座

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int i, j;
char arr[][] = new char[10][10];
for (i = 1; i < 9; i++)
for (j = 1; j < 9; j++)
arr[i][j] = scn.next().charAt(0);
for (i = 1; i < 9; i++) {
for (j = 1; j < 9; j++) {
if (arr[i][j] == arr[i - 1][j - 1]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i][j - 1]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i + 1][j - 1]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i - 1][j]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i + 1][j]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i - 1][j + 1]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i][j + 1]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
} else if (arr[i][j] == arr[i + 1][j + 1]) {
System.out.printf("(%d,%d)\n", i - 1, j - 1);
continue;
}
}
}
}
/*
題目:[C_AR115-易] 梅花座
作者:1010
時間:西元 2016 年 7 月 */
}
這題算用途法煉鋼法逐一檢視八方是否有重複的,java8*8要建立10*10的陣列(考慮到牆)否則會跳出超出陣列大小的例外

[C_MM116-易] 買東西

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

#include<stdio.h>
int main(){
int x,y,z,tot=0,count=0;
scanf("%d%d%d0",&x,&y,&z);
tot=x*20+y*25;
while(1){
if(x-3>=0&&y-2>=0){
x-=3;
y-=2;
count++;
}
else
break;
}
tot+=(z-count)*30;
printf("%d\n",tot);
/*
題目:[C_MM116-易] 買東西
作者:1010
時間:西元 2016 年 7 月 */
}
這題先把綠茶跟麵包金額先算出來,然後再判斷有送幾個餅乾最後相減加上餅乾錢就是答案了!

[C_MM110-中] Ugly number

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

#include<stdio.h>
int main(){
int n,i=1,count=0,x=1;
scanf("%d",&n);
while(1){
x=i;
while(x!=1){
if(x%2==0)
x/=2;
else if(x%3==0)
x/=3;
else if(x%5==0)
x/=5;
else
break;
}
if(x==1)
count++;
if(count==n)
break;
i++;
}
printf("%d\n",i);
/*
題目:[C_MM110-中] Ugly number
作者:1010
時間:西元 2016 年 7 月 */
}
這題用迴圈依序除以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

#include<stdio.h>
int main(){
int i,n,bx=0,by=0;
double num,max=-1,x,y;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%lf%lf",&x,&y);
num=(double)x/y;
if(num>=max){max=num,bx=x,by=y;}
}
x=bx;y=by;
while((bx%=by)&&(by%=bx));
printf("%.0f %.0f\n",x/(bx+by),y/(bx+by));
/*
題目:[C_MM142-易] 分數最大值
作者:1010
時間:西元 2016 年 7 月 */
}
這題輸出結果還要必須考慮到是否最簡分數,求兩者最大公因數再相除

[C_MM108-易] 位元計數器

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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
System.out.printf("The number of bits is %d.\n",Integer.bitCount(n));
}
/*
題目:[C_MM108-易] 位元計數器
作者:1010
時間:西元 2016 年 7 月 */
}
這題是輸入時進位整數顯示二進位中有幾個一,Integer.bitCount()剛好符合要求

[C_MM104-易] 約瑟夫問題

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

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 [100],i=0,count=0,c=0;
while(true){
if(i==100)i=0;
if(arr[i]==0){
count++;
}
if(count==n){
count=0;
arr[i]=1;
c++;
}
if(c==99)break;
i++;
}
for(int j=0;j<=99;j++){
if(arr[j]==0){
System.out.println(j);
break;
}
}
}
/*
題目:[C_MM104-易] 約瑟夫問題
作者:1010
時間:西元 2016 年 7 月 */
}
約瑟夫的問題是很經典的題目,跟另一題心得報數很類似

[C_MM103-易] 費式數列

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

#include<stdio.h>
int main(){
int n,a=1,b=1,temp,i,num;
scanf("%d",&n);
for(i=2;i<=n;i++){
temp=a;
a=b;
b=a+temp;
}
printf("%d\n",a);
/*
題目:[C_MM103-易] 費式數列
作者:1010
時間:西元 2016 年 7 月 */
}
費式數列是很經典的題目每一個數目為前面兩個加總

[C_MM101-易] 糖果紙兌換

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

#include<stdio.h>
int main()
{
int n,tot=0,temp;;
scanf("%d",&n);
tot=n;
while(n/3){
temp=n%3;
n/=3;
tot+=n;
n+=temp;
}
printf("%d\n",tot);
/*
題目:[C_MM101-易] 糖果紙兌換
作者:1010
時間:西元 2016 年 7 月 */
}
這題就只是每3張糖果紙換一顆糖果每次除以三然後再加除以三的餘數直到除以三為零

[C_MM118-易] 數字反向排列

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

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();
StringBuffer sb=new StringBuffer();
sb.append(str);
System.out.println(sb.reverse());
}
}
/*
題目:[C_MM118-易] 數字反向排列
作者:1010
時間:西元 2016 年 7 月 */
}
這題利用StringBuffer()呈現反轉

[C_MM109-易] 計算密碼


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

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int x = scn.nextInt(), n = scn.nextInt();
double tot = 0.0;
for (int i = 0; i < x; i++) {
double t = 1;
for (int j = i + 1; j >= 1; j--)
t *= j;
tot += 1.0 / t;
}
System.out.println((Double.toString(tot)).substring((n + 1), n + 5));
}
/*
題目:[C_MM109-易] 計算密碼
作者:1010
時間:西元 2016 年 7 月 */
}
這題利用一個技巧就是先把浮點數轉成字串然後利用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

#include<stdio.h>
int main()
{
int arr[]={6,28,496,8128},i,a,b,count=0;
scanf("%d%d",&a,&b);
for(i=0;i<4;i++){
if(arr[i]>=a&&arr[i]<=b){
if(count>0)printf(" ");
printf("%d",arr[i]);
count++;
}
}
if(count==0)printf("null\n");
else printf("\n");
return 0;
}

古希臘數學家歐幾里得是通過 的表達式發現前四個完全數的。
  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

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(), arr[] = str.split(":");
if (str.equals("0:00"))
break;
double h = Double.parseDouble(arr[0]), m = Double.parseDouble(arr[1]), h_hand, m_hand, tot = 0.0;
h_hand = h * (360 / 12) + m / 60 * (360 / 12);
m_hand = m * (360 / 60);
tot = Math.abs(h_hand - m_hand);
System.out.printf("%.3f\n", tot < 180 ? 360 - tot : tot);
}
}
/*
題目:Q579 : Clock Hands
作者:1010
時間:西元 2016 年 7 月 */
}

困難度 
這提示輸入時針和分針顯示出在時鐘上最小的角度
首先先計算時針角度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()