第一周 基礎練習
1、顯示Hello World!

#include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }
2、顯示唐詩

#include <iostream> using namespace std; int main() { cout<< "慈母手中線\n" "游子身上衣\n" "臨行密密縫\n" "意恐遲遲歸\n" "誰言寸草心\n" "報得三春暉\n"; return 0; }

#include <iostream> using namespace std; int main() { cout<< "慈母手中線\n\ 游子身上衣\n\ 臨行密密縫\n\ 意恐遲遲歸\n\ 誰言寸草心\n\ 報得三春暉" << endl; return 0; }
3、顯示一句話

#include <iostream> #include <string> using namespace std; int main() { string name; cin >> name; cout << "This program is coded by " + name + "." << endl; return 0; }
4、還是一句話,getline(cin,name); 是string類重載的友元函數;cin.getline()是istream類的成員函數

#include <iostream> #include <string> using namespace std; int main() { string name; getline(cin,name); cout << "This program is coded by " + name + "." << endl; return 0; }

#include <iostream> using namespace std; int main() { char name[20]; /* 從鍵盤輸入字符時,字符一直在鍵盤緩沖區中(shell緩沖區), 當在鍵盤輸入'\n'時,鍵盤緩沖區中的字符才被全部傳送到stdin緩沖區中 */ cin >> name; cout << name << endl; cin >> name; /* 忽略空白符 緩沖區還剩余一個回車*/ cout << name << endl; cin.sync(); /* 清空緩沖區 */ /* 可以讀取空白符'\n' 以回車作為結束標記並且讀取回車符 緩沖區沒有剩余*/ cin.getline(name, 15); cout << name << endl; cout << getchar() << endl; /* 緩沖區是空的 等待輸入 */ return 0; }
5、計算矩形周長

#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << 2 * ( a + b ) << endl; return 0; }
6、已知直角邊求斜邊

#include <iostream> #include <cmath> using namespace std; int main() { double a, b, c; cin >> a >> b; cout << sqrt ( a*a + b*b ) << endl; return 0; }
第一周 中級練習
1、計算公式的值(對數),以m為底的n的對數 log(n) / log(m)

#include <iostream> #include <cmath> using namespace std; int main() { double x, a; cin >> x >> a; cout << log(x+sqrt(x*x+1)) / log(a) << endl; return 0; }
2、e的近似值

#include <iostream> #include <cmath> using namespace std; int main() { double n; cin >> n; cout << pow(1+1/n, n) << endl; return 0; }

#include <iostream> using namespace std; int main() { int n, t; double y = 1.0; cin >> n; t = n; while(t--) { y *= (1+1.0/n); } cout << y << endl; return 0; }
3、計算公式的值(三角等)

#include <iostream> #include <cmath> using namespace std; int main() { double x; cin >> x; cout << sin(x) - log(x) + sqrt(x) - 5 << endl; return 0; }
4、計算公式的值(開方)

#include <iostream> #include <cmath> using namespace std; int main() { double x; cin >> x; cout << x / sqrt(x*x-3*x+2) << endl; return 0; }
第一周 編程作業
1、1-1我愛C++

#include <iostream> using namespace std; int main() { cout << "Hello C++." << endl; cout << "I like programming." << endl; return 0; }
2、1-2來自系統的問候,注意字符數組的長度

#include <iostream> using namespace std; int main() { char name[256]; cin.getline( name, 35 ); cout << "Hello " << name << "." << endl; return 0; }
3、1-3乘法計算器

#include <iostream> using namespace std; int main() { double a, b; cin >> a >> b; cout << a * b << endl; return 0; }
4、1-4單位換算

#include <iostream> using namespace std; int main() { double inch; const double inch_to_cm = 2.54; cin >> inch; cout << inch << "inch=" << inch * inch_to_cm << "cm" << endl; return 0; }
5、1-5平方根計算器

#include <iostream> #include <cmath> using namespace std; int main() { double x; cin >> x; cout << sqrt(x) << endl; return 0; }
第二周 基礎練習
1、求過平面上兩點的直線的斜率,斜率 ( y1 - y2 ) / ( x1 - x2 )

#include <iostream> using namespace std; int main() { double x1, y1 , x2, y2; cin >> x1 >> y1 >> x2 >> y2; cout << ( y1 - y2 ) / ( x1 - x2 ) << endl; return 0; }
2、計算平面上兩點之間的距離

#include <iostream> #include <cmath> using namespace std; int main() { double x1, y1 , x2, y2, distance; cin >> x1 >> y1 >> x2 >> y2; distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); cout << distance << endl; return 0; }
3、判斷大小寫

#include <iostream> using namespace std; int main() { char ch; cin >> ch; if(ch >= 'A' && ch <= 'Z') cout << 1 << endl; else cout << 0 << endl; return 0; }
4、判斷數字

#include <iostream> using namespace std; int main() { char ch; cin >> ch; if(ch >= '0' && ch <= '9') cout << 1 << endl; else cout << 0 << endl; return 0; }
5、判斷閏年

#include <iostream> using namespace std; int main() { int year; cin >> year; if(year%4==0&&year%100!=0||year%400==0) cout << "IsLeapYear" << endl; else cout << "NotLeapYear" << endl; return 0; }
6、求商和余數

#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << a/b << " " << a%b << endl; return 0; }
7、計算平均分取整

#include <iostream> using namespace std; int main() { int x, y, sum = 0, n = 7; while(n--) { cin >> x; sum += x; } y = int( sum/7.0 + 0.5 ); cout << y << endl; return 0; }
8、計算點到直線的距離保留兩位小數,int( d*100 + 0.5 ) / 100.0

#include <iostream> #include <cmath> using namespace std; int main() { int A, B, C, x, y; cin >> A >> B >> C; cin >> x >> y; double d = fabs(A*x+B*y+C) / sqrt(A*A+B*B); cout << int( d*100 + 0.5 ) / 100.0 << endl; return 0; }
9、輸入字符顯示ASCII值,int(ch)

#include <iostream> using namespace std; int main() { char ch; cin >> ch; cout << int(ch) << endl; return 0; }
10、輸入整數顯示ASCII字符

#include <iostream> using namespace std; int main() { int k; cin >> k; cout << char(k) << endl; return 0; }
11、輸入整數顯示十六進制

#include <iostream> using namespace std; int main() { int n; cin >> n; cout << hex << n << endl; return 0; }
12、輸入整數顯示十六進制和八進制,int sign = n < 0 ? (n = -n, -1) : 1;

#include <iostream> using namespace std; int main() { int n; cin >> n; int sign = n < 0 ? (n = -n, -1) : 1; if(sign == -1){ cout << n*sign << " -" << hex << n << " -"<< oct << n << endl; } else{ cout << n << " " << hex << n << " "<< oct << n << endl; } return 0; }

#include <iostream> using namespace std; int main() { int n; cin >> n; int sign = n < 0 ? (n = -n, -1) : 1; sign == -1 ? cout << n*sign << " -" << hex << n << " -"<< oct << n << endl : cout << n << " " << hex << n << " "<< oct << n << endl; return 0; }
第二周 中級練習
1、加密

#include <iostream> using namespace std; int main() { char a, b, c, d; cin >> a >> b >> c >> d; (a-'a'>10) ? cout << a-'a' : cout << '0' << a-'a'; (b-'a'>10) ? cout << b-'a' : cout << '0' << b-'a'; (c-'a'>10) ? cout << c-'a' : cout << '0' << c-'a'; (d-'a'>10) ? cout << d-'a' : cout << '0' << d-'a'; cout << endl; return 0; }

#include <iostream> using namespace std; void fun(char a) { (a-'a'>10) ? cout << a-'a' : cout << '0' << a-'a'; } int main() { char a, n = 4; while(n--) { cin >> a; fun(a); } cout << endl; return 0; }
2、解密

#include <iostream> using namespace std; int main() { char a[256]; cin >> a; for(int i=0; i<7; i += 2){ cout << char((a[i]-'0')*10 + a[i+1]-'0' + 'a'); } cout << endl; return 0; }
3、壓縮存儲,位移

#include <iostream> using namespace std; int main() { int x = 0, a, b, c, d; cin >> a >> b >> c >> d; x = a << 24; x += b << 16; x += c <<8; x += d; cout << x << endl; return 0; }

#include <iostream> using namespace std; int main() { int a, x = 0, n = 4; while(n--) { cin >> a; x += (a << n*8); } cout << x << endl; return 0; }
4、石頭剪刀布

#include <iostream> using namespace std; int main() { const char* answer[] = {"不認識", "石頭", "剪刀", "布"}; int i; cin >> i; (i >= 1 && i <= 3) ? cout << answer[i] : cout << answer[0]; cout << endl; return 0; }
5、排排坐分果果,格式要求,末尾無空格

#include <iostream> using namespace std; int main() { int n = 4, a, k; cin >> a >> k; while(n--) { ( n == 3 ) ? : cout << " "; (a = (a + k - 1)%10) ? cout << a : cout << "10"; a = (a+1)%10; } cout << endl; return 0; }
第二周 編程作業
1、溫度轉換,小數常量默認是double類型

#include <iostream> using namespace std; int main() { double C, F; cin >> F; C = 5.0/9*(F-32); cout << C << endl; return 0; }
2、計算數學函數式的值

#include <iostream> #include <cmath> using namespace std; int main() { double x, y; cin >> x; y = sin(x*x)/(1-cos(x)); cout << y << endl; return 0; }
3、數據的簡單統計,題目要求,平均值的四舍五入整數值

#include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int sum = a + b + c; cout << sum << endl; cout << sum/3.0 << endl; int ave = sum/3.0 + 0.5; cout << ave << endl; return 0; }
4、找零錢,格式最后沒有空格

#include <iostream> using namespace std; int main() { int x; cin >> x; cout << x/50 << " "; x %= 50; cout << x/20 << " "; x %= 20; cout << x/10 << " "; x %= 10; cout << x/5 << " "; x %= 5; cout << x << endl; return 0; }

#include <iostream> using namespace std; void fun(int x){ int arr[] = {50,20,10,5,1}; for(int i=0; i<4; i++){ cout << x/arr[i] << " "; x %= arr[i]; } cout << x << endl; } int main() { int x; cin >> x; fun(x); return 0; }

#include <iostream> using namespace std; int main() { int x; cin >> x; int arr[] = {50,20,10,5,1}; for(int i=0; i<5; i++){ if(i) cout << " "; cout << x/arr[i]; x %= arr[i]; } return 0; }
5、小寫轉大寫,判斷是否為小寫字母

#include <iostream> using namespace std; int main() { char ch; cin >> ch; if(ch >= 'a' && ch <= 'z') ch -= 32; cout.put(ch) << endl; return 0; }

#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; if(s[0] >= 'a' && s[0] <= 'z') s[0] -= 32; cout << s[0] << endl; return 0; }

#include <iostream> using namespace std; int main() { char s[256]; cin >> s; if(s[0] >= 'a' && s[0] <= 'z') s[0] -= 32; cout << s[0] << endl; return 0; }

#include <iostream> using namespace std; int main() { char ch; cin >> ch; if(ch >= 'a' && ch <= 'z') ch -= 32; cout<< ch << endl; return 0; }
第三周 基礎練習
1、判斷奇偶數

#include <iostream> using namespace std; int main() { int x; cin >> x; if(x%2) cout << "odd" << endl; else cout << "even" << endl; return 0; }
2、判斷數的類型,int(x)==x,判斷是否為整數

