ITSA E-tutor Code Exchange

READ ME!

貼 CODE 請按照下方提供的固定格式(按左上角「筆」或「視窗分割」按鈕就可以看到)

  1. 首先找到程式練習的題庫,沒有的話用 h2 (兩個#)開新的區塊。
  2. 使用 h3 (三個#) 貼上題名(再放置連結),E-tutor格式如下。
  3. 使用下面這個範本來貼程式碼(Java可以替換成其他 HackMD 支援的語言,如 C++ 為 CPP ,"="是提供行數標示,如果同一題有不同語言的解法,應放在同一題的區塊下,用 h4 (四個#)標示使用何種語言,同語言不同解法可用 h5 (五個#) 標示解法一、解法二⋯⋯等等。
  4. 可標示程式碼提供者。
  5. 本頁面使用 C 寫的 Code 在格式部分請選擇「cpp=」。
  6. 右側有選單 (雙欄編輯模式時會收至右下角) 可以快速跳至欲查看的區塊,不用慢慢滑。
  7. 程式碼力求可讀性高(因為看到自己的黑歷史)。
  8. 善用註解,另可額外補充解決此題應知資訊。
  9. 看看別人、看看自己的程式碼,不妨想想 TQM (Total Quality Control) 「完全品質控制」的概念,即「重來一次,怎麼做會更好?」 一次次的思考,伴隨著一次次的提升。
  10. 如果有能力的話,請嘗試不同的解法;也別被別人的想法拘束住。
PASTE YOUR CODE HERE

我的編譯環境說明:
C++ 版本使用 C++ 14,編譯器為 clang/llvm
OS: macOS Mojave 10.14.2
Howard Guo


每天都要記得 Coding,C、C++、Java至少要碰其中一個!
Sat, Jul 20, 2019 1:16 AM
風思


Mathematics I

[C_MM01-易] 計算梯型面積

C Version

Howard Guo

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int a; int b; int c; scanf("%d %d %d", &a, &b, &c ); double result = (a+b)*c/2.0; printf("Trapezoid area:%.1f\n", + result ); return 0; }

風思

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int a , b , c; scanf("%d %d %d",&a,&b,&c); printf("Trapezoid area:%.1f\n",(a+b)*c/2.0); return 0; }

Java Version

AfreecaKing

import java.util.*; class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int up=sc.nextInt(); int down=sc.nextInt(); int high=sc.nextInt(); System.out.println("Trapezoid area:"+((up+down)*high/2.0)); } }

[C_MM02-易] 計算三角形面積

Java Version

AfreecaKing

import java.util.*; class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int under=sc.nextInt(); int high=sc.nextInt(); System.out.println(under*high/2.0); } }

[C_MM03-易] 兩數總和

Java Version

AfreecaKing

import java.util.*; class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int m=sc.nextInt(); System.out.println(n+m); } } }

[C_MM04-易] 計算總和、乘積、差、商和餘數

Java Version

AfreecaKing
注意餘數不能是負的

import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc =new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); System.out.printf("%d+%d=%d\n",a,b,a+b); System.out.printf("%d*%d=%d\n",a,b,a*b); System.out.printf("%d-%d=%d\n",a,b,a-b); if(a%b>=0) System.out.printf("%d/%d=%d...%d\n",a,b,a/b,a%b); else System.out.printf("%d/%d=%d...%d\n",a,b,a/b-1,a%b+b); } }

[C_MM05-易] 計算正方形面積

C Version

風思
當初卡很久在四捨五入的問題

#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { double a,b,c; scanf("%lf",&a); b=pow(a,2); //平方 //printf("%.1f\n",b); c= (int)(b); //取整數,floor(b,2); //printf("%.0f\n",c); if(b-c>=0.05) printf("%.1f",b+0.01); /*取小數第二位,若大於0.05,則加上0.01, 又因為c語(可)言(誤)本身會五捨六入,這樣恰好可以四捨五入,這花了我好多時間...*/ else printf("%.1f",b); printf("\n"); return 0; }

Java Version

解法一

風思
相同的格式化輸出,java四捨五入;C五捨六入

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { double n = sc.nextDouble(); System.out.printf("%.1f\n", Math.pow(n, 2)); } sc.close(); } }
解法二

風思
當初寫C要是知道這招,就好啦!

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { double n=sc.nextDouble(); System.out.println(Math.round(n*n*10)/10.0); } sc.close(); } }

[C_MM06-易] 英哩轉公里

Java Version

AfreecaKing

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); double n=sc.nextInt(); System.out.println(Math.round(1.6*n*10.0)/10.0); } }

[C_MM07-易] 計算平方值與立方值

Java Version

AfreecaKing

import java.util.*; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); System.out.printf("%d %d %d\n",n,n*n,n*n*n); } }

[C_MM08-易] 計算兩數和的平方值

Java Version

Afreecaking

import java.util.*; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); System.out.println((n+m)*(n+m)); } }

[C_MM09-易] 計算 i 次方的值

Java Version

AfreecaKing

import java.util.*; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int i=sc.nextInt(); int sum=1; if(i>31) System.out.println("Value of more than 31"); else { for(int j=1;j<=i;j++) sum=sum*2; System.out.println(sum); } } }

[C_MM10-易] 攝氏溫度轉華式溫度

Java Version

