updata:20170621
好的,已經是準高一了,現在看起來太蠢了。。。
--------------------------------------------------------------------------------------
要真正的運用,程序一定是要來解決實際問題的——比如作業(懶就直說)
蛤蛤蛤蛤蛤蛤這里就是黑の魔法作業程序的存放地啦
當然了,現在只是初中生,開始解決繁雜作業的程序或許也就是用些基礎部分解決問題。
不過還是很期待之后能用上算法來解決作業啦,又是一個學習歷程記錄處咯!
?
一、火柴三角推廣
……對,沒有代碼,不過這是第一個我用程序解決作業的程序,只是可惜無法找到源代碼了。只記得是用幾根火柴擺幾個三角形什么的,是八年級上期百練百勝里的題,自己還機(zuo)智(si)推廣到一般情況。至少給第一次嘗試留個紀念吧。
?
二、平均數、方差計算


1 #include <stdio.h> 2 #include <string.h> 3 4 float num[110] = {0}; 5 6 int main (){ 7 // freopen ("a.in","r",stdin); 8 // freopen ("a.out","w",stdout); 9 10 int i,n,j; 11 float sum = 0,k; 12 13 i = 0; 14 n = 1; 15 while (1){ 16 i++; 17 scanf ("%f",num+i); 18 sum+=num[i]; 19 20 char c = getchar(); 21 if (c == '\n'){ 22 float ping = sum/i; 23 printf ("第%d組的平均數為:%f\n",n,ping); 24 25 for (j = 1,k = 0;j<=i;j++) 26 k+=(num[j]-ping)*(num[j]-ping); 27 printf (" 方差為:%f\n\n",k/i); 28 29 sum = 0; n++; i = 0; 30 memset(num,0,sizeof (num)); 31 } 32 if (c == 'E'){ 33 return 0; 34 } 35 } 36 37 return 0; 38 }
對,特別簡單,還毫無泛化設計,不過確實夠用,也是幫了我很大忙的
?
三、二次函數自配方


1 #include <bits/stdc++.h> 2 3 inline void swap(int &a,int &b){ 4 int t = a;a = b,b = t; 5 } 6 inline void Gcd (int &aa,int &bb){ 7 int a = aa,b = bb; 8 if (a<b) 9 swap (a,b); 10 int r = a%b; 11 12 while (r){ 13 a = b; 14 b = r; 15 r = a%b; 16 } 17 18 aa/=b; 19 bb/=b; 20 } 21 22 void First (int a){ 23 if (abs(a) == 1){ 24 if (a == -1) 25 std::cout<<'-'; 26 } 27 else 28 std::cout<<a; 29 } 30 void MLPrint (bool f,int a,int b){ 31 if (f == false) 32 std::cout<<'-'; 33 else 34 std::cout<<'+'; 35 std::cout<<a; 36 if (b != 1) 37 std::cout<<'/'<<b; 38 } 39 void Mid (int a,int b){ 40 int ha = b,hb = 2*a; 41 bool hf = (ha*hb)>0; 42 ha = abs(ha),hb = abs(hb); 43 Gcd(ha,hb); 44 45 std::cout<<"(x"; 46 MLPrint(hf,ha,hb); 47 std::cout<<")^2"; 48 } 49 void Last (int a,int b,int c){ 50 int ka = 4*a*c-b*b,kb = 4*a; 51 bool kf = (ka*kb)>0; 52 ka = abs(ka),kb = abs(kb); 53 Gcd (ka,kb); 54 55 MLPrint (kf,ka,kb); 56 } 57 58 int main (){ 59 // freopen ("a.in","r",stdin); 60 // freopen ("a.out","w",stdout); 61 62 int a,b,c; 63 while (std::cin>>a>>b>>c){ 64 std::cout<<"y = "; 65 if (abs(a) == 1){ 66 if (a == -1) 67 std::cout<<"-"; 68 std::cout<<"x^2"; 69 } 70 else 71 std::cout<<a<<"x^2"; 72 if (abs(b) == 1){ 73 if (b == -1) 74 std::cout<<"-"; 75 std::cout<<"x"; 76 } 77 else if (b!=0){ 78 if (b>1) 79 std::cout<<'+'; 80 std::cout<<b<<"x"; 81 } 82 if (c!=0){ 83 if (c>0) 84 std::cout<<'+'; 85 std::cout<<c; 86 } 87 std::cout<<'\n'; 88 std::cout<<" = "; 89 90 First (a); 91 92 if (b == 0 && c == 0){ 93 std::cout<<'\n'; 94 continue; 95 } 96 if (b == 0) 97 std::cout<<"x2"; 98 else 99 Mid (a,b); 100 Last (a,b,c); 101 102 std::cout<<'\n'; 103 } 104 105 return 0; 106 }
輸入形如ax^2+bx+c的二次函數的a,b,c
輸出該二次函數的一般式與頂點式
其實也很簡單,可以當新手練手那種,不過承認確實寫得繁雜了。不過一直秉承不影響不優化原則(就是懶),也懶得優化了。