#include <iostream> using namespace std; int main() { double x; cin >> x; switch(int(x)==x){ //是否為整數 case 0: if(x>1e-6) cout << "positive real" << endl; else if(x<1e-6) cout << "negative real" << endl; else cout << "zero" << endl; break; case 1: if(int(x)>0) cout << "positive integer" << endl; else if(int(x)<0) cout << "negative integer" << endl; else cout << "zero" << endl; break; } return 0; }
3、判斷點的象限

#include <iostream> using namespace std; int main() { double x, y; cin >> x >> y; if(x>1e-6 && y>1e-6) cout << 1 << endl; else if(x<1e-6 && y>1e-6) cout << 2 << endl; else if(x<1e-6 && y<1e-6) cout << 3 << endl; else cout << 4 << endl; return 0; }
4、判斷字符類型

#include <iostream> using namespace std; int main() { char ch; cin >> ch; if(ch>='0' && ch<='9') cout << 0 << endl; else if(ch>='A' && ch<='Z') cout << 1 << endl; else if(ch>='a' && ch<='z') cout << 2 << endl; else cout << -1 << endl; return 0; }
5、百分制成績轉五分制成績

#include <iostream> using namespace std; int main() { int x; cin >> x; switch(x/10){ case 10: case 9: cout << 5 << endl; break; case 8: cout << 4 << endl; break; case 7: cout << 3 << endl; break; case 6: cout << 2 <<endl; break; case 5:case 4:case 3:case 2:case 1: cout << 1 << endl; break; default: cout << 0 << endl; } return 0; }
6、顯示n個字符

#include <iostream> using namespace std; int main() { int n; char ch; cin >> n >> ch; while(n--) cout << ch; cout << endl; return 0; }
7、顯示字符組成的矩形

#include <iostream> using namespace std; int main() { int n, m; char ch; cin >> m >> n >> ch; for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j) cout << ch; cout << endl; } return 0; }
8、用循環計算1+2+3+...+n

#include <iostream> using namespace std; int main() { int n, s = 0; cin >> n; for(int i=1; i<=n; ++i){ s += i; } cout << s << endl; return 0; }
9、計算1+1/2+1/3+...+1/n

#include <iostream> using namespace std; int main() { int n; double s = 0; cin >> n; for(int i=1; i<=n; ++i){ s += 1.0/i; } cout << s << endl; return 0; }
10、計算n!,long fac = 1;

#include <iostream> using namespace std; int main() { int n; long fac = 1; cin >> n; for(int i=1; i<=n; ++i){ fac *= i; } cout << fac << endl; return 0; }
11、交替輸出1和-1

#include <iostream> using namespace std; int main() { int n, sign = 1, t; cin >> n; t = n; while(n--) { if(n<t-1) cout << " "; cout << sign; sign = -sign; } return 0; }
12、判斷整數的位數,n非負,即可以為0

#include <iostream> using namespace std; int main() { int n, cnt=0; cin >> n; if(!n) cnt++; while(n) { n /= 10; cnt++; } cout << cnt << endl; return 0; }
13、求非負整數的各位數字的和

#include <iostream> using namespace std; int main() { int n, sum=0; cin >> n; while(n) { sum += n%10; n /= 10; } cout << sum << endl; return 0; }
14、九九乘法表,行尾無空格

#include <iostream> using namespace std; int main() { int n; cin >> n; for(int i=1; i<=n; ++i){ for(int j=1; j<=i; ++j){ if(j>1) cout << " "; cout << i << "*" << j << "=" << i*j; } cout << endl; } return 0; }
15、不一樣的九九乘法表

#include <iostream> using namespace std; int main() { int n; cin >> n; for(int i=n; i>=1; --i){ for(int j=1; j<=i; ++j){ if(j>1) cout << " "; cout << i << "*" << j << "=" << i*j; } cout << endl; } return 0; }
16、Fibonacci序列,輸出n+1項

#include <iostream> using namespace std; int main() { int n, f0 = 0, f1 = 1; cin >> n; for(int i=0; i<n+1; i++){ if(i) cout << " "; cout << f0; int sum = f0 + f1; f0 = f1; f1 = sum; } return 0; }
第三周 中級練習
1、計算1!+2!+3!+…+n!

#include <iostream> using namespace std; int main() { int n, t = 1, result = 0; cin >> n; for(int i=1; i<=n; ++i){ t *= i; result += t; } cout << result << endl; return 0; }
2、a+aa+aaa

#include <iostream> using namespace std; int main() { int a, n, t = 0, result = 0; cin >> a >> n; for(int i=0; i<n; ++i){ t = t*10 + a; //保存中間結果 result += t; } cout << result << endl; return 0; }
3、arcsin(x)

#include <iostream> #include <cmath> using namespace std; int main() { double x; cin >> x; int n = 0; //第0項 double u = x, arcsin = 0; while(fabs(u)>=1e-8){ arcsin += u; //累加 n++; //從第一項開始計算u u = u*(2*n-1)*(2*n-1)*x*x/2/n/(2*n+1); } cout << arcsin << endl; return 0; }

#include <iostream> #include <cmath> using namespace std; int main() { double x; cin >> x; cout << asin(x) << endl; return 0; }
4、回文數

#include <iostream> using namespace std; int main() { int n, t, sum = 0; cin >> n; t = n; while(t) { sum = sum*10 + t%10; t /= 10; } if(sum==n) cout << "YES" << endl; else cout << "NO" << endl; return 0; }

#include <iostream> #include <string.h> using namespace std; int main() { char str[256], *p, *q; cin >> str; p = str, q = str + strlen(str)-1; while(*p==*q && p<q){ p++,q--; } if(p>=q) cout << "YES" << endl; else cout << "NO" << endl; return 0; }

#include <iostream> #include <string.h> using namespace std; int main() { char str[256], i, j; cin >> str; for(i=0, j=strlen(str)-1; str[i] == str[j] && i<j; ++i, --j); if(i>=j) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
5、整數的素數因子分解

#include <iostream> using namespace std; int main() { int i=2, n; cin >> n; cout << n << "="; while(n>1) { if(n%i==0){ cout << i; n /= i; if(n>1) cout << "*"; /* */ } else{ i++; } } return 0; }

#include <iostream> using namespace std; int main() { int n; cin >> n; cout << n << "="; for(int i=2; n>1; ++i) { if(n%i==0){ cout << i; n /= i; --i; if(n>1) cout << "*"; } } return 0; }
第三周 編程作業
1、3-1打印3個相鄰字母

#include <iostream> using namespace std; int main() { char ch,ch1 = 'A'; cin >> ch; if(ch>='a' && ch<='z') ch1 = 'a'; cout << char((ch-ch1-1+26)%26+ch1) << ch << char((ch-ch1+1)%26+ch1) << endl; return 0; }
2、3-2歌唱大賽選手成績計算

#include <iostream> using namespace std; int main() { int score, min = 100, max = 0, sum = 0; int n = 10; while(n--) { cin >> score; if(score<0 || score>100){ cout << "the score is invalid." << endl; break; } if(score>max) max = score; if(score<min) min = score; sum += score; } if(n==-1) cout << (sum - max - min)/8.0 << endl; return 0; }
3、3-3猴子吃桃

#include <iostream> using namespace std; int main() { int days,x=1; cin >> days; while(--days){ x = 2*(x+1); } cout << x << endl; return 0; }
4、3-4搬磚問題

#include <iostream> using namespace std; int main() { int n, flag = 0; cin >> n; for(int men=0; men<=n/4; ++men) { for(int women=0; women<=(n-men)/3; ++women) { int children = n - men - women; //if(men*4*2+women*3*2+children==2*n){ if(children%2==0 && men*4+women*3+children/2==n){ cout << "men" << men << endl; cout << "women" << women << endl; cout << "children" << children << endl; flag = 1; } } } if(!flag) cout << "no result!" << endl; return 0; }
5、3-5美分找錢

#include <iostream> using namespace std; int arr[] = {25, 10, 5}; int func(int left, int index) { if(left==0 || index>=3) return 1; int count = 0; int x = left/arr[index]; for (int i=x; i>=0; i--) { count += func(left - i*arr[index], index+1); } return count; } int main() { unsigned int n, count = 0; cin >> n; if (n<0||n>99) cout << "the money is invalid!" << endl; else cout << func(n, 0) << endl; return 0; }

#include <iostream> using namespace std; int MakeChangeCore(int n,int denom) { int next_denom=0; switch(denom) { case 25: next_denom=10; break; case 10: next_denom=5; break; case 5: next_denom=1; break; case 1: return 1; } int res=0; for(int i=0;i*denom<=n;i++) res+=MakeChangeCore(n-i*denom,next_denom); return res; } int MakeChange(int n) { if(n<=0) return -1; return MakeChangeCore(n,25); } int main() { unsigned int n, count = 0; cin >> n; if (n<0||n>99) cout << "the money is invalid!" << endl; else cout << MakeChange(n) << endl; return 0; }

#include <iostream> using namespace std; int main() { unsigned int n, count = 0; cin >> n; if (n<0||n>99) cout << "the money is invalid!" << endl; else{ for (int a=n/25; a>=0; a--) //25美元 { int ten = n-25*a; //10美元 for (int b=ten/10; b>=0; b--) { int five = ten - 10*b; //5美元 for (int c=five/5; c>=0; c--) { //(n-25*a-10*b-5*c) 1美元 /* if (25*a+10*b+5*c+(n-25*a-10*b-5*c) == n) { count++; } */ count++; } } } cout << count << endl; } return 0; }
第4周 基礎練習
1、數組元素反序輸出

#include <iostream> using namespace std; int main( ) { int n; cin >> n; int *arr = new int[n]; for(int i=0; i<n; ++i) cin >> arr[i]; cout << arr[n-1]; for(int j=n-2; j>=0; --j) cout << " " << arr[j]; return 0; }
2、求數組元素最大值

#include <iostream> #include <climits> using namespace std; int main( ) { int arr[100], index = 0; //其實數組無實質意義 int max = INT_MIN; int x; for(; cin >> x, x != -9999; ) { arr[index++] = x; if(arr[index-1] > max ) max = arr[index-1]; } cout << max << endl; return 0; }

#include <iostream> #include <climits> using namespace std; int main( ) { int max = INT_MIN; int x; for(; cin >> x, x != -9999; ) { if(x > max ) max = x; } cout << max << endl; return 0; }
3、數組指定區間的元素的最大、最小、總和和平均值,總和及平均值均為double,總和若為整形,求平均值要強轉

#include <iostream> #include <climits> using namespace std; int main( ) { int arr[] = {-1,15,-40,-180,99,-122,-124,27,192,128, -165,95,161,-138, -183,51,107,39,-184,113, -63,9,107,188,-11,-13,151,-52,7,6}; int i, j, max = INT_MIN, min = INT_MAX; double sum = 0, ave = 0; cin >> i >> j; if(i<j && i>=0 && j<=30) { for(int k = i; k<j; ++k) { sum += arr[k]; if(arr[k] > max) max = arr[k] ; if(arr[k] < min) min = arr[k]; } cout << max << " " << min << " " << sum << " " << sum/(j-i) << endl; } else cout << "0 0 0 0" << endl; return 0; }

#include <iostream> using namespace std; int main( ) { int arr[] = {-1,15,-40,-180,99,-122,-124,27,192,128, -165,95,161,-138, -183,51,107,39,-184,113, -63,9,107,188,-11,-13,151,-52,7,6}; int i, j, max, min; double sum = 0, ave = 0; cin >> i >> j; if(i<j && i>=0 && j<=30) // i,j條件 { max = arr[i], min = arr[i]; //賦初始值 for(int k = i; k<j; ++k) //從i開始累加 { sum += arr[k]; if(arr[k] > max) max = arr[k] ; if(arr[k] < min) min = arr[k]; } cout << max << " " << min << " " << sum << " " << sum/(j-i) << endl; } else cout << "0 0 0 0" << endl; return 0; }
4、求矩陣每行元素最大值

