[作業] 網路程式設計 Week 2
國立嘉義大學網路程式設計作業,可觀摩但請勿抄襲。
國立嘉義大學網路程式設計作業,可觀摩但請勿抄襲。
國立嘉義大學演算法作業,可觀摩但請勿抄襲。
二分搜尋法 Binary Search 搜尋是一種很經典的演算法, 其中有一種搜尋法是將 已排序 的數列, 藉由比大小將數列分成 一半 並 縮小 搜尋範圍。 稱之為 「二分搜尋法」、「二元搜尋法」! #include <iostream> #include <algorithm> using namespace std; void binarySearch(int *number, int wanted, int strat, int stop); int main() { int number[105]; int n; while (cin>>n) { for (int i=0;i<n;i++) cin>>number[i]; sort(number,number+n); int m, wanted; cin>>m; for (int i=0;i<m;i++) { cin>>wanted; binarySearch(number, wanted, 0, n-1); cout<<endl; } } } void binarySearch(int *number, int wanted, int start, int stop) { int next = (start + stop) / 2; if (start > stop) { cout<<"error"; return ; } cout<<next<<" "<<number[next]<<" "; if (wanted > number[next]) binarySearch(number, wanted, next+1, stop); else if (wanted == number[next]) return ; else binarySearch(number, wanted, start, next-1); }
我只解出一元二次、三次方程式 . . . 我好爛 import math as math def toFactor(matrix) : length = len(matrix) if length == 4 : a, b = matrix[0], matrix[1] c, d = matrix[2], matrix[3] A = 1 B = (a+d) * (-1) C = a*d - b*c return A,B,C if length == 9 : a, b, c = matrix[0], matrix[1], matrix[2] d, f, g = matrix[3], matrix[4], matrix[5] h, j, l = matrix[6], matrix[7], matrix[8] A = 1 B = - a - f - l C = - b*d + a*f - c*h - g*j + a*l + f*l D = c*f*h + b*g*h + c*d*j + a*g*j + b*d*l - a*f*l return A,B,C,D if length == 16 : a, b, c, d = matrix[0], matrix[1], matrix[2], matrix[3] f, g, h, j = matrix[4], matrix[5], matrix[6], matrix[7] l, m, o, p = matrix[8], matrix[9], matrix[10], matrix[11] q, r, s, t = matrix[12], matrix[13], matrix[14], matrix[15] A = 1 B = - a - g - o - t C = - b*f + a*g - c*l - h*m + a*o + g*o - d*q - j*r - p*s + a*t + g*t + o*t D = ( c*g*l - b*h*l - c*f*m + a*h*m + b*f*o - a*g*o + d*g*q - b*j*q + d*o*q - c*p*q - d*f*r + a*j*r + j*o*r - h*p*r - d*l*s - j*m*s + a*p*s + g*p*s + b*f*t - a*g*t + c*l*t + h*m*t - a*o*t - g*o*t) E = (d*h*m*q - c*j*m*q - d*g*o*q + b*j*o*q + c*g*p*q - b*h*p*q - d*h*l*r + c*j*l*r + d*f*o*r - a*j*o*r - c*f*p*r + a*h*p*r + d*g*l*s - b*j*l*s - d*f*m*s + a*j*m*s + b*f*p*s - a*g*p*s - c*g*l*t + b*h*l*t + c*f*m*t - a*h*m*t - b*f*o*t + a*g*o*t) return A,B,C,D,E def solveFunction(f) : length = len(f) if length == 3 : A, B, C = f[0],f[1],f[2] x1 = ( -B + (B*B - 4*A*C)**(1/2)) / (2*A) x2 = ( -B - (B*B - 4*A*C)**(1/2)) / (2*A) return x1,x2 if length == 4 : A, B, C, D = f[0], f[1], f[2], f[3] p = ( -(B*B) / (3*A*A) ) + (C/A) q = ( (2*B*B*B) / (27*A*A*A) ) - ((B*C) / (3*A*A)) + (D/A) w = complex(-1,3**(1/2)) / 2 U = (-q / 2) + ( ( ( q / 2 )**2 + ( p / 3 )**3 )**(1/2) ) V = (-q / 2) - ( ( ( q / 2 )**2 + ( p / 3 )**3 )**(1/2) ) if U > 0 : U = math....
「欸!你這題怎麽做?」 程式電神曾經說過:「 copy and paste 」 「 . . . 」 我謹記在心 def makeMatrix(m) : length = len(m) size = int( length**(1/2) ) matrix = [] for i in range(size) : newRow = [] for j in range(size) : newRow.append(m[i*size+j]) matrix.append(newRow) return matrix def makeCoordinate(m) : length = len(m) matrix = [] for row in range(length) : newRow = [m[row]] matrix.append(newRow) return matrix def transpose(m) : lengthRow = len(m) lengthCol = len(m[0]) matrix = [] for col in range(lengthCol) : newRow = [] for row in range(lengthRow) : newRow....