AfreecaKing
強烈建議不要用C寫,浮點數會有誤差

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); double n=sc.nextDouble(); System.out.println(Math.round((n*9/5+32)*10.0)/10.0); sc.close(); } }

C Version

AfreecaKing
用round來處理浮點數誤差,很久以前寫的可讀性不高

#include <stdio.h> #include <stdlib.h> #include <cmath> int main() { double n; scanf("%lf",&n); double X,f; f=n*(9/5.0)+32; X=round(10*f)/10.0; printf("%.1lf\n",X); return 0; }

[C_MM11-易] 購票計算

C Version

風思
天罰版,可讀性幾乎為0

#include <stdio.h> int main () { int x, t = 0, m = 0, f = 0, o = 0; //x=初始值,t=NT10數量,m=過渡運算值,f=NT5數量,o=NT1數量; scanf ("%d", &x); if (x >= 10) { t = x / 10; m = x - 10 * t; if (m >= 5) { f = m / 5; o = m - 5 * f; } else { o = m; } } if (x >= 5 && x < 10) { f = x / 5; o = x - 5; } if (x < 5) { o = x; } printf ("NT10=%d\n", t); printf ("NT5=%d\n", f); printf ("NT1=%d\n", o); return 0; }

Java Version

風思
好多了

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n = sc.nextInt(); int n10= n/10; int n5= (n%10)/5; int n1= n%5; System.out.println("NT10="+n10); System.out.println("NT5="+n5); System.out.println("NT1="+n1); } sc.close(); } }

Java Version

AfreecaKing

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); int a,b,c; //寫C的習慣了 a=n/10; b=n%10/5; c=n%5; System.out.println("NT10="+a); System.out.println("NT5="+b); System.out.println("NT1="+c); sc.close(); } }

[C_MM12-易] 相遇時間計算

Java Version

AfreecaKing

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); System.out.println((int)Math.ceil(n/0.238)); sc.close(); } }

[C_MM13-易] 停車費計算

Java Version

AfreecaKing
將分鐘換成秒來計算

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n1=sc.nextInt(); int m1=sc.nextInt(); int n2=sc.nextInt(); int m2=sc.nextInt(); sc.close(); int sum=(n2*60+m2)-(n1*60+m1); if(sum<=120) System.out.println(sum/30*30); else if(sum>120 && sum<240) System.out.println(120+((sum-120)/30*40)); else System.out.println(280+((sum-240)/30*60)); } }

[C_MM14-易] 計算時間的組合

Java Version

AfreecaKing

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int second=sc.nextInt(); sc.close(); System.out.println(+second/86400+" days"); System.out.println(+second%86400/3600+" hours"); System.out.println(+second%3600/60+" minutes"); System.out.println(+second%60+" seconds"); } } //一天86400秒 //一小時3600秒 //一分鐘60秒

[C_MM15-易] 判斷座標是否在正方形的範圍內

Java Version

AfreecaKing

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int x=sc.nextInt(); int y=sc.nextInt(); sc.close(); if(x<=100 && y<=100) System.out.println("inside"); else System.out.println("outside"); } }

[C_MM16-易] 判斷座標是否在圓形的範圍內

Java Version

AfreecaKing

import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc =new Scanner(System.in); int x=sc.nextInt(); int y=sc.nextInt(); if(x<=100 && y<=100 && x*x+y*y<=10000) System.out.println("inside"); else System.out.println("outside"); } }

[C_MM17-易] 求最大公因數

Java Version

AfreecaKing
也可以用輾轉相除法,請上網路自行搜尋
因為不好解釋,要用到遞迴的概念

import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); if(n>m) for(int i=m;i>0;i--) if(n%i==0 && m%i==0) { System.out.println(i); break; } if(m>n) for(int i=n;i>0;i--) if(n%i==0 && m%i==0) { System.out.println(i); break; } } }

[C_MM18-易] 十進制轉二進制

Java Version

風思
~n為n的ones' complement(一的補數)

import java.util.*; //[C_MM18-易] 十進制轉二進制 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] result = new int[8]; if (n < 0) { int negn = ~n; //負數變成其一補數,方便處理 for (int i = 0; i < 8; i++) { result[7 - i] = negn % 2; negn /= 2; } for (int i = 0; i < 8; i++) { if (result[i] == 0) result[i] = 1; else result[i] = 0; } } else { for (int i = 0; i < 8; i++) { result[7 - i] = n % 2; n /= 2; } } for (int i = 0; i < 8; i++) System.out.print(result[i]); System.out.println(); sc.close(); } }

[C_MM19-易] 電話費計算

Java Version

AfreecaKing

import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int time=sc.nextInt(); sc.close(); if(time<=800) System.out.println(time*0.9); else if(time>800 && time<1500) System.out.println(Math.round(time*0.9*0.9*10.0)/10.0); else System.out.println(Math.round(time*0.9*0.79*10.0)/10.0); } }

[C_MM36-易] 季節判定

C++ Version

這題也是用笨笨 switch 硬幹, There should be a better solution even it's a simple problem.
Howard Guo

#include<iostream> using namespace std; int main(){ int month; cin >> month; switch (month) { case 3: cout << "Spring" << endl; break; case 4: cout << "Spring" << endl; break; case 5: cout << "Spring" << endl; break; case 6: cout << "Summer" << endl; break; case 7: cout << "Summer" << endl; break; case 8: cout << "Summer" << endl; break; case 9: cout << "Autumn" << endl; break; case 10: cout << "Autumn" << endl; break; case 11: cout << "Autumn" << endl; break; case 12: cout << "Winter" << endl; break; case 1: cout << "Winter" << endl; break; case 2: cout << "Winter" << endl; break; } return 0; }