#include <iostream> using namespace std; int main( ) { //int arr[20][20]; //省略 int row, col, x; cin >> row >> col; for( int i= 0; i<row; i++){ cin >> x; int max = x; for(int j=1; j<col; ++j) { cin >> x; if(x>max) max = x; } cout << max << endl; } return 0; }

#include <iostream> using namespace std; int main( ) { int arr[20][20]; int row, col; cin >> row >> col; for( int i= 0; i<row; i++){ cin >> arr[i][0]; int max = arr[i][0]; for(int j=1; j<col; ++j) { cin >> arr[i][j]; if(arr[i][j]>max) max = arr[i][j]; } cout << max << endl; } return 0; }
5、求矩陣每列元素最大值,int max = arr[0][i];

#include <iostream> using namespace std; int main( ) { int arr[20][20]; int row, col, i, j; cin >> row >> col; for( i= 0; i<row; i++){ for( j=0; j<col; ++j) { cin >> arr[i][j]; } } for( i= 0; i<col; i++){ int max = arr[0][i]; for(j=1; j<row; ++j) { if(max<arr[j][i]) max = arr[j][i]; } if(i) cout << " "; cout << max; } return 0; }
6、計算向量的和

#include <iostream> using namespace std; int main( ) { int n; int arr1[100],arr2[100]; cin >> n; for(int i=0; i<n; ++i) cin >> arr1[i]; for(int i=0; i<n; ++i) cin >> arr2[i]; for(int i=0; i<n; ++i){ if(i) cout << " "; cout << arr1[i] + arr2[i]; } return 0; }
7、矩陣向量的內積

#include <iostream> using namespace std; int main( ) { int n, sum = 0; int arr1[100],arr2[100]; cin >> n; for(int i=0; i<n; ++i) cin >> arr1[i]; for(int i=0; i<n; ++i) cin >> arr2[i]; for(int i=0; i<n; ++i){ sum += arr1[i] * arr2[i]; } cout << sum << endl; return 0; }
8、計算向量的范數

#include <iostream> #include <cmath> using namespace std; int main( ) { int n; double arr[100],sum = 0; cin >> n; for(int i=0; i<n; ++i){ cin >> arr[i]; sum += arr[i]*arr[i]; } cout << sqrt(sum) << endl; return 0; }

#include <iostream> #include <cmath> using namespace std; int main( ) { int n; double x, sum = 0; cin >> n; for(int i=0; i<n; ++i){ cin >> x; sum += x * x; } cout << sqrt(sum) << endl; return 0; }
9、計算向量的歐氏距離,double類型

#include <iostream> #include <cmath> using namespace std; int main( ) { int n; double sum = 0; double arr1[100],arr2[100]; cin >> n; for(int i=0; i<n; ++i) cin >> arr1[i]; for(int i=0; i<n; ++i) cin >> arr2[i]; for(int i=0; i<n; ++i){ sum += (arr1[i] - arr2[i]) * (arr1[i] - arr2[i]); } cout << sqrt(sum) << endl; return 0; }
10、矩陣求和

#include <iostream> #include <cmath> using namespace std; int main( ) { int n, m; double arr1[100][100],arr2[100][100],sum[100][100]; cin >> n >> m; for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) cin >> arr1[i][j]; } for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) cin >> arr2[i][j]; } for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j){ sum[i][j] = arr1[i][j] + arr2[i][j]; if(j) cout << " "; cout << sum[i][j]; } cout << endl; } return 0; }

#include <iostream> using namespace std; int main( ) { int n, m; double arr1[100][100], arr2[100][100]; cin >> n >> m; for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) cin >> arr1[i][j]; } for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) cin >> arr2[i][j]; } for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j){ if(j) cout << " "; cout << arr1[i][j] + arr2[i][j]; } cout << endl; } return 0; }
11、輸入字符串,求長度

#include <iostream> using namespace std; int main( ) { char str[100]; cin.getline(str,100); char *p = str; int cnt = 0; while(*p) { cnt++; p++; } cout << cnt << endl; return 0; }
12、統計字符串中大寫字母的數量

#include <iostream> using namespace std; int main( ) { char str[100]; cin.getline(str,100); char *p = str; int cnt = 0; while(*p) { if(*p>='A' && *p<='Z') cnt++; p++; } cout << cnt << endl; return 0; }
13、復制字符串

#include <iostream> using namespace std; int main( ) { char s1[100], s2[100]; cin >> s1; char* p1 = s1, *p2 = s2; while( *p2++ = *p1++ ); cout << s2 << endl; return 0; }
14、字符串逆序

#include <iostream> using namespace std; int main( ) { char s[100]; cin >> s; int index = 0, i,j; while(s[index++]); for(i=0, j = index - 2; i<j; ++i,--j) { char t = s[i]; s[i] = s[j]; s[j] = t; } cout << s << endl; return 0; }
15、定義表示平面坐標點的結構體計算兩點距離

#include <iostream> #include <cmath> using namespace std; struct POINT{ double x, y; }; int main( ) { POINT p1, p2; cin >> p1.x >> p1.y >> p2.x >> p2.y; cout << sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)) << endl; return 0; }
第4周 中級練習
1、矩陣原地轉置,for(j=0; j<i; ++j)

#include <iostream> using namespace std; int main( ) { int arr[10][10], i, j; int n; cin >> n; for(i=0; i<n; ++i){ for(int j=0; j<n; ++j){ cin >> arr[i][j]; } } //轉置 for(i=0; i<n; ++i){ for(j=0; j<i; ++j){ if(i!=j){ int t = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = t; } } } for(i=0; i<n; ++i){ for( j=0; j<n; ++j){ if(j) cout << " "; cout << arr[i][j]; } cout << endl; } return 0; }
2、判斷對稱矩陣,flag = 1;

#include <iostream> using namespace std; int main( ) { int arr[10][10], i, j; int n, flag = 0; cin >> n; for(i=0; i<n; ++i){ for(j=0; j<n; ++j){ cin >> arr[i][j]; } } //判斷對稱 for(i=0; i<n; ++i){ for(j=0; j<i; ++j){ if(arr[i][j] != arr[j][i]){ flag = 1; break; } } } if(flag) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
3、去掉字符串末尾的空格

#include <iostream> using namespace std; int main( ) { char str[100]; cin.get(str,100); cout << "|" << str << "|" << endl; char *p = str; while( *p ) p++; while( *--p == ' ' ); *(p+1) = '\0'; cout << "|" << str << "|" << endl; return 0; }
4、去掉字符串開頭的空格符

#include <iostream> using namespace std; int main( ) { char str[100]; cin.get(str,100); cout << "|" << str << "|" << endl; char *p = str, *q = str; while( *p == ' ') p++; while( *q++ = *p++); cout << "|" << str << "|" << endl; return 0; }
5、去掉字符串中間的所有空格,兩頭的空格保留,從后向前可能會覆蓋(計算后頭空格個數)

#include <iostream> using namespace std; int main( ) { char str[100]; cin.get(str,100); cout << "|" << str << "|" << endl; //保留字符串前空格 char *p = str, *q; while(*p == ' ') p++; q = p; //去掉字符串中間的空格 while( *p ) { if( *p != ' ' ) *q++ = *p; p++; } //計算字符串后的空格 int cnt = 0; while(*--p == ' ') cnt++; while(cnt--) *q++ = ' '; *q = '\0'; cout << "|" << str << "|" << endl; return 0; }
6、查找子串

#include <iostream> using namespace std; int main( ) { char s1[100], s2[100]; char *p, *q; gets(s1); gets(s2); /*cin.getline(s1,100); cin.getline(s2,100);*/ p = s1, q = s2; /* p,q 源串、目標串比較開始位置 */ int cnt = 1; while(*p&&*q) { if (*p==*q) { p++; q++; } else { p = s1 + cnt++; q = s2; } } if (*q=='\0') /* 匹配 */ cout << cnt << endl; else cout << "沒有該子串" << endl; return 0; }
7、排序,插入排序

#include <iostream> using namespace std; int main( ) { int arr[100]; int n, i, j; cin >> n; for(i=0; i<n; ++i) cin >> arr[i]; for (j=1; j<n; j++ ) { int t = arr[j]; for ( i=j; i>0 && arr[i-1]<t; i-- ) arr[i] = arr[i-1]; /*依次與已排序序列中元素比較並右移*/ arr[i] = t; /* 放進合適的位置 */ } for(i=0; i<n; ++i){ if(i) cout << " "; cout << arr[i]; } return 0; }
8、二分查找,找到,break;

#include <iostream> using namespace std; int main( ) { int arr[300], index = 0; int x1, x2; for(; cin >> x1, x1 != -99999; ) arr[index++] = x1; cin >> x2; int left = 0, right = index-1; while(left<=right) { int mid = (right-left)/2 + left; //防溢出 if(arr[mid]>x2) right = mid - 1; else if(arr[mid]<x2) left = mid + 1; else{ cout << mid << endl; break; } } if(left>right) cout << -1 << endl; return 0; }
第4周 編程作業
1、愷撒加密

#include <iostream> using namespace std; int main( ) { char str[256]; cin >> str; char *p = str; while(*p) { char ch = 'a'; char num = -32; if(*p>='A' && *p<='Z') ch = 'A', num = 32; *p = (*p - ch + 3)%26 + ch + num; p++; } cout << str << endl; return 0; }
2、矩陣轉置

#include <iostream> using namespace std; int main( ) { int n, arr[6][6]; cin >> n; if(n<1 || n > 5) cout << "matrix order error" << endl; else{ for(int i=0; i<n; ++i){ for(int j=0; j<n; ++j){ cin >> arr[j][i]; } } for(int i=0; i<n; ++i){ for(int j=0; j<n; ++j){ if(j) cout << " "; cout << arr[i][j]; } cout << endl; } } return 0; }
3、按點擊率顯示歌曲

#include <iostream> using namespace std; struct SONG{ char title[50]; char name[20]; int point; }; int main( ) { SONG songs[5]; int i, j; for(i=0; i<5; ++i){ cin >> songs[i].title >> songs[i].name >> songs[i].point; } /* 插入排序 */ for (j=1; j<5; j++ ) { SONG t = songs[j]; /* 取出未排序序列中的第一個元素*/ for ( i=j; i>0 && songs[i-1].point<t.point; i-- ) songs[i] = songs[i-1]; /*依次與已排序序列中元素比較並右移*/ songs[i] = t; /* 放進合適的位置 */ } for(i=0; i<5; ++i){ cout << songs[i].title << " " << songs[i].name << " "; cout << songs[i].point << endl; } return 0; }
4、星期轉換

#include <iostream> using namespace std; int main( ) { const char *weeks[] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"}; int n; cin >> n; if(n<1||n>7) cout << "invalid" << endl; else cout << weeks[n%7] << endl; return 0; }
5、插入加密,明文最后一個字符后也要打印插入字符

#include <iostream> using namespace std; int main( ) { char str[30], letter[6] = "abcde"; int n, index = 0; cin >> str >> n; char *p = str; while(*p){ int t = n; while(*p && t--){ putchar(*p++); } putchar(letter[index++]); index %= 5; } return 0; }

#include <iostream> using namespace std; int main( ) { char str[30], letter[6] = "abcde"; int n, index = 0; cin >> str >> n; char *p = str; for(int t = n; *p; index %= 5){ //單循環 if(t--) putchar(*p++); //間隔后打印插入字符並恢復間隔, 明文最后一個字符后也要打印插入字符 if(!*p || !t){ putchar(letter[index++]); t = n; } } return 0; }

