第一周 基础练习
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<< "慈母手中线 " "游子身上衣 " "临行密密缝 " "意恐迟迟归 " "谁言寸草心 " "报得三春晖 "; return 0; }
#include <iostream> using namespace std; int main() { cout<< "慈母手中线 游子身上衣 临行密密缝 意恐迟迟归 谁言寸草心 报得三春晖" << 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缓冲区), 当在键盘输入' '时,键盘缓冲区中的字符才被全部传送到stdin缓冲区中 */ cin >> name; cout << name << endl; cin >> name; /* 忽略空白符 缓冲区还剩余一个回车*/ cout << name << endl; cin.sync(); /* 清空缓冲区 */ /* 可以读取空白符' ' 以回车作为结束标记并且读取回车符 缓冲区没有剩余*/ 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) = '