Posts

zerojudge d122. Oh! My Zero!!

 zerojudge d122. Oh! My Zero!! 千萬別用暴力解( 1a和2a的解法)!!,因為一定會TLE。 千萬別用2b!!,因為輸入很大(長整數),這樣字要用很大的記憶體,會出現錯誤(RE),我試過,真的。 解法大全: 1.依思路分: a)直接算階乘 b)算5法 2.建表 a)建階乘表 b)建5表 千萬別用暴力解( 1a和2a的解法)!!,因為一定會TLE。 千萬別用2b!!,因為輸入很大(長整數),這樣字要用很大的記憶體,會出現錯誤(RE),我試過,真的。 思路: 數學歸納法 1. 首先,你要先了解10=2*5,且2出現的次數比5多 所以要算出現比較少的次數(5的次數) 2. 再來你得先寫兩個表格 分別為, a)5的倍數數字的質因數中有多少的5(5的倍數數字可以整除幾次5) 用數學式表示:n=k*5^m (k,m為正整數) b)輸入和輸出的關係 3. 找規律 我的解法: 先用這方式寫 2b)先建表(1~1000000中能被5整除的次數),在逐項加起來 # define PO 7 const int MX= pow ( 10.0 ,PO);//because n is too large // MX=pow(10.0,5); typedef long long int LLI; int five_n[MX]={ 0 }; void print ( int *, int ) ; void build () { //memset(five_n,0,sizeof(five_n)); for ( int i= 1 ;i<=PO;i++) { int n= pow ( 5.0 ,i); for ( int j=n;j<=MX;j+=n) { five_n[j]++; } } return ; } int deal (LLI n) { int sum= 0 ; for ( int i= 5 ;i<=n;i+= 5 ) { sum+=five_n[i]; } return sum; } RE   (SIGSEGV)   1b)算5法:就是輸入一個數字N,算N!可以被5除多少次...

zerojudge-a147: Print it all

  zerojudge-a147: Print it all difficulty: extremely easy (for beginners) Q: give a number "n", output 1 to n increasingly except for the modulus of 7. tips: use a nested loop a "while" loop for input a "for" loop for output.

zerojudge a799 適合新手看的超多解法

  zerojudge a799 正值國 題意:  如果一個數的絕對值。   解法1: 用數字型別讀取 整數用abs(),浮點數用fabs() 優點:簡單 缺點:無法處理大數 解法2: 用string型別讀取 用string 類別中的erase把負號弄掉 優點:可以處理大數 解法3: 用string或char陣列型別讀取 不要列印出負號即可,反正它只判斷結果 如下: string s; int i=0; cin>>s; if(s[i]=='-')i++; for(;i<s.length();i++){cout<<s[i];} cout<<endl; 優點:如果用char 陣列,不需用函式 缺點:程式碼較長,執行時較慢

zerojudge b367.翻轉世界

 zerojudge b367.翻轉世界 題意: 判斷一組二維陣列數字翻轉180度後,會部會跟原來的陣列一模一樣,即判斷該組陣列是不是 對角線對稱。 輸入: 第一行為T,代表有T組測資。 每一組測資有n,m,代表有n,代表有n行m列的陣列。 接下來有n*m個數字r(0<=r<=231-1) 輸出: 對於每個測資,判斷是否可以符合翻轉180度不會改變的圖形 是的話請輸出 go forward 否的話請輸出 keep defending  解法大全: 依陣列維度: 解法1:開二維陣列 (n行m列) 解法2:開一維陣列(n*m個) 因為一維陣列和二維陣列的判斷方式一樣 依型態: 解法1:用數字 解法2:用字串和reverse()函式 最好不要用字元,因為r最大到231-1 解法3:用STL <deque> 存入後,用front()和back()比較 deque <int>deq1; 如果deq1.front()!=deq1.back()代表不能翻轉 解法4:用STL<list> 同上 註:更多STL資訊你可以上網查

zerojudge d068. 該減肥了

 zerojudge d068. 該減肥了 解法大全: 1.判斷式 2.三元運算子 3.布林比較:      w=w-(w>50);     //same as w-=(w>50)     //not w=w-w>50;     //"-" has  higher priority to">" , this will always get 0     //more complex     w=(w<=50)*w+(w>50)*(w-1);

zerojudge d065: 三人行必有我師

 zerojudge d065: 三人行必有我師 這個最好用 陣列 解法大全: 判斷: 三元運算子: 內建函數: max() 用以下函數前要先引進<algorithm> max_element() 如下: #include <iostream> using namespace std; //#include <algorithm> //according to the version of compiler int main() { int arr[3];         cin>>arr[0];int MAX=arr[0]; for(int i=1;i<3;i++) { cin>>arr[i];                    MAX=max(arr[i],MAX); } cout<<MAX<<endl; return 0; } or #include <iostream> using namespace std; #include <algorithm> int main() { int arr[3]; for(int i=0;i<3;i++) { cin>>arr[i]; } int MAX= *max_element(arr,arr+3); cout<<MAX<<endl; return 0; } 注意: 引進 <algorithm>時,可以不用引進<iostream>,如下: # include <cstdio> using namespace std ; # include <algorithm> int main () { int arr[ 3 ]; for ( int i= 0 ;i< 3 ;i++) { scanf ( "%d" ,&arr[i]); } int MA...

zerojudge d063. 0 與 1

zerojudge d063. 0 與 1 題意:若輸入0輸出1,若輸入1輸出0 解法大全: 判斷式: 三元運算子 布林代數:! n=!n; n= not n; n=n^1 (n^=1) n= n xor 1 布林代數比較: n=(n!=1) n=(n==0); n=(n<1); 式子: 求經過(1,0)和(0,1)的點 ans: 1)y=-(x-1); 2)y=(x-1)*(x-1); 3)y=| x-1|; [process and proof] 1)Let it be a linear equation(y=mx+b) replace the points (0,1) and (1,0). 0=1*m+b; 1=0*m+b; get m=-1,b=1; 2)Let it be a quadratic equation(y=a*x*x+b*x+c) replace the points (0,1) and (1,0). 1=a*0+b*0+c; 0=a*1+b*1+c; get c=1; which has infinitely many solution.(a*x*x-b*x=-1) the euation can be simplify y=(x-1)*(x-1); if a=1,b=-2; 3)according to 1),2) we can conclude that y=|x-1|; #end