C Version

風思
嘿嘿嘿,比↑少了粉多break跟輸出~

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int n; scanf("%d",&n); switch (n){ case 3: case 4: case 5: printf("Spring\n"); break; case 6: case 7: case 8: printf("Summer\n"); break; case 9: case 10: case 11: printf("Autumn\n"); break; case 12: case 1: case 2: printf("Winter\n"); break; } return 0; }

Java Version

風思

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n = sc.nextInt(); switch (n){ case 3: case 4: case 5: System.out.println("Spring"); break; case 6: case 7: case 8: System.out.println("Summer"); break; case 9: case 10: case 11: System.out.println("Autumn"); break; case 12: case 1: case 2: System.out.println("Winter"); break; } } sc.close(); } }

[C_MM37-易] 判斷座標位於何處

C++ Version

Howard Guo

#include <iostream> using namespace std; int main(void){ int x, y; cin >>x >> y; if(x==0 || y==0){ if(x==0 && y==0) cout << "Original" << endl; else if(x==0 && y!=0) cout << "y-axis" << endl; else if(y==0 && x!=0) cout << "x-axis" << endl; }else if(x>0 && y >0) cout << "1st Quadrant" << endl; else if(x<0 && y>0) cout << "2nd Quadrant" << endl; else if(x<0 && y<0) cout << "3rd Quadrant" << endl; else if(x>0 && y<0) cout << "4th Quadrant" << endl; return 0; }

C Version

風思

#include <stdio.h> int main() { int x,y; scanf("%d %d",&x,&y); if(x==0&&y==0) printf("Origin\n"); if(x==0&&y!=0) printf("y-axis\n"); if(x!=0&&y==0) printf("x-axis\n"); if(x>0&&y>0) printf("1st Quadrant\n"); if(x<0&&y>0) printf("2nd Quadrant\n"); if(x<0&&y<0) printf("3rd Quadrant\n"); if(x>0&&y<0) printf("4th Quadrant\n"); return 0; }

[C_MM38-易] 判斷3整數是否能構成三角形之三邊長

C++ Version

Howard Guo

#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int a, b, c; while (cin >> a >> b >> c) { if ((a + b) > c) { if ((b + c) > a) { if ((a + c) > b) { cout << "fit" << endl; } else cout << "unfit" << endl; } else cout << "unfit" << endl; } else cout << "unfit" << endl; } return 0; }

C Version

風思

#include <stdio.h> int main() { int x,y,z; scanf("%d %d %d",&x,&y,&z); if(x+y>z&&x+z>y&&y+z>x){ printf("fit\n"); }else{ printf("unfit\n"); } return 0; }

Java Version

風思

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if(x+y>z&&x+z>y&&y+z>x){ System.out.println("fit"); }else{ System.out.println("unfit"); } } sc.close(); } }

[C_MM39-易] 判斷是何種三角形

C++ Version

Howard Guo

#include <iostream> using namespace std; void tri(int, int, int); int main(int argc, char const *argv[]) { int a, b, c; while (cin >> a >> b >> c) { tri(a, b, c); } return 0; } void tri(int a, int b, int c) { int temp; for (int i = 1; i <= 3; i++) { if (a > b) { temp = a; a = b; b = temp; } else if (b > c) { temp = b; b = c; c = temp; } else if (a > c) { temp = a; a = c; c = temp; } } if(a+b<=c){ cout << "Not Triangle" << endl; } else if ((a * a + b * b) == c * c) { //直角三角形 cout << "Right Triangle" << endl; } else if ((a * a + b * b) < c * c) { //鈍角三角形 cout << "Obtuse Triangle" << endl; } else if ((a * a + b * b) > c * c) { //銳角三角形 cout << "Acute Triangle" << endl; } }

C Version

風思

#include <stdio.h> int main() { int a,b,c,a2,b2,c2; scanf("%d %d %d",&a,&b,&c); a2=a*a; b2=b*b; c2=c*c; if(a+b>c && a+c>b && b+c>b) { if(a2+b2==c2 || a2+c2==b2 || b2+c2==b2) printf("Right Triangle\n"); if(a2+b2<c2 || a2+c2<b2 || b2+c2<b2) printf("Obtuse Triangle\n"); if(a2+b2>c2 && a2+c2>b2 && b2+c2>b2) printf("Acute Triangle\n"); }else printf("Not Triangle\n"); return 0; }

[C_MM40-易] 1~N之間的總和

C++ Version

Howard Guo

#include<iostream> using namespace std; int main(void){ int n, sum=0; cin >> n; for(int i=1;i<=n;i++){ sum += i; } for(int i=1;i<=n;i++){ cout << i; if(i != n) cout << " + "; } cout << " = " << sum << endl; return 0; }

C version

風思

#include <stdio.h> int main() { int a,i,sum; scanf("%d",&a); for(i=1;i<=a;i++){ sum+=i; if(i==1) printf("%d ",i); else printf("+ %d ",i); } printf("= %d",sum); printf("\n"); return 0; }

[C_MM44-易] The Numbers

C Version