#include <iostream> using namespace std; int main() { string s; string word[5] = {"a","b","c","d","e"}; int step, index = 0; getline( cin, s ); cin >> step; for(decltype(s.size())i=step; i<s.size(); i+=(step+1),index%=5){ s.insert(i,word[index++]); } s += word[index]; cout << s << endl; return 0; }

#include <iostream> using namespace std; int main() { string s; string word = "abcde"; int step, n = 1, index = 0; //n插入的字符個數 cin >> s >> step; for(int i=step; i<s.size(); i+=(n+step),index%=5){ s.insert(i,n,word[index++]); } s.insert(s.size(),n,word[index]);//字符串尾部要求插入字符 cout << s << endl; return 0; }
6、三色球,枚舉,j = i+1;

#include <iostream> using namespace std; int main( ) { enum color {red, yellow, blue}; int tmp, i, j, t; for(i=red; i<=yellow; ++i){ for(j=i+1; j<=blue; ++j){ //j=i+1 for(t=0; t<2; ++t){ //二種選擇或i或j switch(t){ case 0: tmp = i; break; //對應枚舉 case 1: tmp = j; break; } switch((enum color)tmp){ //強轉枚舉 case red: cout << "red "; break; case yellow: cout << "yellow "; break; case blue: cout << "blue "; break; } } cout << endl; } } return 0; }
第5周 基礎練習
1、求兩個數的和

#include <iostream> using namespace std; double mysum(double a, double b){ return a + b; } int main() { double a, b; cin >> a >> b; cout << mysum(a,b) << endl; return 0; }
2、求絕對值的函數,double的最高位是符號位,位運算 &0x7fffffff 可以改變最高位的值

#include <iostream> using namespace std; double myfabs(double x){ *((int*)&x + 1) &= 0x7fffffff; return x; } int main() { double x; cin >> x; cout << myfabs(x) << endl; return 0; }
3、x的k次方

#include <iostream> using namespace std; double myfabs(double x){ *((int*)&x + 1) &= 0x7fffffff; return x; } double mypow(double x, int n){ double ret = 1; if( myfabs(x) <= 1e-7 ) ret = 0.0; else if(!n) ret = 1.0; else if( n<0 ){ n = -n; for(int i=0; i<n; ++i) ret /= x; } else if(n>0){ for(int i=0; i<n; ++i) ret *= x; } return ret; } int main() { double x; int n; cin >> x >> n; cout << mypow(x, n) << endl; return 0; }
4、求n!的函數

#include <iostream> using namespace std; long fact(int n){ long ret = 1; for(int i=1; i<=n; ++i) ret *= i; return ret; } int main() { int n; cin >> n; cout << fact( n ) << endl; return 0; }
5、輸入數組元素

#include <iostream> using namespace std; int input(int *arr){ int index = 0, x; for(; cin >> x, x != -9999; ) arr[index++] = x; return index; } int main() { int arr[100]; int n = input(arr); for(int i= n-1; i>=0; --i) { if(i != n-1) cout << " "; cout << arr[i]; } return 0; }
6、輸出數組元素值

#include <iostream> using namespace std; int input(int *arr){ int index = 0, x; for(; cin >> x, x != -9999; ) arr[index++] = x; return index; } void output(int *arr, int n){ for(int i= 0; i<n; ++i) { if(i) cout << " "; cout << arr[i]; } } int main() { int arr[100]; int n = input(arr); output(arr, n); return 0; }
7、將數組元素逆序

#include <iostream> using namespace std; int input(int *arr){ int index = 0, x; for(; cin >> x, x != -9999; ) arr[index++] = x; return index; } void output(int *arr, int n){ for(int i= 0; i<n; ++i) { if(i) cout << " "; cout << arr[i]; } } void reserve(int *arr, int n){ for(int i=0, j=n-1; i<j; ++i, --j){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } } int main() { int arr[100]; int n = input(arr); reserve( arr, n); output(arr, n); return 0; }
8、求數組元素的和

#include <iostream> using namespace std; int input(int *arr){ int index = 0, x; for(; cin >> x, x != -9999; ) arr[index++] = x; return index; } int addArr(int *arr, int n){ int sum = 0; for(int i=0; i<n; ++i) sum += arr[i]; return sum; } int main() { int arr[100]; int n = input(arr); cout << addArr(arr, n) << endl; return 0; }
9、求字符串的長度的函數

#include <iostream> using namespace std; int stringLength(char *p){ int i = 0; while(p[i]) i++; return i; } int main() { char str[100]; cin >> str; cout << stringLength(str) << endl; return 0; }
10、字符串轉大寫

#include <iostream> using namespace std; void upper(char *p){ while(*p){ if(*p>='a'&&*p<='z') *p -= 32; p++; } } int main() { char str[200]; cin >> str; upper(str); cout << str << endl; return 0; }
11、字符串復制函數

#include <iostream> using namespace std; void mystrcpy(char s1[],char s2[]); int main() { char s1[100], s2[100]; cin.getline(s1, 100); mystrcpy(s2,s1); cout << s2 << endl; return 0; } void mystrcpy(char s1[],char s2[]){ while(*s1++ = *s2++); }
12、字符串比較函數

#include <iostream> using namespace std; int compare(char*, char*); int main() { char s1[200], s2[200]; cin >> s1 >> s2; cout << compare(s1,s2) << endl; return 0; } int compare(char *s1, char *s2) { int i, ret = 0; while( ! (ret = *( unsigned char *)s1 - *(unsigned char *)s2) && *s2) s1++, s2++; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return ret; }
第5周 中級練習
1、比較字符串(不區分大小寫),比較不應修改字符串,可以用2個變量對兩個字符串的字母取值,轉為小寫,比較

#include <iostream> using namespace std; int compare(const char*, const char*); int main() { char str1[100], str2[100]; cin >> str1 >> str2; cout << compare(str1,str2) << endl; return 0; } int compare(const char *dst, const char *src) { int ch1, ch2, ret = 0; do { if ( ((ch1 = (unsigned char)(*(dst++))) >= 'A') &&(ch1 <= 'Z') ) ch1 += 0x20; if ( ((ch2 = (unsigned char)(*(src++))) >= 'A') &&(ch2 <= 'Z') ) ch2 += 0x20; } while ( ch1 && (ch1 == ch2) ); if ( ch1-ch2 < 0) ret = -1 ; else if ( ch1-ch2 > 0 ) ret = 1 ; return ret; }
2、二分法解方程

#include <iostream> #include <cmath> using namespace std; double fun(double x){ return 2*x*x*x - 4*x*x + 3*x - 6; } int main() { double left, right, middle, eps; cin >> left >> right >> eps; do { middle = (left+right)/2; if(fun(middle)*fun(left)>0) left = middle; else right = middle; } while(fabs(fun(middle))>=eps); cout << middle << endl; return 0; }
3、牛頓法解方程,求導,3*a*x*x + 2*b*x + c

#include <iostream> using namespace std; double fun(double x){ return x*x*x + 2*x*x + 3*x + 4;//系數a,b,c,d } double fun1(double x){ return 3*x*x + 4*x + 3; //3*a*x*x + 2*b*x + c } double fabs(double x){ *(((int *) &x) + 1) &= 0x7fffffff; //最高位, 置0 return x; } int main() { double x0, eps; cin >> x0 >> eps; while(fabs(fun(x0))>eps) x0 -= fun(x0)/fun1(x0); cout << x0 << endl; return 0; }
4、輸入、排序、查找

#include <iostream> using namespace std; int input(int *arr){ //輸入 int x, index = 0; for( ;cin >> x, x != -9999; ) arr[index++] = x; return index; } void insertSort(int *arr, int n){//插入排序 int i, j, t; for (j=1; j<n; j++ ) { t = arr[j]; for ( i=j; i>0 && arr[i-1]>t; i-- ) arr[i] = arr[i-1]; arr[i] = t; } } int findX(int *arr, int n, int x){//二分查找 int left = 0, right = n-1, mid = -1; while(left<=right) { mid = (right-left)/2 + left; //防溢出 if(arr[mid]>x) right = mid - 1; else if(arr[mid]<x) left = mid + 1; else break; } if(left>right) mid = -1; return mid; } void find5X(int *arr, int n){ //查找5個元素 int x; for(int i=0; i<5; ++i) { cin >> x; cout << findX(arr, n, x) << endl; } } int main() { int arr[100], n; n = input( arr ); insertSort( arr, n ); find5X( arr, n ); return 0; }
5、單詞排序,用不到復制、大小寫轉換函數

#include <iostream> using namespace std; void insertSort(char **arr, int n); int compare(const char *dst, const char *src); int main() { char **str = new char*[100]; for(int i=0; i<100; ++i) str[i] = new char[20]; int n; cin >> n; for(int i=0; i<n; ++i){ cin >> str[i]; } insertSort( str, n ); for(int j=0; j<n; ++j) cout << str[j] << endl; return 0; } void insertSort(char **arr, int n){//插入排序 int i, j, t; for (j=1; j<n; j++ ) { char *t = arr[j]; for ( i=j; i>0 && 1 == compare(arr[i-1],t); i-- ) arr[i] = arr[i-1]; arr[i] = t; } } int compare(const char *dst, const char *src) { int ch1, ch2, ret = 0; do { if ( ((ch1 = (unsigned char)(*(dst++))) >= 'A') &&(ch1 <= 'Z') ) ch1 += 0x20; if ( ((ch2 = (unsigned char)(*(src++))) >= 'A') &&(ch2 <= 'Z') ) ch2 += 0x20; } while ( ch1 && (ch1 == ch2) ); if ( ch1-ch2 < 0) ret = -1 ; else if ( ch1-ch2 > 0 ) ret = 1 ; return ret; }
6、識別數字,數字逐字累加后乘以符號存儲數字;還可以數字累加后存儲數字,符號也可以存儲

#include <iostream> using namespace std; void solution(char *p) { int i, arr[256], index = 0; int flag = 0, sum = 0, sign = 1; for(i=0; p[i]; ++i) { if( p[i]>='0' && p[i]<='9' ) { sum = sum*10 + p[i] - '0'; if( i && p[i-1] == '-' ) sign = -1; flag = 1; continue; } if(flag) { arr[index++] = sum*sign; sign = 1; //符號置 1 sum = 0; //sum置 0 flag = 0; //數字標記置 0 } } if(flag){ //字符串最后為數字 arr[index++] = sum*sign; } for(i=0; i<index; ++i) { if(i) cout << " "; cout << arr[i]; } } int main() { char str[256]; cin.getline(str,256); solution(str); return 0; }

#include <iostream> using namespace std; typedef struct { short tag;//判斷類型 union { int num; char ch; }u; //聯合體 }SignedNumber; void solution(char *p) { SignedNumber arr[128]; //結構體數組 int index = 0; int flag = 0, sum = 0; for( ; *p; p++) { //與數字相關 if( *p>='0' && *p<='9' ) { sum = sum*10 + *p - '0'; flag = 1; continue; } if(flag) { arr[index].u.num = sum; arr[index++].tag = 0; sum = 0; flag = 0; } //符號相關 if(*p=='-' && *(p+1) && *(p+1)>='0'&&*(p+1)<='9'){ arr[index].u.ch = *p; arr[index++].tag = 1; } } if(flag){ //字符串最后是數字 arr[index].u.num = sum; arr[index++].tag = 0; } for(int i=0; i<index; ++i) { if(i && !arr[i-1].tag) //前面的元素是數字打印空格 cout << " "; if(!arr[i].tag){ cout << arr[i].u.num; } else if(arr[i+1].u.num){//0前不打印符號 cout << arr[i].u.ch; } } } int main() { char str[256]; cin.getline(str,256); solution(str); return 0; }
第5周 編程作業
1、編寫字符串反轉函數mystrrev

