1、输入3个数,求最大值
int main()
{ int a,b,c,m; cin>>a>>b>>c; m=a; if(b>m) m=b; if(c>m) m=c; cout<<m;
}
2、编程序,求方程ax2+bx+c=0的根
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{double a, b, c, d, x1, x2;cin >> a >> b >> c;if (a == 0)if (b == 0) cout << "error\n";else cout << "x=" << -c / b << endl;else{d = b * b - 4 * a*c;if (fabs(d) <= 1e-6)cout << "x1=x2=" << -b / (2 * a) << endl;else if (d > 1e-6){x1 = (-b + sqrt(d)) / (2 * a);x2 = (-b - sqrt(d)) / (2 * a);cout << "x1=" << x1 << ",x2=" << x2 << endl;}else cout << "方程无实根\n";}return 0;
}
3、输入一个成绩,打印相应的等级
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{int score;cin >> score;switch (score / 10){case 10:case 9:cout << "A" << endl; break;case 8:cout << "B" << endl; break;case 7:cout << "C" << endl; break;case 6:cout << "D" << endl; break;default:cout << "E" << endl; break;}return 0;
}
4、输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{double a, b, c;cin >> a >> b >> c;if (a + b > c&&a + c > b&&b + c > a)cout << "可以构成三角形" << endl;elsecout << "不能构成三角形" << endl;return 0;
}
5、输入20个数,求其最大、最小和平均值
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{int a[20];double sum = 0;for (int i = 0; i < 20; i++){cout << i + 1 << " ";cin >> a[i];sum += a[i];}sort(a, a + 20);cout << "max=" << a[19] << endl;cout << "min=" << a[0] << endl;cout << "average=" << sum / 20 << endl;return 0;
}
6、输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{int n, max, temp;double sum = 0;cin >> n;cin >> temp;max = temp;sum = temp;for (int i = 1; i < n; i++){cin >> temp;if (temp > max)max = temp;sum += temp;}cout << "平均值:" << double(sum) / n << endl;cout << "最大值:" << max << endl;return 0;
}
7、输入若干个数,输入-999表示结束,求平均值及最大值。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{int n, cnt = 0;double max, sum = 0;cin >> n;if (n != -999){sum = n;max = n;cnt++;}while (cin >> n){if (n == -999)break;if (n > max)max = n;sum += n;cnt++;}cout << "最大值:" << max << endl;cout << "平均值:" << sum /double(cnt) << endl;return 0;
}
8、求和 s=11 + 22 + 33 +…+ 100100
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{double sum = 0;for (int i = 1; i <= 100; i++){sum += i * i;}cout << sum << endl;return 0;
}
9、印度国王的奖励
int main()
{ double t=1,s=0; for(int i=0; i<=63; i++) { s=s+t; t=2*t; } cout<<s/1.4e8<<endl;
}
10、求和 s=1! + 2! + 3! +…+ 10!
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int fun(int n)
{if (n == 1)return 1;elsereturn n * fun(n - 1);
}
int main()
{long sum = 0;for (int i = 1; i <= 10; i++)sum += fun(i);cout << sum << endl;return 0;
}
11、求 e=1 + 1/1! + 1/2! + 1/3! + …(前20项)
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int fun(int n)
{if (n == 1)return 1;elsereturn n * fun(n-1);
}
int main()
{double e = 0;for (int i = 1; i <= 20; i++){e += double(1) / fun(i);}cout << e << endl;return 0;
}
12、求PI值,PI/4 = 1 - 1/3 + 1/5 - 1/7 + …
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{double pi = 0,t=1;int s = 1;int i = 1;while(fabs(t)>1e-18){pi += t;s = -s;i = i + 2;;t = double(s) / i;}cout << 4*pi << endl;return 0;
}
14、输入20个数,统计其中正数、负数和零的个数。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{int a = 0, b = 0, c = 0, n;for (int i = 0; i < 20; i++){cout << i + 1 << ": ";cin >> n;if (n == 0)c++;else if (n > 0)a++;elseb++;}cout << "正数:" << a << endl;cout << "负数:" << b << endl;cout << "零:" << c << endl;return 0;
}
15、输入若干个整数,计算其中的奇数之和与偶数之和,假设输入0表示结束
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int main()
{int a = 0, b = 0;int n;while (cin >> n){if (n == 0)break;if (n % 2 == 0)a += n;elseb += n;}cout << "偶数之和:" << a << endl;cout << "奇数之和:" << b << endl;return 0;
}
16、写一函数,计算x的y次方(假设x、y都为正整数)。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int pow(int x, int y)
{int s = 1;for (int i = 1; i <= y; i++)s = s * x;return s;
}
int main()
{int x, y;cin >> x >> y;cout << "x的y次方=" << pow(x, y) << endl;return 0;
}
17、求水仙花数(一个三位数,其各位数字立方和等于该数字本身)
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;bool fun(int n)
{int a = n % 10;//个位数int b = n / 10 % 10;//十位数int c = n / 100; //百位数if (n == (a*a*a + b * b*b + c * c*c))return true;return false;
}int main()
{int cnt = 0;for(int i=100;i<=999;i++)if (fun(i)) {cout << i << '\t';cnt++;if (cnt % 5 == 0)cout << endl;}return 0;
}
18、编写一个函数,确定一个整数是否为完全数(一个数,等于他的因子之和)。用这个函数确定和打印1到1000之间的所有完全数。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;bool fun(int n)
{int s = 0;for (int i = 1; i <= n / 2; i++)if (n%i == 0)s += i;if (s == n)return true;return false;
}
int main()
{for (int i = 2; i <= 1000; i++)if (fun(i))cout << i << endl;return 0;
}
19、写一函数,求斐波那契数列的第n项。
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int fun(int n)
{if (n == 1 || n == 2)return 1;elsereturn fun(n - 1) + fun(n - 2);
}int main()
{int sum = 0;for (int i = 1; i <= 20; i++){cout << fun(i) << '\t';if (i % 5 == 0)cout << endl;}return 0;
}
20、写一个函数,取一个整数值并返回将此整数的各数字反序的数值
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int fun(int n)
{int s = 0;while (n){s = s * 10 + n % 10;n = n / 10;}return s;
}int main()
{int n;cin >> n;cout << fun(n) << endl;return 0;
}
21、写一个函数,将一个整数的各位数字的反序打印
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;void fun(int n)
{while (n){cout<< n % 10<<" ";n = n / 10;}cout << endl;
}int main()
{int n;cin >> n;fun(n);return 0;
}
22、写一个函数,将一个整数的各位数字的按顺序打印出来
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;//数组方法
void show(int n)
{int a[10], i = 0;while (n){a[i++] = n % 10;n = n / 10;}for (int j = i - 1; j >= 0; j--)cout << a[j] << " ";cout << endl;
}
//递归
void show1(int n)
{if (n < 10)cout << n << endl;else {show1(n/10);cout << n % 10 << " ";}cout << endl;
}
int main()
{int n;cin >> n;cout << "数组存储方法:" << endl;show(n);cout << "递归方法:" << endl;show(n);return 0;
}
23、求一个整数的各位数之和的函数
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int fun(int n)
{int s = 0;while (n){s += n % 10;n = n / 10;}return s;
}
int main()
{int n;cin >> n;cout << fun(n) << endl;return 0;
}
24、写一函数,判断某个数是否素数,以及求1-1000之内的素数
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;bool isprime(int n)
{for (int i = 2; i <= sqrt(n); i++)if (n%i == 0)return false;return true;
}
int main()
{int cnt = 0;for (int i = 1; i <= 1000; i++){if (isprime(i)) {cnt++;cout << i << '\t';if (cnt % 5 == 0)cout << endl;}}return 0;
}
25、用筛法求1-1000之内的素数
#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
using namespace std;
int main()
{ int i,k,a[1001]; for(i=2; i<=1000; i++) a[i]=1; float s=sqrt(float(1000)); for(i=2; i<=s; i++) if(a[i]==1) { k=2*i; while(k<=1000) { a[k]=0; k=k+i; } } for(i=2; i<=1000; i++) if(a[i]==1) cout<<setw(5)<<i;
}
26、判断某一年是否闰年的函数
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;bool IsLeapYear(int y)
{if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)return true;return false;
}
int main()
{int year;cin >> year;if (IsLeapYear(year))cout << year << "是闰年!" << endl;elsecout << year << "不是闰年!" << endl;return 0;
}
27、写一个函数,交换两个整型变量的值
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;void swap(int &a, int &b)
{int t;t = a;a = b;b = t;
}
int main()
{int a, b;cout << "输入a和b:" << endl;cin >> a >> b;swap(a, b);cout << "交换后的a和b:" << endl;cout << a << " " << b << endl;return 0;
}
28、求两个数的最大公约数,欧几里德算法(辗转相除法)
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int gcd(int a, int b)
{if (a%b == 0)return b;elsereturn gcd(b, a%b);
}
int main()
{int a, b;cin >> a >> b;cout << "最大公约数:" << gcd(a, b) << endl;return 0;
}
29、求两个数的最小公倍数
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;int gcd(int a, int b)
{if (a%b == 0)return b;elsereturn gcd(b, a%b);
}
int main()
{int a, b;cin >> a >> b;cout << "最小公倍数:" << a*b/gcd(a, b) << endl;return 0;
}
30、百钱买百鸡问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,百钱买百鸡,问鸡翁、母、雏各几何?
int main()
{int cock,hen,chick;for(cock=0; cock<=20; cock++) for(hen=0; hen<=33; hen++){ chick=100-cock-hen;if(5*cock+3*hen+chick/3.0==100)cout<<setw(4)<<cock<<setw(4)<<hen <<setw(4)<<chick<<endl;}
}
31、编一程序,输入一行字符串,统计其中的小写英文字母的个数。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;int main()
{int cnt = 0;string s;cin >> s;for (int i = 0; i < s.size(); i++){if (s[i] >= 'a'&&s[i] <= 'z')cnt++;}cout << "小写字母个数:" << cnt << endl;
}
32、编一程序,输入一行字符串,将其中的大写英文字母改为小写,再输出。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;int main()
{int cnt = 0;string s;cin >> s;for (int i = 0; i < s.size(); i++){if (s[i] >= 'A'&&s[i] <= 'Z')s[i] += 32;}cout << s<< endl;
}
33、打印杨辉三角形(帕斯卡三角形),打印10行。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;int main()
{int a[10][10] = { 0 };for (int i = 0; i < 10; i++){a[i][0] = 1;a[i][i] = 1;}for(int i=2;i<10;i++)for (int j = 1; j <= i; j++){a[i][j] = a[i - 1][j - 1] + a[i - 1][j];}for (int i = 0; i < 10; i++) {for (int j = 0; j <= i; j++)cout << a[i][j] << " ";cout << endl;}return 0;
}
34、打印一个九九乘法表
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;int main()
{for (int i = 1; i <= 9; i++){for (int j = 1; j <=i; j++)cout << i << "*" << j << "=" << i * j << " ";cout << endl;}return 0;
}
35、掷骰子10000次,统计得到各点数的次数。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;int main()
{int a[7] = { 0 };srand(time(NULL));for (int i = 1; i <= 10000; i++)++a[rand() % 6 + 1];for (int i = 1; i <= 6; i++)cout << i << ":" << a[i] << endl;return 0;
}
36、编写函数distance,计算两点(x1,y1)和(x2,y2)之间的距离。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;double distance(double x1, double y1, double x2, double y2)
{return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}int main()
{double x1, x2, y1, y2;cout << "输入两点" << endl;cin >> x1 >> y1 >> x2 >> y2;cout << distance(x1, x2, y1, y2) << endl;return 0;
}
37、写一个程序,进行体操评分,依次输入10名评委所评分数,去除一个最高分和一个最低分,再算出平均分作为选手的得分。
int main()
{ int i;float max,min,s,x;max = 0; min = 10; s=0;for(i=1;i<=10;i++){ cin >> x;s = s + x;if(x<min) min = x;if(x>max) max = x;}s = s - min - max;cout << s/8;
}
38、写一函数,将一数组中的元素反转。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;void reverse(int a[], int n)
{int t;for (int i = 0; i < n / 2; i++){t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}
}int main()
{int a[100];int n;cin >> n;for (int i = 0; i < n; i++)cin >> a[i];reverse(a, n);for (int i = 0; i < n; i++){cout << a[i] << " ";}cout << endl;return 0;
}
39、写一函数,在一个数组中找出最大元素的位置
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;int fun(int a[], int n)
{int max=a[0], x;for (int i = 0; i < n; i++){if (a[i] > max){max = a[i];x = i;}}return x;
}int main()
{int n, a[100];cin >> n;for (int i = 0; i < n; i++)cin >> a[i];cout << "最大值的位置:"<<fun(a, n) << endl;return 0;
}
41、写一个字符串拷贝函数
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;void strcp(char *p, const char *q)
{while (*p = *q){p++;q++;}
}
int main()
{char a[20] = "hello world!";char b[20];strcp(b, a);cout << b << endl;return 0;
}
42、写一个字符串比较函数
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;int cmp(char *a, char *b)
{while (*a&&*b&&*a == *b){a++;b++;}return *a - *b;
}
int main()
{char a[20] = "hello world!";char b[20] = "hello !";cout << cmp(a, b) << endl;return 0;
}
43、写一个字符串连接函数
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;char *str(char *a, char *b)
{char *p = a;while (*a)a++;while (*a++ = *b++);*a = '\0';return p;
}
int main()
{char a[100] = "hello world!";char b[20] = "my name is Lily";cout << str(a, b) << endl;return 0;
}
44、写一个求字符串长度函数
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;int len(char *a)
{int len = 0;while (*a){len++;a++;}return len;
}
int main()
{char a[20] = "hello world!";cout << "hello world!长度:" << len(a) << endl;return 0;
}
45、写一函数,在一数组里查找某个值。
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;int search(int a[], int n, int key)
{for (int i = 0; i < n; i++)if (a[i] == key)return i;return -1;
}
int main()
{int n, a[100];cin >> n;for (int i = 0; i < n; i++)cin >> a[i];cout << "输入你要搜索的关键词:" << endl;int key;cin >> key;cout << "key的位置:" << search(a, n, key);return 0;
}
46、编一程序,求两个矩阵的乘积
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;int main()
{int a[3][3], b[3][3], c[3][3];for (int i = 0; i < 3; i++)for (int j = 0; j < 3; j++)cin >> a[i][j];for (int i = 0; i < 3; i++)for (int j = 0; j < 3; j++)cin >> b[i][j];for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){int sum = 0;for (int k = 0; k < 3; k++){sum += a[i][k] * b[k][j];}c[i][j] = sum;}}for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++)cout << c[i][j] << " ";cout << endl;}return 0;
}
47、计算某日是某年的第几天
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;bool fun(int y)
{if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)return true;return false;
}
int main()
{int y;cin >> y;if (fun(y))cout << y << "是闰年!" << endl;elsecout << y << "不是闰年!" << endl;return 0;
}