風思
scanf("%[^\n]",&N)為可輸入空白之字串表示方法

include <stdio.h> int main() { char N[11]; //欲將兩數合併為一字串,中有空格後有'\0', //所以=>N[2+1+7+1]; char M[8]; //預備給第二數儲存=>用來與第一數比對; short i,times; //宣告times紀錄次數; scanf("%[^\n]",&N); //輸入兩數; //printf("%s",N); for(i=3;i<=9;i++){ //由於第二數由N[3]開始; M[i-3]=N[i]; //複製; } //printf("%s",M); for(i=0;i<=6;i++){ //兩兩比對,至多比對7-1=6次; if(M[i]==N[0] && M[i+1]==N[1]) times++; } printf("%d",times); printf("\n"); //想好久...2019/1/30 10:10; return 0; }

[C_MM48-易] F91

C++ Version

Howard Guo

#include <iostream> #include<cstring> //To use the fuction "memset". using namespace std; int f91(int); int main(int argc, char const *argv[]) { int k; //根據題意:有k個測試資料 cin >> k; int arr[k]; memset(arr, 0, k); //把陣列arr清0 for (int i = 0; i < k; i++) { cin >> arr[i]; } for (int i = 0; i < k; i++) { cout << f91(arr[i]) << endl; } return 0; } int f91(int n) { if (n >= 101) { return n - 10; } else if (n <= 100) { return f91(f91(n + 11)); } else { return 0; } }

C Version

風思

#include <stdio.h> int f91(int n){ if(n>=1 && n<=100000){ if(n<=100) return f91(f91(n+11)); if(n>=101) return n-10; } } int main() { int k,i; scanf("%d",&k); int a[k]; if(k>=1 && k<=10){ for(i=0;i<=k-1;i++){ if(i!=k-1) scanf("%d ",&a[i]); else scanf("%d",&a[i]); } for(i=0;i<=k-1;i++){ if(i!=k-1) printf("%d\n",f91(a[i])); else printf("%d",f91(a[i])); } } //可惡的格式; printf("\n"); return 0; }

Arrayc I

[C_AR01-易] 一維陣列反轉 I

C++ Version

Howard Guo

#include <iostream> #include <cstring> using namespace std; int main(int argc, char const *argv[]) { int arr[100]; int arr_count = 0; string input; while (getline(cin, input)) { memset(arr, 0, 100); //陣列清0 arr_count = 0; //計數器歸零,不然第二次迴圈開始就會亂吐東西 for (int i = 0; i < input.length(); i++) { if (input[i + 1] != ' ' && input[i] != ' ' && i != input.length() - 1) { //cout << input[i] << "a" << endl; For purpose of debugging arr[arr_count] = arr[arr_count] * 10 + (int(input[i]) - '0') * 10; } else if (input[i + 1] == ' ' || i == input.length() - 1) { //cout << input[i] << "q" << endl; arr[arr_count] += int(input[i]) - '0'; //cout << "value= " << arr[arr_count] << " @ index= " << arr_count << endl; For purpose of debugging arr_count++; //cout << "new_count" << arr_count << endl; For purpose of debugging } } //印 for (int i = arr_count - 1; i >= 0; i--) { if (i == arr_count - 1) cout << arr[i]; else cout << " " << arr[i]; } cout << endl; } return 0; }

Java Version

風思
未來你會看到很多spilt();

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String st = sc.nextLine(); String[] tokens = st.split(" "); for (int i = tokens.length - 1; i >= 0; i--) { if (i == tokens.length - 1) System.out.print(tokens[i]); else System.out.print(" "+tokens[i]); } System.out.println(); } sc.close(); } }

[C_AR02-易] 一維陣列反轉 II

C Version

風思

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int a[6]; int i; for(i=0;i<6;i++){ scanf("%d",&a[i]); } for(i=5;i>=0;i--){ if(i==5){ printf("%d",a[i]); }else{ printf(" %d",a[i]); } } printf("\n"); return 0; }