#include <iostream> using namespace std; void mystrrev(char str[]); int main() { char s[100]; cin.getline(s, 100); mystrrev(s); cout << s << endl; return 0; } void mystrrev(char str[]) { int i, j, len = 0; while(str[len]) len++; for(i=0, j=len-1; i<j; ++i, --j) { char t = str[i]; str[i] = str[j]; str[j] = t; } }
2、編寫一組求數組中最大最小元素的函數

#include <iostream> using namespace std; int imax(int array[], int count); int imin(int array[], int count); int main() { int arr[100]; int n; cin >> n; for(int i=0; i<n; ++i) cin >> arr[i]; cout << imax(arr, n) << endl; cout << imin(arr, n) << endl; return 0; } int imax(int array[], int count){ int max = array[0]; for(int i=1; i<count; ++i) if(max<array[i]) max = array[i]; return max; } int imin(int array[], int count){ int min = array[0]; for(int i=1; i<count; ++i) if(min>array[i]) min = array[i]; return min; }
3、編寫函數判斷一個整數是否為素數,高效的算法是用一個數組存儲素數,被素數整除的不是素數

#include <iostream> using namespace std; int isprime(int a); int main() { int a, flag = 0; for(; cin >> a, a; ){ if(isprime(a)){ if(flag) cout << " "; cout << a; flag = 1; } } return 0; } int isprime(int a){ int ret = 1; if(a==1) ret = 0; for(int i=2; i<=a/i; ++i){ if(a%i==0){ ret = 0; break; } } return ret; }
4、編寫函數去除字符串中包含的非字母字符(不包括空格),並將小寫字母轉換成大寫字母

#include <iostream> using namespace std; void upperStr(char*); int main() { char str[200]; cin.getline(str, 200); upperStr(str); cout << str << endl; return 0; } void upperStr(char* str){ char *p = str; while(*p){ if(*p>='a'&&*p<='z') *str++ = *p - 32; else if(*p>='A'&&*p<='Z'||*p == ' ') *str++ = *p; p++; } *str = *p; }
5、編寫函數計算一個英文句子中的單詞個數,忽略非字母,發現字母計數,忽略字母,重復這個過程;一層循環,二個 if

#include <iostream> using namespace std; int countWords(char*); int main() { char str[500]; cin.getline(str, 500); cout << countWords(str) << endl; return 0; } int countWords(char* p){ int count = 0, flag = 1;//如果只一個字母 for( ; *p; p++){ if(!(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')){ flag = 1; //之后是字母 continue; //沒有字母 } if(flag){ count++; flag = 0; } } return count; }
第6周 基礎練習
1、遞歸計算n!

#include <iostream> using namespace std; int fact( int n){ if(n==1||n==0) return 1; return n*fact(n-1); } int main() { int n; cin >> n; cout << fact( n ) << endl; return 0; }
2、遞歸計算1+2+3+…+n

#include <iostream> using namespace std; int sum( int n){ if(n==0) //n 為 0 和為 0 return 0; if(n==1) return 1; return n+sum(n-1); } int main() { int n; cin >> n; cout << sum( n ) << endl; return 0; }
3、遞歸求數組元素的最大值

#include <iostream> using namespace std; int arrMax( int *arr, int n){ int max; if(n==1) return arr[0]; max = arrMax(arr, n-1); if(arr[n-1]>max) max = arr[n-1]; return max; } int main() { int n; int arr[100]; cin >> n; for(int i=0; i<n; ++i) cin >> arr[i]; cout << arrMax( arr, n ) << endl; return 0; }
4、遞歸求數組元素的和

#include <iostream> using namespace std; int arrSum( int *arr, int n){ int sum; if(n==0) return 0; //n為0時返回0 if(n==1) return arr[0]; sum = arrSum(arr, n-1); sum += arr[n-1]; return sum; } int main() { int n; int arr[100]; cin >> n; for(int i=0; i<n; ++i) cin >> arr[i]; cout << arrSum( arr, n ) << endl; return 0; }
5、遞歸求Fibonacci序列的第n項

#include <iostream> using namespace std; int Fibonacci( int n){ if(n==0) return 0; if(n==1) return 1; return Fibonacci(n-1) + Fibonacci(n-2); } int main() { int n; cin >> n; cout << Fibonacci( n ) << endl; return 0; }
6、遞歸逆序數組元素

#include <iostream> using namespace std; void reverse(int *arr, int i, int j){ if( i < j ){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t; reverse(arr, i+1, j-1); } } int main() { int n; int arr[100]; cin >> n; for(int i=0; i<n; ++i) cin >> arr[i]; reverse(arr,0,n-1); for(int i=0; i<n; ++i){ if(i) cout << " "; cout << arr[i]; } cout << endl; return 0; }
7、輸入輸出數組元素的函數重載

#include <iostream> #include <cmath> using namespace std; int input( int *ai ); int input( double *ad ); void print( int *ai, int ni ); void print( double *ad, int nd ); int main() { int ai[100]; double ad[100]; int ni, nd; ni = input( ai ); nd = input( ad ); print( ai, ni ); print( ad, nd ); return 0; } int input( int *ai ){ int i = 0, x; for(; cin >> x, x != -9999;) ai[i++] = x; return i; } int input( double *ad ){ int i = 0; double x; for(; cin >> x, fabs(x - (-9999))>1e-7; ) ad[i++] = x; return i; } void print( int *ai, int ni ){ for(int i=0; i<ni; ++i) { if(i) cout << " "; cout << ai[i]; } cout << endl; } void print( double *ad, int nd ){ for(int i=0; i<nd; ++i){ if(i) cout << " "; cout << ad[i]; } cout << endl; }
8、逆序函數重載

#include <iostream> #include <cmath> using namespace std; int input( int *ai ); int input( double *ad ); void print( int *ai, int ni ); void print( double *ad, int nd ); void reverse( int *ai, int ni ); void reverse( double *ad, int nd ); int main() { int ai[100]; double ad[100]; int ni, nd; ni = input( ai ); nd = input( ad ); reverse( ai, ni ); reverse( ad, nd ); print( ai, ni ); print( ad, nd ); return 0; } int input( int *ai ){ int i = 0, x; for(; cin >> x, x != -9999;) ai[i++] = x; return i; } int input( double *ad ){ int i = 0; double x; for(; cin >> x, fabs(x - (-9999))>1e-7; ) ad[i++] = x; return i; } void print( int *ai, int ni ){ for(int i=0; i<ni; ++i) { if(i) cout << " "; cout << ai[i]; } cout << endl; } void print( double *ad, int nd ){ for(int i=0; i<nd; ++i){ if(i) cout << " "; cout << ad[i]; } cout << endl; } void reverse( int *ai, int ni ){ int i, j; for(i=0, j = ni-1; i<j; ++i, --j){ int t = ai[i]; ai[i] = ai[j]; ai[j] = t; } } void reverse( double *ad, int nd ){ int i, j; for(i=0, j = nd-1; i<j; ++i, --j){ double t = ad[i]; ad[i] = ad[j]; ad[j] = t; } }
9、數組元素求和函數的重載

#include <iostream> #include <cmath> using namespace std; int input( int *ai ); int input( double *ad ); int mysum( int *ai, int ni ); double mysum(double *ad, int nd ); int main() { int ai[100]; double ad[100]; int sumi; double sumd; int ni, nd; ni = input( ai ); nd = input( ad ); sumi = mysum( ai, ni ); sumd = mysum(ad, nd ); cout << sumi << " " << sumd << endl; return 0; } int input( int *ai ){ int i = 0, x; for(; cin >> x, x != -9999;) ai[i++] = x; return i; } int input( double *ad ){ int i = 0; double x; for(; cin >> x, fabs(x - (-9999))>1e-7; ) ad[i++] = x; return i; } int mysum( int *ai, int ni ){ int sum = 0; for(int i=0; i<ni; ++i) sum += ai[i]; return sum; } double mysum(double *ad, int nd ){ double sum = 0.0; for(int i=0; i<nd; ++i) sum += ad[i]; return sum; }
10、交換兩個元素值的重載函數

#include <iostream> //#include <cstring> using namespace std; void swap( int &a, int &b); void swap( double &a, double &b); void swap ( int *a, int n, int *b, int m); void swap ( char *a, char *b); void print(int *arr, int n); int main() { int a, b; double da, db; int aa[100], ab[100]; char s1[100], s2[100]; int i, n, m; cin >> a >> b; cin >> da >> db; cin >> n; for(i=0; i<n; i++){ cin >> aa[i]; } cin >> m; for(i=0; i<m; i++){ cin >> ab[i]; } cin >> s1 >> s2; swap( a, b ); swap( da, db ); swap( aa, n, ab, m ); swap( s1, s2); cout << a << " " << b << endl; cout << da << " " << db << endl; print( aa, m ); //交換 print( ab, n ); cout << s1 << " " << s2 << endl; return 0; } void swap( int &a, int &b){ int t = a; a = b; b = t; } void swap( double &a, double &b){ double t = a; a = b; b = t; } void swap ( int *a, int n, int *b, int m){ int i; int count = n<m ? n : m; for(i=0; i<count; ++i){ int t = a[i]; a[i] = b[i]; b[i] = t; } if(n<m){ for(; i<m; ++i) a[i] = b[i]; } if(n>m){ for(; i<n; ++i) b[i] = a[i]; } } void swap ( char *a, char *b){ /* char *t = new char[100]; strcpy( t, a ); strcpy( a, b ); strcpy( b, t );*/ for(int i=0; a[i] || b[i]; ++i){ char t = a[i]; a[i] = b[i]; b[i] = t; } } void print(int *arr, int n){ for(int i=0; i<n; ++i){ if(i) cout << " "; cout << arr[i]; } cout << endl; }
第6周 中級練習
1、遞歸插入排序

#include <iostream> using namespace std; int input(int *a); void Insert( int *a, int n ); //插入 void InsertSort( int *a, int n ); //遞歸插入 void print(int *arr, int n); int main() { int data[100]; int n; n = input( data ); InsertSort( data, n ); print( data, n); return 0; } int input(int *a){ int i = 0, x; // i = 0; for( ; cin >> x, x != -9999; ) a[i++] = x; return i; } void print(int *arr, int n){ for(int i=0; i<n; ++i){ if(i) cout << " "; cout << arr[i]; } cout << endl; } void Insert( int *a, int n )//把數組a的第n個數插入前n-1個數中 { int i = n-1; //插入前n-1個元素中 int key = a[n]; //保存插入元素 while( (i>=0) && (key<a[i]) ) { a[i+1] = a[i]; //前面元素后移 i--; } a[i+1] = key; //找到位置 } void InsertSort( int *a, int n ) { if( n>0 ) { InsertSort( a, n-1 ); // n 遞減 Insert( a, n-1 ); } }
2、遞歸求兩個數的最大公因數

#include <iostream> using namespace std; int gcd( int a, int b, int n ){ if(n==1||a%n==0 && b%n==0) return n; return gcd( a, b, n-1 ); } int main() { int a, b; cin >> a >> b; int n = a<b ? a : b; cout << gcd( a, b, n ) << endl; return 0; }
3、全排列

#include <iostream> using namespace std; void print(int array[], int end); void swap(int &a, int &b); void permute(int array[], int begin, int end); int main() { int array[100]; int n; cin >> n; for(int i=0; i<n; ++i) array[i] = i+1; permute( array, 0, n-1 ); return 0; } void permute(int array[], int begin, int end) { if (begin == end) { print(array, end); } else { for (int j = begin; j <= end; ++j) { swap(array[j], array[begin]); permute (array, begin+1, end); swap(array[j], array[begin]); } } } void print(int array[], int end) { for (int i = 0; i <= end; ++i) { if(i) cout << " "; cout << array[i]; } cout << endl; } void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
4、遞歸構造可重復字符串