Howard Guo

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int a[6]; int i=0; for(i=0;i<6;i++){ scanf("%d", &a[i]); } for(i=5;i>=0;i--){ if(i==5){ //要注意不能用= printf("%d", a[i]); }else{ printf(" %d", a[i]); } } printf("\n"); return 0; }

[C_AR03-易] 計算陣列中所有元素的立方和

C Version

Howard Guo

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int a[6]; int i=0; int total=0; //要記得設初始值 for(i=0;i<6;i++){ scanf("%d", &a[i]); } for(i=0;i<6;i++){ total += a[i] * a[i] * a[i]; } printf("%d\n", total); return 0; }

[C_AR04-易] 邊緣偵測

C++ Version

Howard Guo

#include <iostream> #include <cstring> using namespace std; int main() { int N, n, m, count_n, count_m; //n上下,m左右 cin >> N; for (int i = 1; i <= N; i++) { cin >> n >> m; int arr[n][m]; //開一個二維陣列 memset(arr, '0', n * m); //陣列清0 for (count_n = 0; count_n < n; count_n++) { for (count_m = 0; count_m < m; count_m++) { cin >> arr[count_n][count_m]; } } //印 for (count_n = 0; count_n < n; count_n++) //上下 { for (count_m = 0; count_m < m; count_m++) //左右 { //if (count_m == m - 1) //最後一列 //{ // if (arr[count_n][count_m] == 1) // cout << "0 "; //else // cout << "_ "; //} //else //{ //一般判斷 if (arr[count_n][count_m] == 1) { if (arr[count_n][count_m - 1] == 0 || arr[count_n][count_m + 1] == 0 || arr[count_n - 1][count_m] == 0 || arr[count_n + 1][count_m] == 0) { cout << "0 "; } else cout << "_ "; } else cout << "_ "; //} } cout << "\n"; } if (i != N) //兩圖形間空一行 cout << "\n"; } return 0; }

[C_AR05-易] 最少派車數

C Version

風思

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int n; scanf("%d",&n); int start[30]={0}; int end[30]={0}; int time[25]={0}; int i=0,j=0; for(i=0;i<n;i++){ scanf("%d %d",&start[i],&end[i]); } for(i=0;i<n;i++){ for(j=start[i];j<end[i];j++){ time[j]++; } } int max=time[0]; for(i=1;i<24;i++){ if(time[i]>max){ max=time[i]; } } printf("%d\n",max); return 0; }

[C_AR09-易] 兩數差值

C++ Version

Howard Guo
arr 的部分也可以用 vector 開動態陣列(但是我還沒學會)

#include <iostream> #include <cstring> //要用memset #include <algorithm> //要用sort using namespace std; int main(int argc, char const *argv[]) { string input; int arr[20], count, MAX, MIN; while (cin >> input) { MAX = 0, MIN = 0; memset(arr, -1, 20); count = 0; for (int i = 0; i < input.length(); i++) { //XXX.length() 取得字串長度 if (input[i + 1] == ',' || i == input.length() - 1) { arr[count++] = input[i] - '0'; } } //cout << count << endl; FOR DEBUGGING PURPOSE sort(arr, arr + count); //由小排到大 for (int i = count - 1; i >= 0; i--) { MAX = MAX * 10 + arr[i]; } //cout << "MAX=" << MAX << endl; FOR DEBUGGING PURPOSE for (int i = 0; i < count; i++) { MIN = MIN * 10 + arr[i]; } //cout << "MIN=" << MIN << endl; FOR DEBUGGING PUROPOSE cout << MAX - MIN << endl; } return 0; }

Java Version

解法一

風思

import java.util.*; public class Main { public static int[] Bsort(int[] input) { // 泡沫排列法 for (int i = input.length - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (input[j] > input[j + 1]) { int temp = input[j]; input[j] = input[j + 1]; input[j + 1] = temp; } } } return input; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String tokens[] = (str.split(",")); int num[] = new int[tokens.length]; for (int i = 0; i < tokens.length; i++) num[i] = Integer.parseInt(tokens[i]); Bsort(num); // 泡沫排列 int max = 0, min = 0; // 求min for (int i = 0; i < num.length; i++) { min += num[i] * (int) Math.pow(10, (double) (num.length - 1 - i)); } // 求max for (int i = num.length - 1; i >= 0; i--) { max += num[i] * (int) Math.pow(10, (double) (i)); } // System.out.println(max); // System.out.println(min); System.out.println(max - min); sc.close(); } }

[C_AR10-中] 新通話費率

Java Version

風思

import java.util.*; //[C_AR10-中] 新通話費率 //2019.07.09;20:41 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] input = sc.nextLine().split(","); double type = Double.parseDouble(input[0]); double time = Double.parseDouble(input[1]); double fee = 0; if (type == 186) { if (time * 0.09 < type) fee = type; else { if (time * 0.09 > 2 * type) { fee = Math.round(time * 0.09) * 0.8; } else { fee = Math.round(time * 0.09) * 0.9; } } } else if (type == 386) { if (time * 0.08 < type) fee = type; else { if (time * 0.08 > 2 * type) { fee = Math.round(time * 0.08) * 0.7; } else { fee = Math.round(time * 0.08) * 0.8; } } } else if (type == 586) { if (time * 0.07 < type) fee = type; else { if (time * 0.07 > 2 * type) { fee = Math.round(time * 0.07) * 0.6; } else { fee = Math.round(time * 0.07) * 0.7; } } } else if (type == 986) { if (time * 0.06 < type) fee = type; else { if (time * 0.06 > 2 * type) { fee = Math.round(time * 0.06) * 0.5; } else { fee = Math.round(time * 0.06) * 0.6; } } } System.out.printf("%.0f\n", fee); sc.close(); } }

[C_AR20-易] 檢查數值是否有重複

Java Version

風思
了解Set,不存入重複的資料,再比較輸入的陣列長度。

import java.util.*; //[C_AR20-易] 檢查數值是否有重複 //2019.07.20;14:23 public class Main { public static boolean checkRespect(String str) { Set<String> set = new HashSet<String>(); String[] tokens = str.split(" "); for (String token : tokens) set.add(token); if (set.size() != tokens.length) return true; else return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // 只是配合題目 sc.nextLine(); //吸收enter String str = sc.nextLine(); boolean isRespect = checkRespect(str); if (isRespect) System.out.println(0); else System.out.println(1); sc.close(); } }

[C_AR021-易] 成績統計

Java Version

風思
熟習spilt與二維陣列;等待勇者用C寫~

import java.util.*; //[C_AR021-易] 成績統計 // 2019,07,20;15:28 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 輸入學生數 int n = sc.nextInt(); sc.nextLine(); double[][] score = new double[n][3]; String[][] input = new String[n][3]; // 一行輸入一個學生的三科分數 for (int i = 0; i < n; i++) { input[i] = sc.nextLine().split(" "); } // 字串轉數字 for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { score[i][j] = Double.parseDouble(input[i][j]); } } double average_all = 0, average_chinese = 0, average_english = 0, average_math = 0; // 累加 for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { average_all += score[i][j]; if (j == 0) average_chinese += score[i][j]; if (j == 1) average_english += score[i][j]; if (j == 2) average_math += score[i][j]; } } // 算出各項平均 average_all /= (n * 3); average_chinese /= n; average_english /= n; average_math /= n; // 輸出 System.out.printf("%.1f %.1f %.1f %.1f\n", average_all, average_chinese, average_english, average_math); sc.close(); } }

[C_AR022-易] 字母出現的頻率

C Version

Howard Guo

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { //https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=220 //[C_AR022-易] 字母出現的頻率 int result[26] = {0}; //最終輸出,26個字母 初始值=0 char a[200]; // 輸入值存放處,最大可放200個字元 int i , temp; //計數器 scanf("%[^\n]" , &a); // %[^\n]表示除了\n以外都要讀入,否則scanf遇到空格就會中斷 for(i=0;a[i] !='\0'; i++){ //使a輸出到字串結尾就結束,不用全部印完 if(a[i]>='A' && a[i]<='Z') { //判斷大寫 temp = a[i] - 'A'; //透過ASCII CODE判斷該字元在序列中的順序 result[temp]++; //在該位置+1 } if(a[i]>='a' && a[i]<='z') { //判斷大寫 temp = a[i] - 'a'; //透過ASCII CODE判斷該字元在序列中的順序 result[temp]++; //在該位置+1 } } for(i=0;i<26;i++){ if(i==0) printf("%d", result[i]); //第一個不要加空格 else printf(" %d", result[i]); //之後都要空格 } printf("\n"); return 0; }

[C_AR023-易] 字根與子字串

Java Version

風思

import java.util.*; //[C_AR023-易] 字根與子字串 //2019,07,20;16:12 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String t = sc.nextLine(); if (t.contains(s)) System.out.println("YES"); else System.out.println("NO"); sc.close(); } }

[C_AR025-易] 計算ASCII字元

C++ Version

Howard Guo

#include <iostream> #include <cstring> #include <algorithm> //使用sort() using namespace std; int main(int argc, char const *argv[]) { int num; char str[100]; int arr[100]; int count = 1, arr_count = 0; memset(arr, '0', 100); memset(str, ' ', 100); //陣列清空,這裡不用0是因為測資可能會涵蓋到 while (cin.getline(str, 99, '\n')) //getline的參數,讀進去str、讀幾個(值必須小於str宣告時的長度)、停止字元(Optional) { //讀進來以後,要先知道有幾個字元 for (num = 0; num < 100; num++) { if (str[num] == ' ') { num--; break; } } sort(str, str + num); //從小排到大,註:此處用str+num是因為在C/C++中,變數str實際上代表的是該段記憶體位址的首位,這邊是直接操作指標 reverse(str, str + num); //從大排到小(題目要求) arr_count = 0; //沒有這行的話,第二次輸入後,下面在印出陣列內容時會抓錯值。 for (int i = 0; i <= num; i++) { if (str[i + 1] == str[i]) { arr[arr_count] = ++count; } else { arr[arr_count] = count; count = 1; arr_count++; } } arr_count = 0; //cout << str << endl; //cout << num << endl; //cout << arr_count << endl; for (int i = 0; i < num; i++) { if (str[i] != str[i + 1]) { cout << int(str[i]) << " " << arr[arr_count++] << endl; //if(arr[arr_count]!=0) cout << endl; } } memset(str, ' ', 100); //陣列清空,準備給下一次迴圈使用 memset(arr, '0', 100); } return 0; }

Java Version

風思

import java.util.*; //[C_AR025-易] 計算ASCII字元 //2019/07/09;22:13 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int[] result=new int[128]; for (int i = 0; i < str.length(); i++) { result[str.codePointAt(i)]++; // 返回ASCII數值 } for (int i = result.length - 1; i >= 0; i--) { if (result[i] != 0) System.out.printf("%d %d\n", i, result[i]); } sc.close(); } }

[C_AR029-難] 文字編碼

Java Version

風思
沒比想像的難

import java.util.*; //[C_AR029-難] 文字編碼 //2019,08,26;21:40 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String inputStr = sc.nextLine(); int N = inputStr.length(); int M; for (M = 1; M <= 15; M++) { if (M * M >= N) break; } String[][] arr = new String[M][M]; for (int i = 0; i < M; i++) Arrays.fill(arr[i], " "); String[] tokens = inputStr.split(""); int index = 0; for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { if (index < tokens.length) arr[i][j] = tokens[index++]; } } StringBuffer strBuf = new StringBuffer(); for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { strBuf.append(arr[j][i]); } } String result = strBuf.toString(); System.out.println(result); sc.close(); } }

[C_AR031-中] 一維矩陣表示二維平面空間

Java Version

風思
有點繁雜

import java.util.*; //[C_AR031-中] 一維矩陣表示二維平面空間 //2019,08,28;22:12 public class Main { // N為列數(有幾列) public static int indexReturn(int N, int i, int j) { // 判斷是否超出邊界,若超出回傳-1 // 若在邊界內則回傳對應一維矩陣索引值 if (i < 0 || j >= N || i >= N || j < 0) return -1; else return (i * N + j); } // 輸入一維矩陣索引值,回傳對應二維矩陣之內容 public static String getDate(String[][] x, int index) { int N = x.length; int pi = index / N; int pj = index % N; if (pi < 0 || pj >= N || pi >= N || pj < 0) return "-1"; else return x[pi][pj]; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int index = sc.nextInt(); int pi = index / N; int pj = index % N; String[][] arr = new String[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) arr[i][j] = (char) ('A' + i) + "" + (j + 1); // 輸出一維矩陣索引 System.out.print(indexReturn(N, pi - 1, pj - 1) + " "); System.out.print(indexReturn(N, pi - 1, pj) + " "); System.out.print(indexReturn(N, pi - 1, pj + 1) + " "); System.out.print(indexReturn(N, pi, pj + 1) + " "); System.out.print(indexReturn(N, pi + 1, pj + 1) + " "); System.out.print(indexReturn(N, pi + 1, pj) + " "); System.out.print(indexReturn(N, pi + 1, pj - 1) + " "); System.out.println(indexReturn(N, pi, pj - 1)+" "); // 印出對應資料 System.out.print(getDate(arr, indexReturn(N, pi - 1, pj - 1)) + " "); System.out.print(getDate(arr, indexReturn(N, pi - 1, pj)) + " "); System.out.print(getDate(arr, indexReturn(N, pi - 1, pj + 1)) + " "); System.out.print(getDate(arr, indexReturn(N, pi, pj + 1)) + " "); System.out.print(getDate(arr, indexReturn(N, pi + 1, pj + 1)) + " "); System.out.print(getDate(arr, indexReturn(N, pi + 1, pj)) + " "); System.out.print(getDate(arr, indexReturn(N, pi + 1, pj - 1)) + " "); System.out.println(getDate(arr, indexReturn(N, pi, pj - 1))+" "); sc.close(); } }

[C_AR34-易] 身分證驗證器

C++ Version

Howard Guo
笨版,用 switch 硬幹,超級笨

#include <iostream> #include <cstring> int table(char); using namespace std; int main(int argc, char const *argv[]) { int X1, X2, N[9], P; string id; while (getline(cin, id)) { memset(N, 0, 9); P = 0; X1 = table(id[0]) / 10; X2 = table(id[0]) % 10; for (int i = 0; i < 9; i++) { N[i] = id[i + 1] - '0'; } P = X1 + (9 * X2) + (8 * N[0]) + (7 * N[1]) + (6 * N[2]) + (5 * N[3]) + (4 * N[4]) + (3 * N[5]) + (2 * N[6]) + N[7] + N[8]; if (P % 10 == 0) { cout << "CORRECT!!!" << endl; } else { cout << "WRONG!!!" << endl; } } return 0; } int table(char ch) { if (ch == 'A') return 10; else if (ch == 'B') return 11; else if (ch == 'C') return 12; else if (ch == 'D') return 13; else if (ch == 'E') return 14; else if (ch == 'F') return 15; else if (ch == 'G') return 16; else if (ch == 'H') return 17; else if (ch == 'J') return 18; else if (ch == 'K') return 19; else if (ch == 'L') return 20; else if (ch == 'M') return 21; else if (ch == 'N') return 22; else if (ch == 'P') return 23; else if (ch == 'Q') return 24; else if (ch == 'R') return 25; else if (ch == 'S') return 26; else if (ch == 'T') return 27; else if (ch == 'U') return 28; else if (ch == 'V') return 29; else if (ch == 'X') return 30; else if (ch == 'Y') return 31; else if (ch == 'W') return 32; else if (ch == 'Z') return 33; else if (ch == 'I') return 34; else if (ch == 'O') return 35; else return 0; }

Java Version

風思
本來也想硬幹,但後來想到有對應的方法~

import java.util.*; //[C_AR34-易] 身分證驗證器 //2019.07.13;21:46 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[] input = sc.nextLine().split(""); String consult = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; int[] Code = new int[consult.length()]; for (int i = 0; i < Code.length; i++) Code[i] = i + 10; //賦值 int code = Code[consult.indexOf(input[0])]; //查表 int X1 = code / 10; int X2 = code % 10; int[] N = new int[10]; // N1~N9 for (int i = 1; i < N.length; i++) N[i] = Integer.parseInt(input[i]); int P = X1 + 9 * X2 + 8 * N[1] + 7 * N[2] + 6 * N[3] + 5 * N[4] + 4 * N[5] + 3 * N[6] + 2 * N[7] + N[8] + N[9]; if (P % 10 == 0) System.out.println("CORRECT!!!"); else System.out.println("WRONG!!!"); } sc.close(); } }

[C_AR35-易] 生肖問題

C Version

風思

#include <stdio.h> #define ROW 12 #define COL 10 int count (int x){ if(x<=2008 && (2008-x)%12==0) return 0; else if(x>2008) return (x-2008)%12; else return 12-(2008-x)%12; } int main() { char zodic[ROW][COL]={"rat","ox","tiger","rabbit","dragon","snake","horse","sheep","monkey","rooster","dog","pig"}; int n,i=0,flag=1; scanf("%d",&n); while(flag==1){ if(zodic[count(n)][i]=='\0') flag=0; //break; else { printf("%c",zodic[count(n)][i++]); } } printf("\n"); return 0; }

[C_AR36-易] 星座查詢

Java Version

風思

import java.util.*; //[C_AR36-易] 星座查詢 //2019,08,17;17:55 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // System.out.println(month+" "+day); while (sc.hasNext()) { int month = sc.nextInt(); int day = sc.nextInt(); if (month == 1) { if (day >= 21) System.out.println("Aquarius"); else System.out.println("Capricorn"); } else if (month == 2) { if (day >= 19) System.out.println("Pisces"); else System.out.println("Aquarius"); } else if (month == 3) { if (day >= 21) System.out.println("Aries"); else System.out.println("Pisces"); } else if (month == 4) { if (day >= 21) System.out.println("Taurus"); else System.out.println("Aries"); } else if (month == 5) { if (day >= 22) System.out.println("Gemini"); else System.out.println("Aries"); } else if (month == 6) { if (day >= 22) System.out.println("Cancer"); else System.out.println("Gemini"); } else if (month == 7) { if (day >= 23) System.out.println("Leo"); else System.out.println("Cancer"); } else if (month == 8) { if (day >= 24) System.out.println("Virgo"); else System.out.println("Leo"); } else if (month == 9) { if (day >= 24) System.out.println("Libra"); else System.out.println("Virgo"); } else if (month == 10) { if (day >= 24) System.out.println("Scorpio"); else System.out.println("Libra"); } else if (month == 11) { if (day >= 23) System.out.println("Sagittarius"); else System.out.println("Scorpio"); } else if (month == 12) { if (day >= 22) System.out.println("Capricorn"); else System.out.println("Sagittarius"); } } sc.close(); } }

[C_AR41-易] 一整數序列所含之整數個數及平均值

Java Version

風思

import java.util.*; //[C_AR41-易] 一整數序列所含之整數個數及平均值 //2019.07.09;23:14 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[] num = sc.nextLine().split(" "); int Size = num.length; double Average = 0; for (int i = 0; i < Size; i++) { Average += Double.parseDouble(num[i]); } Average /= Size; System.out.println("Size: " + Size); System.out.printf("Average: %.3f\n", Average); } sc.close(); } }

[C_AR42-易] 過半元素

Java Version

風思

import java.util.*; //[C_AR42-易] 過半元素 //2019.07.13;19:58 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input = sc.nextLine(); String[] nums = input.split(" "); String[] control = input.split(" "); // 對照組 Double N = nums.length / 2.0; int[] result = new int[nums.length]; for (int i = 0; i < nums.length; i++) { for (int j = i; j < nums.length && (!nums[j].equals("iscount")); j++) { if (nums[i].equals(nums[j]) && (!nums[j].equals("iscount"))) { result[i]++; if (i != j) nums[j] = "iscount"; } } nums[i] = "iscount"; } boolean isfind = false; int index = 0; for (int i = 0; i < result.length; i++) { if (result[i] > N) { isfind = true; index = i; break; } } if (isfind) System.out.println(control[index]); else System.out.println("NO"); } sc.close(); } }

[C_AR43-易] 以遞迴函數計算冪次

C Version

風思

#include <stdio.h> unsigned long long intPower(int m,int n){ if(n==0) return 1; else if(n==1) return m; else return (m*intPower(m,n-1)); } int main() { int m,n; while(scanf("%d %d",&m,&n)!=EOF){ printf("%ld",intPower(m,n)); printf("\n"); } return 0; }

[C_AR44-易] 迴文問題

C Version

風思

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char a[80]; scanf("%s",&a); int size = 0 ; int flag = 1; int i ; for(i=0;i<80;i++){ if(a[i]=='\0'){ break; } size++; } //printf("%d\n",size); for(i=0;i<size/2;i++){ if(a[i] != a[size-1-i]){ printf("NO\n"); flag =0; break; } } if(flag==1) printf("YES\n"); return 0; }

[C_AR45-易] 輸入字串算字元

Java Version

風思
就是這麼簡單

import java.util.*; //[C_AR45-易] 輸入字串算字元 //2019,7,09;22:20 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.printf("There are %d characters\n", sc.nextLine().length()); sc.close(); } }

[C_AR47-易] 利用指標傳遞陣列到函數

Java Version

風思
有空再用C寫吧~

import java.util.*; //[C_AR47-易] 利用指標傳遞陣列到函數 //2019.07.09;22:57 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[] num = sc.nextLine().split(" "); for (int i = num.length - 1; i >= 0; i--) { if (i != 0) System.out.print(num[i] + " "); else System.out.println(num[i]); } } sc.close(); } }

[C_AR48-易] 數字加密

C Version

風思

#include <stdio.h> int change (int arr[],int size){ int i; for(i=0;i<size;i++) arr[i]=(arr[i]+7)%10; int temp =arr[0]; arr[0]=arr[2]; arr[2]=temp; int temp2=arr[1]; arr[1]=arr[3]; arr[3]=temp2; } int main() { int key [4]={0}; int i=0; for (i=0;i<4;i++)scanf("%1d",&key[i]); change(key,4); for(i=0;i<4;i++) printf("%d",key[i]); printf("\n"); return 0; }

[C_AR57-易] 刪除重複資料

此題用 C++ 的 cin 跟 getline 需要注意 這個觀念

Select a repo