#include <iostream> #include <cstring> using namespace std; void solution(int pos, char *p, int m, int n) { if( pos == n ){ cout << p << endl; return ; } for(int i='A'; i<'A'+m; i++) { p[pos] = i; solution( pos+1, p, m, n ); } } int main() { int m, n; cin >> m >> n; char *p = new char [n+1]; p[n]='\0'; solution( 0, p, m, n ); delete []p; return 0; }
5、自然數的拆分

#include <iostream> using namespace std; int n; int a[10001] = {1}; int print( int ); int search( int s, int t );//回溯算法 int main() { cin >> n; search( n, 1 ); //將要拆分的數n傳遞給s return 0; } int search(int s,int t) { int i; for(i=a[t-1]; i<=s; i++) if(i<=n) //當前數i要大於等於前1位數,且等於n { a[t] = i; //保存當前拆分的數i s -= i; //s減去i,s的值繼續拆分 if(s==0) print(t); //s==0 拆分結束輸出結果 else search( s, t+1 ); //s>0,繼續遞歸 s += i; //回溯:加上拆分的數,以便產分所有可能的拆分 } return 0; } int print(int t) { cout << n << "="; for(int i=1;i<=t;i++) { if(i==t) { cout << a[i]; cout << endl; } else cout << a[i] << "+"; } }
第6周 編程作業
1、遞歸猴子摘桃

#include <iostream> using namespace std; int monkeyandPeak(int k,int n); int main() { int n = 10; cin >> n; int count = monkeyandPeak(1,n); cout << count << endl; return 0; } int monkeyandPeak(int k,int n){ if(n==1) return k; /* k = monkeyandPeak(2*(k+1),n-1); return k; */ return monkeyandPeak(2*(k+1),n-1); }
2、編寫內聯函數求矩形的面積和周長

#include <iostream> using namespace std; inline void fun(int length, int width){ cout << length*width << " " << 2*(length+width) << endl; } int main() { int length, width; cin >> length >> width; fun(length, width); return 0; }
3、編寫重載函數顯示字符串

#include <iostream> #include <string> #include <cstring> using namespace std; void print_spaced(char* str); void print_spaced(string str); int main() { char str1[100]; string str2; cin >> str1 >> str2; print_spaced( str1 ); print_spaced( str2 ); return 0; } void print_spaced(char* str){ for(int i=0; i<strlen(str); ++i) { if(i) cout << " "; cout << str[i]; } cout << endl; } void print_spaced(string str){ for(int i=0; i<str.length(); ++i) { if(i) cout << " "; cout << str[i]; } cout << endl; }
4、排序函數重載

#include <iostream> using namespace std; void sort(int &a, int &b); void sort(int &a, int &b, int &c); void sort(int &a, int &b, int &c, int &d); void Insert( int *a, int n ); void sort( int *a, int n ); void print(int *arr, int n); int main() { int a,b,c,d; int data[100]; int k,n,i; cin >> k; switch(k) { case 1: cin >> a >> b; sort( a, b ); cout <<a << " " << b <<endl; break; case 2: cin >> a >> b >> c; sort( a, b, c); cout << a << " " << b << " " << c << endl; break; case 3: cin >> a >> b >> c >> d; sort( a, b, c, d ); cout << a << " " << b << " " << c << " " << d << endl; break; case 4: cin >> n; for(i=0; i<n; i++) { cin >> data[i]; } sort( data, n ); print( data, n); break; } return 0; } void sort(int &a, int &b){ if(a < b) { int t = a; a = b; b = t; } } void sort(int &a, int &b, int &c){ sort(a,b); sort(a,c); sort(b,c); } void sort(int &a, int &b, int &c, int &d){ sort(a,b,c); sort(a,d); sort(b,d); sort(c,d); } void print(int *arr, int n){ for(int i=0; i<n; ++i){ if(i) cout << " "; cout << arr[i]; } cout << endl; } void Insert( int *a, int n )//把數組a的第n個數插入前n-1個數中 { int i = n-1; //插入前n-1個元素中 int key = a[n]; //保存插入元素 while( (i>=0) && (key>a[i]) ) { a[i+1] = a[i]; //前面元素后移 i--; } a[i+1] = key; //找到位置 } void sort( int *a, int n ) { if( n>0 ) { sort( a, n-1 ); // n 遞減 Insert( a, n-1 ); } }
5、編寫遞歸函數來使字符串逆序

#include <iostream> #include <cstring> using namespace std; void reverse(char *str, int i, int j){ if( i < j ){ char ch = str[i]; str[i] = str[j]; str[j] = ch; reverse(str, i+1, j-1); } } int main() { char str[100]; cin.getline(str,100); int len = strlen(str); reverse(str,0,len-1); cout << str; return 0; }
第7周 基礎練習
1、兩個數的排序

#include <iostream> using namespace std; void sort(int *a, int *b){ if( *a > *b ){ int t = *a; *a = *b; *b = t; } } int main() { int a, b; cin >> a >> b; sort( &a, &b ); cout << a << " " << b << endl; return 0; }
2、三個數的排序

#include <iostream> using namespace std; void sort(int *a, int *b); void sort(int *a, int *b, int *c); int main() { int a, b, c; cin >> a >> b >> c; sort( &a, &b, &c ); cout << a << " " << b << " " << c << endl; return 0; } void sort(int *a, int *b){ if( *a > *b ){ int t = *a; *a = *b; *b = t; } } void sort(int *a, int *b, int *c){ sort(a,b); sort(a,c); sort(b,c); }
3、返回數組的統計值(最大、最小、平均值、標准差)

#include <iostream> #include <cmath> using namespace std; int input(double *a); void statistics(double a[],int n,double *max,double *min,double *avg,double *stdev); int main() { double a[100]; double max, min, avg, stdev; int n = input( a ); statistics(a,n,&max,&min,&avg,&stdev); cout << max << " " << min << " " << avg << " " << stdev << endl; return 0; } int input(double *a){ int n = 0; double x; for( ; cin >> x, fabs(x+9999)>1e-6; ) a[n++] = x; return n; } void statistics(double a[],int n,double *max,double *min,double *avg,double *stdev){ double sum1 = a[0], sum2 = 0.0; *max = *min = a[0]; for(int i=1; i<n; ++i){ if(a[i]>*max) *max = a[i]; if(a[i]<*min) *min = a[i]; sum1 += a[i]; } *avg = sum1/n; for(int i=0; i<n; ++i){ sum2 += (a[i]-*avg)*(a[i]-*avg); } *stdev = sqrt(1.0/n*sum2); }
4、通過指向函數的指針調用函數

#include <iostream> #include <cmath> using namespace std; double x2(double x); double mysin(double x); int main() { double (*f)(double); double x; cin >> x; f = x2; cout << f(x) << " "; f = mysin; cout << f(x) << endl; return 0; } double x2(double x){ return x*x; } double mysin(double x){ return 2*sin(2*3.14*2*x+3.14); }
5、計算任意一元函數值的通用函數

#include <iostream> #include <cmath> using namespace std; double x2(double x); double mysin(double x); double anyfun(double (*f)(double),double x); int main() { double x; cin >> x; cout << anyfun(x2,x) << " "; cout << anyfun(mysin,x) << endl; return 0; } double x2(double x){ return x*x; } double mysin(double x){ return 2*sin(2*3.14*2*x+3.14); } double anyfun(double (*f)(double),double x){ return f(x); }
6、計算函數在指定區間的近似平均值

#include <iostream> #include <cmath> using namespace std; double funavg(double (*f)(double x), double a,double b,int n); int main() { const int n = 1000; double a, b; cin >> a >> b; cout << funavg( exp, a, b, n ) << " "; cout << funavg( sin, a, b, n ) << " "; cout << funavg( cos, a, b, n ) << endl; return 0; } double funavg(double (*f)(double x), double a,double b,int n){ double h = (b-a)/n, sum = f(a); double x = a; for(int i=1; fabs(b-x)>1e-6; ++i){ x = a+i*h; sum += f(x); } return sum/(n+1); }
第7周 中級練習
1、指針實現向量的內積計算

#include <iostream> #include <cmath> using namespace std; int input(double *a); double solution(double *a, double *b, int n); int main() { double a[100], b[100]; int n = input( a ); input( b ); cout << solution( a, b, n ) << endl; return 0; } int input(double *a){ double *t = a, x; for( ; cin >> x, fabs(x+9999)>1e-6; ) *a++ = x; return a - t; } double solution(double *a, double *b, int n){ double sum = 0.0; for(int i=0; i<n; ++i) sum += (*a++) * (*b++); return sum; }
2、使用指針的插入排序

#include <iostream> using namespace std; int input(int *a); void InsertSort(int *arr, int n); int main() { int a[100]; int n = input( a ); InsertSort( a, n ); for(int i=0; i<n; ++i){ if(i) cout << " "; cout << *(a+i); } return 0; } int input(int *a){ int *t = a, x; for( ; cin >> x, x != -9999; ) *a++ = x; return a - t; } void InsertSort(int *arr, int n) { int i; for(i=1; i<n; ++i){/*第0個元素有序,從第1個元素向右無序*/ int j=i-1,key=*(arr+i);/*保存第i個元素,左邊的元素i-1*/ while(j>=0 && key<*(arr+j)){/*保存的元素key與之前的元素從右向左逐個比較*/ *(arr+j+1)=*(arr+j);/*移動(向后賦值)*/ j--; } *(arr+j+1)=key;/*j--退出,恢復正確值j+1*/ } }
3、指針實現成績排序

#include <iostream> using namespace std; struct Student{ int no; int achievement; }; int input(Student *s); void BubbleSort(Student *a,int left, int right); int main() { Student s[100]; int n = input( s ); BubbleSort( s, 0, n-1 ); for(int i=0; i<n; ++i){ cout << (s+i)->no << " " << (s+i)->achievement << endl; } return 0; } int input(Student *s){ Student *t = s; int x, y; for( ; cin >> x >> y, x != 0; ){ s->no = x; s->achievement = y; s++; } return s - t; } /*冒泡排序--遞歸*/ void BubbleSort(Student *a,int left, int right) { if(left<right){ int j; Student t; for(j=right; left<j; j--){ if((a+j-1)->achievement>(a+j)->achievement)/*相鄰比較*/ t=*(a+j),*(a+j)=*(a+j-1),*(a+j-1)=t; } BubbleSort(a,j+1,right);/*遞歸*/ } }
4、計算函數在某點的近似導數

#include <iostream> #include <cmath> using namespace std; #define DELTA 0.001 //德爾塔 double solution(double (*fun)(double x), double x){ return ((*fun)(x+DELTA)-(*fun)(x-DELTA))/(2*DELTA); //導數公式 } int main() { double x; cin >> x; cout << solution(sin,x) << " "; cout << solution(cos,x) << " "; cout << solution(sin,x)+solution(cos,x) << endl; return 0; }
5、計算函數在指定區間的近似積分

#include <iostream> #include <cmath> using namespace std; #define N 100 // δ = (b-a)/n, n=100 double solution(double (*fun)(double x), double a, double b){ double sum = 0.0; for(int i=0; i<N; ++i) sum += fun(a+i*(b-a)/N); return sum*(b-a)/N; } int main() { double a, b; cin >> a >> b; cout << solution(sin,a,b) << " "; cout << solution(cos,a,b) << " "; cout << solution(sin,a,b)+solution(cos,a,b) << endl; return 0; }
第7周 編程作業
1、編寫函數重置兩個變量的值

#include <iostream> #include <cmath> using namespace std; void reset(int *a, int *b); int main() { int a, b; cin >> a >> b; reset( &a, &b ); cout << a << " " << b << endl; return 0; } void reset(int *a, int *b){ *a = (*a+*b)/2.0 + 0.5; *b = *a; }
2、編寫函數對數組中的元素求和

#include <iostream> #include <cmath> using namespace std; int input( int *array); void add_array(int a, int *sum); int main() { int array[100], sum = 0; int n = input( array ); for(int i=0; i<n; ++i) add_array(array[i], &sum); cout << sum << endl; return 0; } int input( int *array){ int x, i = 0; for( ; cin >> x, x != -1; ) array[i++] = x; return i; } void add_array(int a, int *sum){ *sum += a; }
3、數組清零

#include <iostream> using namespace std; int input( int *array); void solution( int *array, int n); void output(int *p, int count); int main() { int a[100], * p= a, n; int count = input( p ); cin >> n; solution( p, n ); output(p, count); return 0; } void solution( int *array, int n){ for( int i=0; i<n; ++i ) array[i] = 0; } int input( int *array){ int x, i = 0; for( ; cin >> x, x != -1; ) array[i++] = x; return i; } void output(int *p, int count){ for(int i=0; i<count; ++i) { if(i) cout << " "; cout << p[i]; } cout << endl; }
4、使用函數指針切換加密方法

#include <iostream> using namespace std; void caesar(char s[]); void oddeven(char s[]); void cipher(void (*f)(char s[]),char s[]);//形參為指向函數的指針,對應實參可為相應格式的函數名。 int main() { char str[256]; int n; cin >> str >> n; switch(n){ case 1: cipher( caesar, str ); break; case 2: cipher( oddeven, str ); break; } return 0; } void caesar(char s[]){ for(int i=0; s[i]; ++i) { char ch = 'a'; if(s[i]>='a' && s[i]<='z'){ s[i] -= 32; //轉大寫 ch = 'A'; } else{ s[i] += 32; //轉小寫 } s[i] = ( s[i] + 3 - ch ) % 26 + ch; } cout << s << endl; } void oddeven(char s[]){ char str[256], index1 = 0, index2 = 0, i; for(i=0; s[i]; ++i){ if(i%2) //下標為奇數的保存到str str[index1++] = s[i]; else //下標為偶數的保存到s s[index2++] = s[i]; } for(i=0; i<index1; ++i) s[index2++] = str[i]; //把奇數的復制到s cout << s << endl; } void cipher(void (*f)(char s[]),char s[]){ f(s); }
5、編寫求函數區間平均值的通用函數

#include <iostream> using namespace std; int a, b, c, m; int func1(int x); int func2(int x); int avg( int (*f)(int),int x1,int x2); int main() { int x1, x2; cin >> a >> b >> c; cin >> m; cin >> x1 >> x2; cout << avg( func1, x1, x2 ) << endl; cout << avg( func2, x1, x2 ) << endl; return 0; } int func1(int x){ return a*x*x+b*x+c; } int func2(int x){ int ret = 1; for(int i=0; i<m; ++i) ret *= x; return ret; } int avg( int (*f)(int),int x1,int x2){ int sum = 0; for(int i=x1; i<=x2; ++i){ //x1,x2 區間 sum += f(i); } return sum/(x2-x1+1); }
第8周 基礎練習
1、使用指針輸出數組元素

#include <iostream> using namespace std; int main() { const int N = 20; int *arr = new int[N]; //沒有判斷申請空間是否成功 int *p = arr; int x, i = 0; for( ; cin >> x, x != 9999 && i<N ; i++ ) *p++ = x; p = arr; /* for(int j=0; j<i; ++j) { if(j) cout << " "; //判斷若干次 cout << *p++; } cout << endl;*/ if(i) { //判斷一次 cout << *p++; while(--i) cout << " " << *p++; cout << endl; } return 0; }
2、通過指針輸入輸出數組元素的函數

#include <iostream> using namespace std; int input(int *p); //輸入,返回輸入的元素個數 void print(int *p,int n); //顯示數組元素,n為元素個數 int main() { const int N = 100; int *arr = new int[N]; int n = input( arr ); print( arr, n ); return 0; } int input(int *p){ int x, i = 0; for( ; cin >> x, x != 9999; ++i ) *p++ = x; return i; } void print(int *p,int n){ if(n) { cout << *p++; while(--n) cout << " " << *p++; cout << endl; } }
3、指針實現字符串復制函數

#include <iostream> using namespace std; char * mystrcpy(char *s1,char *s2); //將s2中的內容復制到s1中,返回s1首地址 int main() { char s1[100], s2[100]; cin >> s2; cout << mystrcpy(s1,s2) << endl; return 0; } char * mystrcpy(char *s1,char *s2){ char *s = s1; while( *s1++ = *s2++ ); return s; }
4、指針實現字符串比較,不區分大小寫

#include <iostream> using namespace std; int mystrcmp(char *s1,char *s2); int main() { char s1[100], s2[100]; cin >> s1 >> s2; cout << mystrcmp(s1,s2) << endl; return 0; } int mystrcmp(char *s1,char *s2) { int ch1, ch2, ret = 0; do { if ( ((ch1 = (unsigned char)(*(s1++))) >= 'A') &&(ch1 <= 'Z') ) ch1 += 0x20; if ( ((ch2 = (unsigned char)(*(s2++))) >= 'A') &&(ch2 <= 'Z') ) ch2 += 0x20; } while ( ch1 && (ch1 == ch2) ); if ( ch1-ch2 < 0) ret = -1 ; else if ( ch1-ch2 > 0 ) ret = 1 ; return ret; }
5、通過指針訪問結構體變量

#include <iostream> using namespace std; struct PERSON{ char name[40], gender[10]; int age; }; int main() { PERSON one; PERSON *p = &one; cin >> p->name >> p->gender >> p->age; cout << p->name << " " << p->gender << " " << p->age << endl; return 0; }
6、通過指針訪問結構體數組

#include <iostream> #include <cstring> using namespace std; struct PERSON{ char name[40], gender[10]; int age; }; int main() { PERSON aclass[50]; PERSON *p = aclass; int n = 0; // cin >> p->name >> p->gender >> p->age; while(strcmp(p->name,"0") && strcmp(p->gender,"0") && p->age) { n++; p++; cin >> p->name >> p->gender >> p->age; } p = aclass+n-1; while( p>=aclass ){ cout << p->name << " " << p->gender << " " << p->age << endl; p--; } return 0; }
7、動態申請變量,別忘了釋放內存

#include <iostream> using namespace std; int main() { int *a = new int; int *b = new int ; int *c = new int; cin >> *a >> *b >> *c; cout << (*a+*b+*c) << endl; delete a, b, c; return 0; }
8、動態申請數組

#include <iostream> using namespace std; int main() { double x, *p, *q; int n, k = 0; cin >> n; q = p = new double[n]; for( ; cin >> x, x != 9999; k++) *q++ = x; q = p+k-1; cout << *q--; while( q>=p ) cout << " " << *q--; cout << endl; delete []p; return 0; }
9、動態申請結構體數組

#include <iostream> #include <cstring> using namespace std; struct PERSON{ char name[40], gender[10]; int age; }; int main() { int i, n; PERSON *t, *p; cin >> n; t = p = new PERSON[n]; cin >> t->name >> t->gender >> t->age; for( i=0; strcmp(t->name,"0") && strcmp(t->gender,"0") && t->age; ++i ){ t++; //開始輸入的不是 "0", t++ , 再次輸入 cin >> t->name >> t->gender >> t->age; } t = p+i-1; while( t>=p ){ cout << t->name << " " << t->gender << " " << t->age << endl; t--; } delete []p; return 0; }
10、動態申請結構體數組空間和結構體數組復制

#include <iostream> #include <cstring> using namespace std; struct PERSON{ char name[40], gender[10]; int age; }; int main() { int i, n; PERSON *t, *p; cin >> n; t = p = new PERSON[n]; cin >> t->name >> t->gender >> t->age; for( i=0; strcmp(t->name,"0") && strcmp(t->gender,"0") && t->age; ++i ){ t++; //開始輸入的不是 "0", t++ , 再次輸入 cin >> t->name >> t->gender >> t->age; } t = p+i-1; PERSON *t1, *p1; t1 = p1 = new PERSON[i]; while(t>=p) *t1++ = *t--; t1 = p1; //p1已經是逆序的 while( t1<p1+i ){ //不越界, 小於p1+i cout << t1->name << " " << t1->gender << " " << t1->age << endl; t1++; } delete []p; delete []p1; return 0; }
第8周 中級練習
1、IP地址轉換1

#include <iostream> using namespace std; void InitBinStr(char *BinStr); void NumToBinStr(char *BinStr, int n, int index); void solution( const char *IpStr, char *BinStr ); int main() { char IpStr[33],BinStr[33]; cin >> IpStr; InitBinStr( BinStr ); solution( IpStr, BinStr ); return 0; } void InitBinStr(char *BinStr){ char *p = BinStr; //為了更好理解 其實形參可以直接使用p for(int i=0; i<32; ++i) //賦值 *p++ = '0'; *p = '\0'; //保存二進制的字符數組,最后有個字符串結束符 } void NumToBinStr(char *BinStr, int n, int index){ while(n){ BinStr[index--] = n%2 + '0'; n /= 2; } } void solution( const char *IpStr, char *BinStr ){ const char *p = IpStr; // 為了更容易書寫 for(int n=0,index=7; index<32; p++ ) { if(*p && *p != '.')//讀數字 { n = n*10 + *p - '0'; continue; } NumToBinStr(BinStr,n,index);//遇到'.'或字符串讀完 轉換二進制 index += 8; //步長8 n = 0; //數字歸零 } cout << BinStr << endl; }
2、IP地址轉換2

#include <iostream> using namespace std; int solution( const char * ); int main() { char BinStr[33]; cin >> BinStr; cout << solution( BinStr ); for(int i=8; i<32; i+=8) cout << "." << solution( BinStr+i ); return 0; } int solution( const char *p ){ int t = 8, n = 0; while(t--) n += (*p++ - '0') << t; return n; }

#include <iostream> using namespace std; void solution( const char *BinStr ); int main() { char BinStr[33]; cin >> BinStr; solution( BinStr ); return 0; } void solution( const char *BinStr ){ const char *p = BinStr; // 為了更容易書寫 for(int n=0,t=8,cnt=0; cnt<4; p++ ) { if(t--)//讀數字 { n += (*p - '0') << t; continue; } if(cnt) cout << "."; cout << n; cnt++; p--; //t==0的時候p多++一次 t = 8; n = 0; //數字歸零 } }

#include <iostream> using namespace std; void solution( char *IpStr, const char *BinStr ); int main() { char IpStr[33],BinStr[33]; cin >> BinStr; solution( IpStr, BinStr ); return 0; } void NumToIpStr(char *IpStr,int n) { static int index = 0; static int cnt = 0; if(n==0){ IpStr[index++] = '0'; } char tmp[3]; int i = 0; while(n){ tmp[i++] = n%10 + '0'; n /= 10; } while(i--) IpStr[index++] = tmp[i]; if(++cnt<4){ IpStr[index++] = '.'; } else{ IpStr[index] = '\0'; } } void solution( char *IpStr, const char *BinStr ){ const char *p = BinStr; // 為了更容易書寫 for(int n=0,t=8,cnt=0; cnt<4; p++ ) { if(t--)//讀數字 { n += (*p - '0') << t; continue; } NumToIpStr(IpStr,n); cnt++; p--; //t==0的時候p多++一次 t = 8; n = 0; //數字歸零 } cout << IpStr << endl; }
3、找數據

#include <iostream> using namespace std; void solution(char *p) { int i, index = 0; int flag = 0, sum = 0, sign = 1; int dot = 0; for(i=0; ; ++i) { if( p[i]>='0' && p[i]<='9'|| p[i]=='.' ) { if( p[i] == '.' ){ dot = 1; continue; } if(dot){ //記錄小數點之后的位數 dot *= 10; } sum = sum*10 + p[i] - '0'; if( i && p[i-1] == '-' ) sign = -1; flag = 1; continue; } if(flag) { if(dot){ cout << 1.0*(sum*sign)/dot+9 << endl; } else{ cout << sum*sign+9 << endl; } sign = 1; //符號置 1 sum = 0; //sum置 0 flag = 0; //數字標記置 0 dot = 0; } if(!p[i]) break; //最后是數字可以輸出 } } int main() { char str[256]; cin.getline(str,256); solution(str); return 0; }

#include <iostream> using namespace std; struct IntAndDoubleArr{ bool tag; union{ double num1; int num2; }u; }; void solution(char *p) { IntAndDoubleArr arr[256]; int i, index = 0; int flag = 0, sum = 0, sign = 1; int dot = 0; for(i=0; ; ++i) { if( p[i]>='0' && p[i]<='9'|| p[i]=='.' ) { if( p[i] == '.' ){ dot = 1; continue; } if(dot){ //記錄小數點之后的位數 dot *= 10; } sum = sum*10 + p[i] - '0'; if( i && p[i-1] == '-' ) sign = -1; flag = 1; continue; } if(flag) { if(dot){ arr[index].tag = true; arr[index++].u.num1 = 1.0*(sum*sign)/dot+9; } else{ arr[index].tag = false; arr[index++].u.num2 = sum*sign+9; } sign = 1; //符號置 1 sum = 0; //sum置 0 flag = 0; //數字標記置 0 dot = 0; } if(!p[i]) break; } for(i=0; i<index; ++i) { if(arr[i].tag) cout << arr[i].u.num1 << endl; else cout << arr[i].u.num2 << endl; } } int main() { char str[256]; cin.getline(str,256); solution(str); return 0; }
4、指針實現矩陣相加

#include <iostream> using namespace std; void solution( int *, int *, int, int ); int main() { int i, j, k, index; cin >> i >> j; int *arr1 = new int[i*j]; int *arr2 = new int[i*j]; for( k=0,index=0; k<i*j; ++k ) cin >> arr1[index++]; for( k=0,index=0; k<i*j; ++k ) cin >> arr2[index++]; solution( arr1, arr2, i, j ); return 0; } void solution( int *arr1, int *arr2, int i, int j) { for( int k=0; k<i*j; ++k ) { if( k && k%j==0 ) cout << endl; else if( k && k%j) cout << " "; cout << (*arr1++) + (*arr2++); } cout << endl; }
5、指針實現矩陣相乘

#include <iostream> using namespace std; /*** 1、當矩陣A的列數(column)等於矩陣B的行數(row)時,A與B可以相乘; 2、矩陣C的行數等於矩陣A的行數,C的列數等於B的列數; 3、乘積C的第m行第n列的元素等於矩陣A的第m行的元素與矩陣B的第n列對應元素乘積之和; ***/ void solution( int *, int *, int, int, int ); int main() { int row1, column1, k, index; cin >> row1 >> column1; int *arr1 = new int[row1*column1]; for( k=0,index=0; k<row1*column1; ++k ) cin >> arr1[index++]; int row2, column2; cin >> row2 >> column2; int *arr2 = new int[row2*column2]; for( k=0,index=0; k<row2*column2; ++k ) cin >> arr2[index++]; //solution( arr1, arr2, row1, column1, row2, column2 ); //row2==column1 solution( arr1, arr2, row1, column1, column2 ); return 0; } void solution( int *arr1, int *arr2, int row1, int column1, int column2 ) { int *t1, *t2, k, row, column, sum = 0; for( row=0; row<row1; row++ ) // row1 { for( column=0; column<column2; column++ ) // column2 { t1 = arr1 + row * column1; t2 = arr2 + column; sum = 0; for( k=0; k<column1; k++ ) { sum += (*t1) * (*t2); t1 += 1; //arr1數組的行 t2 += column2; //arr2數組的列 } if(column) cout << " "; cout << sum; } cout << endl; } }
第8周 編程作業
1、輸出數字的英文名稱

#include <iostream> using namespace std; const char * digitName(int n); int main() { int n; cin >> n; cout << digitName( n ) << endl; return 0; } const char * digitName(int n){ const char *digitName[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}; return digitName[n]; }
2、去除字符串首尾多余的空格

#include <iostream> using namespace std; int main( ) { char str[100]; cin.get(str,100); char *p = str, *q = str; while( *p != '#' ) p++; while( *--p == ' ' ); *(p+1) = '#'; *(p+2) = '\0'; p = str; while( *p == ' ') p++; while( *q++ = *p++); cout << str << endl; return 0; }
3、遍歷二維數組

#include <iostream> using namespace std; int main() { int **a; //指向指針的指針 int n, m;//n行 m列 int i, j; int sum = 0; cin >> n >> m; //輸入行數和列數 //申請空間 a = new int * [n]; //n個 int 指針 數組 for( i=0; i<n; i++ ) //n個大小為m的一維數組 { a[i] = new int[m]; //1個大小為m的一維數組,a[i]是int指針 } //輸入數據 for( i=0; i<n; i++ ) { for( j=0; j<m; j++ ) { //cin>>a[i][j];//輸入 cin>> *(*(a+i)+j);//同上a相當於行指針 sum += *(*(a+i)+j); } } cout << sum << endl; /* //輸出數據 for(i=0; i<n; i++) { for(j=0; j<m; j++) { //cout<<a[i][j]<<"\t";//輸出 cout<< *(*(a+i)+j)<<"\t"; } cout<<endl; } */ //釋放申請的空間!!! for(i=0;i<n;i++) //釋放n個大小為m的一維數組 { delete []a[i]; } delete []a;//釋放int指針數組 return 0; }
4、動態申請大數組

#include <iostream> using namespace std; int *new_array(int n); void init_array(int *p, int n,int c); int main() { int n, c; cin >> n >> c; int *p = new_array( n ); init_array( p, n, c); for( int i=0; i<n; ++i ) { if(i) cout << " "; cout << *p; } delete []p; return 0; } int *new_array(int n){ int *p = new int[n]; return p; } void init_array(int *p, int n,int c){ for( int i=0; i<n; ++i ) *p++ = c; }
5、矩陣對角線元素之和

#include <iostream> using namespace std; int main() { int n, sum = 0; cin >> n; int *p = new int[n*n]; for( int i=0; i<n*n; ++i ) { cin >> *p; if(i%(n+1)==0) sum += *p; } cout << sum << endl; delete []p; return 0; }
6、十進制點分IP轉換為32位二進制IP,IP合法性是沒有連續的 '.'

#include <iostream> using namespace std; void InitBinStr(char *BinStr); void NumToBinStr(char *BinStr, int n, int index); void solution( const char *IpStr, char *BinStr ); int main() { char IpStr[33],BinStr[33]; cin >> IpStr; InitBinStr( BinStr ); solution( IpStr, BinStr ); return 0; } void InitBinStr(char *BinStr){ char *p = BinStr; //為了更好理解 其實形參可以直接使用p for(int i=0; i<32; ++i) //賦值 *p++ = '0'; *p = '\0'; //保存二進制的字符數組,最后有個字符串結束符 } void NumToBinStr(char *BinStr, int n, int index){ while(n){ BinStr[index--] = n%2 + '0'; n /= 2; } } void solution( const char *IpStr, char *BinStr ){ const char *p = IpStr; // 為了更容易書寫 //for(int n=0,index=7,flag=0; index<32; p++ ) for(int n=0,index=7; index<32; p++ ) { if(*p>='0' && *p<='9') //是數字 { n = n*10 + *p - '0'; //flag = 1; continue; } if(*(p+1)=='.'){ //經過測試題目要求的合法性是沒有連續的'.' cout<<"data error"<<endl; return; } //還有數字是否超過255 字符串是否超短 超長的情況 /* if(*p && *p!='.' || !flag || *(p+1)=='.'){ //不是數字也不是 '.' 或第一個就是'.' 或連續的'.' cout<<"data error"<<endl; return; } */ NumToBinStr(BinStr,n,index);//遇到'.'或字符串讀完 轉換二進制 index += 8; //步長8 n = 0; //數字歸零 } /* if(*(p-1)){ //字符串超長 cout<<"data error"<<endl; return; } */ cout << BinStr << endl; }

#include <iostream> using namespace std; void InitBinStr(char *BinStr); void NumToBinStr(char *BinStr, int n, int index); void solution( const char *IpStr, char *BinStr ); int main() { char IpStr[33],BinStr[33]; cin >> IpStr; InitBinStr( BinStr ); solution( IpStr, BinStr ); return 0; } void InitBinStr(char *BinStr){ char *p = BinStr; //為了更好理解 其實形參可以直接使用p for(int i=0; i<32; ++i) //賦值 *p++ = '0'; *p = '\0'; //保存二進制的字符數組,最后有個字符串結束符 } void NumToBinStr(char *BinStr, int n, int index){ while(n){ BinStr[index--] = n%2 + '0'; n /= 2; } } void solution( const char *IpStr, char *BinStr ){ const char *p = IpStr; // 為了更容易書寫 for(int n=0,index=7; index<32; p++ ) { if(*p>='0' && *p<='9') //是數字 { n = n*10 + *p - '0'; continue; } if(*(p+1)=='.'){ //經過測試題目要求的合法性是沒有連續的'.' cout<<"data error"<<endl; return; } NumToBinStr(BinStr,n,index);//遇到'.'或字符串讀完 轉換二進制 index += 8; //步長8 n = 0; //數字歸零 } cout << BinStr << endl; }
第13周 編程作業
5、讀文件中的字符並排序輸出

#include <iostream> using namespace std; int main() { int n; cin >> n; int s[256] = { 0 }; //256下標代表字符 char a; while (n--) { //輸入並排序 cin >> a; s[a]++; } int flag = 0; for(int i = 0; i<256; i++){ //按題目要求格式輸出 if(s[i]){ n = s[i]; while(n--){ if(flag) cout << " "; cout << char(i); flag++; } } } return 0; }

#include<iostream> #include<algorithm> using namespace std; int main() { int n; cin >> n; char s[100] = { 0 }; int k = 0; char a; while (n--) { cin >> a; s[k++] = a; } sort(s, s + k); for (int i = 0;i < k;i++) { if(i) cout << " "; cout << s[i]; } return 0; }

#include <iostream> #include <fstream> using namespace std; /* 5 Z Y X A C */ int main() { int t,n; cin >> n; t = n; char s[256] = { 0 }; char a; int flag = 0; //寫到文件A.txt ofstream out; // out.open("A.txt"); // while (t--) { cin >> a; if(flag) out << ' '; out << a; // flag++; // } out.close(); //讀A.txt排序 ifstream in; in.open("A.txt"); while(in){ in >> a; if(in){ s[a]++; //排序到數組 } } in.close(); //寫到文件B.txt out.open("B.txt"); // flag = 0; for(int i = 0; i<256; i++){ //按題目要求格式輸出 if(s[i]){ n = s[i]; while(n--){ if(flag){ out << ' '; cout << ' '; } out << char(i); cout << char(i); flag++; } } } out.close(); return 0; }