• [x] 1、输入3个数,求最大值

    1. //1、输入3个数,求最大值
    2. #include <iostream>
    3. using namespace std;
    4. int main()
    5. {
    6. int a, b, c, m;
    7. cin >> a >> b >> c;
    8. m = a;
    9. if (b > a)
    10. m = b;
    11. if (c > b)
    12. m = c;
    13. cout << m << endl;
    14. system("pause");
    15. retunrn 0;
    16. }
  • [x] 2、编程序,求方程ax2+bx+c=0的根 (不会)

    1. // 2、编程序,求方程ax2 + bx + c = 0的根
    2. #include <iostream>
    3. #include <cmath>
    4. using namespace std;
    5. int main()
    6. {
    7. double a, b, c, d, x1, x2;
    8. cin >> a >> b >> c;
    9. if (a == 0)
    10. if (b == 0)
    11. cout << "error\n";
    12. else
    13. cout << "x=" << -c / b << endl;
    14. else
    15. {
    16. d = b * b - 4 * a * c;
    17. if (fabs(d) <= 1e-6)
    18. cout << "x1=x2=" << -b / (2 * a) << endl;
    19. else if (d > 1e-6)
    20. {
    21. x1 = (-b + sqrt(d)) / (2 * a);
    22. x2 = (-b - sqrt(d)) / (2 * a);
    23. cout << "x1=" << x1 << ",x2=" << x2 << endl;
    24. }
    25. else
    26. cout << "方程无实根\n";
    27. }
    28. }
  • [x] 3、输入一个成绩,打印相应的等级

    1. int main()
    2. { int a;
    3. cin >> a;
    4. if(a>=90) cout<<"A";
    5. else if(a>=80) cout<<"B";
    6. else if(a>=70) cout<<"C";
    7. else if(a>=60) cout<<"D";
    8. else cout<<"E";
    9. }
  • [x] 4、输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边。

    1. int main()
    2. {
    3. double a,b,c;
    4. cin>>a>>b>>c;
    5. if(a+b>c && b+c>a && c+a>b)
    6. cout<<"可以构成三角形";
    7. else
    8. cout<<"不可以构成三角形";
    9. }
  • [x] 5、输入20个数,求其最大、最小和平均值

    1. int main()
    2. {
    3. int i;
    4. int a,max,min,s;
    5. cin>>a;
    6. max=min=a; s=a;
    7. for(i=1;i<20;i++)
    8. { cin>>a;
    9. if(a>max) max=a;
    10. if(a<min) min=a;
    11. s=s+a;
    12. }
    13. cout<<max<<","<<min<<","<<s/20.0<<endl;
    14. }
  • [x] 6、输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值。

    1. int main()
    2. {
    3. int a,m,s=0;
    4. cin>>n;
    5. cin>>a;
    6. m=a; s=a;
    7. for(int i=1; i<n; i++)
    8. { cin>>a;
    9. s +=a;
    10. if(a>m) m=a;
    11. }
    12. cout<<"平均值:"<<(double)s/n<<",最大值:"<<m<<endl;
    13. }
  • [x] 7、输入若干个数,输入-999表示结束,求平均值及最大值。

    1. #include<iostream>
    2. #include<iomanip>
    3. #include<cstdlib>
    4. using namespace std;
    5. int main()
    6. { int n, count, sum, max;
    7. double ave;
    8. count = 0;
    9. cin >> n;
    10. sum = 0; max = n;
    11. while( n != -999 )
    12. { sum = sum + n;
    13. if( n > max ) max = n;
    14. count++;
    15. cin >> n;
    16. }
    17. if( count != 0 )
    18. { ave=static_cast<double>(sum) / count;
    19. cout<<setiosflags(ios::fixed) <<setprecision(2);
    20. cout<<"平均值为:"<<ave<<" 最大值为:"<<max<<endl;
    21. }
    22. }
  • [x] 8、求和 s=11 + 22 + 33 +…+ 100100

    1. int main()
    2. { int i,t;
    3. double s=0;
    4. for(i=1; i<=100; i++)
    5. { t=i*i;
    6. s=s+t;
    7. }
    8. }

    疑问????

    ```cpp

    include

    using namespace std;

double function(int i,int sum){ if(i<=0){ return sum; } return function(i—,sum+=i*i); }

int main(){ cout<<function(100,0)<<endl; }

  1. - [x] 9、印度国王的奖励,求和 s=20 + 21 + 22 +...+ 263
  2. ```cpp
  3. int main()
  4. { double t=1,s=0;
  5. for(int i=0; i<=63; i++)
  6. { s=s+t;
  7. t=2*t;
  8. }
  9. cout<<s/1.4e8<<endl;
  10. }
  • 10、求和 s=1! + 2! + 3! +…+ 10!
    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. { int i;
    5. long t,s;
    6. t=1; s=0;
    7. for(i=1; i<=2; i++)
    8. { t=t*i;
    9. s=s+t;
    10. }
    11. cout<<s<<endl;
    12. }

11、求 e=1 + 1/1! + 1/2! + 1/3! + …

  1. int main()
  2. { int i;
  3. double t,e;
  4. i=1; t=1; e=1;
  5. while(t>=1e-7)
  6. { t=t/i;
  7. e=e+t;
  8. i=i+1;
  9. }
  10. cout<<e;
  11. }

12、求PI值,PI/4 = 1 - 1/3 + 1/5 - 1/7 + …

  1. int main()
  2. { int i,k;
  3. double pi,t;
  4. i=1; t=1; pi=0; k=1;
  5. while(fabs(t)>1e-8)
  6. { pi=pi+t;
  7. i=i+2;
  8. k=-k;
  9. t=double(k)/i;
  10. }
  11. cout<<4*pi;
  12. }

13、求PI值,PI/2 = 1 + 1/3 + 1/32/5 + 1/32/5*3/7 + …

  1. #include<iostream>
  2. #include<cstdlib>
  3. int main()
  4. {
  5. int i,j;
  6. double pi,t;
  7. i=0; j=1; t=1; pi=0;
  8. while(t>1e-18)
  9. { pi=pi+t;
  10. i=i+1;
  11. j=j+2;
  12. t=t*i/j;
  13. }
  14. cout<<setprecision(17)<<2*pi;
  15. }
  • [x] 14、输入20个数,统计其中正数、负数和零的个数。

    1. int main()
    2. {
    3. int a,n=0,m=0,s=0;
    4. for(int i=1; i<=20; i++)
    5. { cin >> a;
    6. if(a>0) n++;
    7. else if(a<0) m++;
    8. else s++;
    9. }
    10. cout<<n<<" "<<m<<" "<<s;
    11. }
  • [x] 15、输入若干个整数,计算其中的奇数之和与偶数之和,假设输入0表示结束。

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int a, n = 0, m = 0;
    6. cin >> a;
    7. while (a != 0)
    8. {
    9. if (a % 2 == 0)
    10. n += a;
    11. else m += a;
    12. cin >> a;
    13. }
    14. cout << n << " " << m;
    15. system("pause");
    16. retunrn 0;
    17. }
  • [x] 16、写一函数,计算x的y次方(假设x、y都为正整数)。

    1. int pow(int x, int y)
    2. {
    3. int s = 1;
    4. for (int i = 1; i <= y; i++)
    5. s = s * x;
    6. return s;
    7. }
  • [x] 17、求水仙花数(一个三位数,其各位数字立方和等于该数字本身) ```cpp int main() { int i, a, b, c; for (i = 100; i <= 999; i++) {

    1. a = i / 100;
    2. b = i / 10 % 10;
    3. c = i % 10;
    4. if (i == a * a * a + b * b * b + c * c * c)
    5. cout << i << endl;

    } }

int main() { int i, a, b, c; for (a = 1; a <= 9; a++) for (b = 0; b <= 9; b++) for (c = 0; c <= 9; c++) { i = a 100 + b 10 + c; if (i == a a a + b b b + c c c) cout << i << endl; } }

  1. ```cpp
  2. int main(){
  3. int i, a, b, c;
  4. for (i = 100; i <= 999; i++)
  5. {
  6. c = i%10;
  7. b = (i/10)%10;
  8. a = (i/100)%10;
  9. if (i == a * a * a + b * b * b + c * c * c)
  10. cout << i << endl;
  11. }
  12. }
  • 18、编写一个函数,确定一个整数是否为完全数(一个数,等于他的因子之和)。用这个函数确定和打印1到1000之间的所有完全数。 ```cpp

    include

    using namespace std; int perfect(int n) { int i, s = 1; for (i = 2; i <= n / 2; i++)
    1. if (n % i == 0)
    2. s = s + i;
    if (s == n)
    1. return 1;
    else
    1. return 0;
    }

int main() { int n; for (n = 2; n <= 1000; n++) if (perfect (n)) cout << n << endl; }

  1. <a name="b6eiT"></a>
  2. ## 19、写一函数,求斐波那契数列的第n项。(重点 递归非递归)
  3. ```cpp
  4. int fib(int n)
  5. {
  6. int i, f1, f2, f;
  7. if (n == 1 || n == 2)
  8. return 1;
  9. f1 = 1;
  10. f2 = 1;
  11. for (i = 3; i <= n; i++)
  12. {
  13. f = f1 + f2;
  14. f1 = f2;
  15. f2 = f;
  16. }
  17. return f;
  18. }
  • 20、整数逆序(重要)
  1. int reverse(int n)
  2. {
  3. int s = 0;
  4. while (n)
  5. {
  6. s = s * 10 + n % 10;
  7. n /= 10;
  8. };
  9. return s;
  10. }
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. long long s ,n;
  6. s=0;
  7. n=1234567890;
  8. while(n!=0){
  9. s=(n%10)+s*10;
  10. n=n/10;
  11. }
  12. cout<<s<<endl;
  13. return 0;
  14. }
  • [x] 21、写一个函数,将一个整数的各位数字的反序打印

    1. void show(int n)
    2. {
    3. while (n)
    4. {
    5. cout << n % 10 << " ";
    6. n /= 10;
    7. }
    8. }
  • [x] 22、写一个函数,将一个整数的各位数字的按顺序打印出来 ```cpp void show(int n) { int k = 1, m = n; while (m > 10) {

    1. k *= 10;
    2. m /= 10;

    } while (n) {

    1. cout << n / k << " ";
    2. n %= k;
    3. k /= 10;

    }; }

void show(int n) { int a[10], i = 0; while (n) { a[i] = n % 10; n /= 10; i++; } for (int j = i - 1; j >= 0; j—) cout << a[j] << “ “; }

void show(int n) { if (n < 10) cout << n; else { show(n / 10); cout << “ “ << n % 10; } }

  1. - [x] 23、求一个整数的各位数之和的函数
  2. ```cpp
  3. int sum(int n)
  4. {
  5. int s = 0;
  6. while (n)
  7. {
  8. s += n % 10;
  9. n /= 10;
  10. };
  11. return s;
  12. }
  • [x] 24、写一函数,判断某个数是否素数,以及求1-1000之内的素(重要)

    1. #include <iostream>
    2. #include <cmath>
    3. #include <stdlib.h>
    4. using namespace std;
    5. bool isprime(int n)
    6. {
    7. float k = sqrt(float(n));
    8. for (int i = 2; i <= k; i++)
    9. if (n % i == 0)
    10. return false;
    11. return true;
    12. }
    13. int main()
    14. {
    15. for (int n = 2; n <= 1000; n++)
    16. if (isprime(n))
    17. cout << setw(5) << n;
    18. }
  • [x] 25、用筛法求1-1000之内的素数

    1. #include<iostream>
    2. #include<cmath>
    3. #include<stdlib.h>
    4. #include<iomanip>
    5. using namespace std;
    6. int main()
    7. {
    8. int i,k,a[1001];
    9. for(i=2; i<=1000; i++) a[i]=1;
    10. float s=sqrt(float(1000));
    11. for(i=2; i<=s; i++)
    12. if(a[i]==1)
    13. { k=2*i;
    14. while(k<=1000)
    15. { a[k]=0;
    16. k=k+i;
    17. }
    18. }
    19. for(i=2; i<=1000; i++)
    20. if(a[i]==1) cout<<setw(5)<<i;
    21. }
  • [x] 26、判断某一年是否闰年的函数

    1. bool IsLeapYear(int y)
    2. { return (y%4 == 0 && y%100 != 0)||(y%400 == 0);
    3. }
  • [x] 27、写一个函数,交换两个整型变量的值 ```cpp void swap(int p, int q) { int t; t=p; p=q; q=t; }

void swap(int &a, int &b) { int t; t=a; a=b; b=t; }

  1. <a name="pirkV"></a>
  2. ## 28、求两个数的最大公约数,欧几里德算法(辗转相除法)
  3. ```cpp
  4. int gcd(int m, int n)
  5. {
  6. int k;
  7. while (n != 0)
  8. {
  9. k = m % n;
  10. m = n;
  11. n = k;
  12. }
  13. return m;
  14. }
  15. int gcd(int m, int n)
  16. {
  17. int k;
  18. while ((k = m % n) != 0)
  19. {
  20. m = n;
  21. n = k;
  22. }
  23. return n;
  24. }
  25. int gcd(int m, int n)
  26. {
  27. while (m != n)
  28. {
  29. if (m > n)
  30. m = m - n;
  31. else
  32. n = n - m;
  33. }
  34. return m;
  35. }

29、求两个数的最小公倍数

  1. int lcm(int m, int n)
  2. {
  3. int t, s;
  4. if (m < n)
  5. {
  6. t = m;
  7. m = n;
  8. n = t;
  9. }
  10. s = m;
  11. while (s % n != 0)
  12. s = s + m;
  13. }
  14. int lcm(int m, int n)
  15. {
  16. return m * n / gcd(m, n);
  17. }
  • [x] 30、百钱买百鸡问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,百钱买百鸡,问鸡翁、母、雏各几何?

    1. int main()
    2. {
    3. int cock, hen, chick;
    4. for (cock = 0; cock <= 20; cock++)
    5. for (hen = 0; hen <= 33; hen++)
    6. {
    7. chick = 100 - cock - hen;
    8. if (5 * cock + 3 * hen + chick / 3.0 == 100)
    9. cout << setw(4) << cock << setw(4) << hen << setw(4) << chick << endl;
    10. }
    11. }
  • [x] 31、编一程序,输入一行字符串,统计其中的小写英文字母的个数。

    1. int main()
    2. {
    3. char s[100];
    4. cin.getline(s, 100);
    5. int i = 0, count = 0;
    6. while (s[i] != '\0')
    7. {
    8. if (s[i] >= 'a' && s[i] <= 'z')
    9. count++;
    10. i++;
    11. }
    12. cout << count << endl;
    13. }
  • [x] 32、编一程序,输入一行字符串,将其中的大写英文字母改为小写,再输出。

    1. int main()
    2. {
    3. char s[100];
    4. int i;
    5. cin.getline(s, 100);
    6. i = 0;
    7. while (s[i] != '\0')
    8. if (s[i] >= 'A' && s[i] <= 'Z')
    9. s[i] = s[i] + 32;
    10. cout << s << endl;
    11. }

33、打印杨辉三角形(帕斯卡三角形),打印10行。
#include
#include
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=1; i<10; i++)
for(int j=1; ja[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<cout<}
}

34、打印一个九九乘法表
#include
#include
using namespace std;
int main()
{
for(int j=1; j<=9; j++)
{ for(int i=1; i<=j; i++)
cout<cout<}
}

35、掷骰子10000次,统计得到各点数的次数。
int main()
{ int a[7]={0};
srand(time(0));
for(int i=1; i <= 10000 ; ++i)
++a[ 1 + rand()%6 ];
for(int i=1; i <= 6 ; ++i)
cout<}

36、编写函数distance,计算两点(x1,y1)和(x2,y2)之间的距离。
double distance(double x1, double y1, double x2, double y2)
{
return sqrt( (x1-x2)(x1-x2) + (y1-y2)(y1-y2) );
}

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(xif(x>max) max = x;
}
s = s - min - max;
cout << s/8;
}

38、写一函数,将一数组中的元素反转。
void reverse(int a[], int n)
{ for(int i=0; iswap(a[i],a[n-i-1]);
}

39、写一函数,在一个数组中找出最大元素的位置
int SearchMax(int a[], int n)
{ int k = 0;
for(int i=1; iif(a[i]>a[k]) k = i;
return k;
}

40、找出一个二维数组中的鞍点,即该元素在该行上最大,在该列上最小。

41、写一个字符串拷贝函数
void strcpy(char p, const char q)
{
while(p++=q++);
}

char strcpy(char str1, const char str2)
{
char
p=str1;
while(str1++=str2++);
return p;
}

42、写一个字符串比较函数
int strcmp(char str1, const char str2)
{
while(str1 && str2 && str1==str2)
{ str1++; str2++; }
return str1-str2;
}

int strcmp(char str1, const char str2)
{
while(str1==str2)
{ if(str1==’\0’) return 0;
str1++; str2++;
}
return
str1-*str2;
}

43、写一个字符串连接函数
char strcat(char str1, char str2)
{
char
p=str1;
while(str1!=0) str1++;
while(
str1++=*str2++);
return p;
}

44、写一个求字符串长度函数
int strlen(char str)
{
int n=0;
while(
str!=’\0’)
{ n++; str++; }
return n;
}

45、写一函数,在一数组里查找某个值。
int search(int a[], int n, int key)
{ for(int i=0; iif(a[i]==key)
return i;
return -1;
}

  • [x] 46、编一程序,求两个矩阵的乘积

  • [x] 47、计算某日是某年的第几天 ```cpp bool isLeapYear(int y) //判断某一年是否闰年 { return (y%4 == 0 && y%100 != 0)||(y%400 == 0); }

int main() { int year,month,day,i,s=0; int a[13]=(0,31,28,31,30,31,30,31,31,30,31,30,31}; cin>>year>>month>>day; for(i=1; i2) s++; cout << s; }

  1. <a name="PrHAF"></a>
  2. ## 48、编写一个帮助小学生学习加法的程序,随机产生2个数,让学生输入答案。
  3. ```cpp
  4. #include<iostream>
  5. #include<cstdlib>
  6. using namespace std;
  7. int main()
  8. {
  9. int x,y,z;
  10. srand( time(0) );
  11. x = rand() % 1000;
  12. y = rand() % 1000;
  13. cout << x << " + " << y << " = ";
  14. cin >> z;
  15. while( z != 0 )
  16. {
  17. while( z != x+y )
  18. {
  19. cout<<" × 错误!请重做\n" ;
  20. cout<<x<<" + "<<y<<" = ";
  21. cin>>z;
  22. }
  23. cout<<" √ 正确!\n" ;
  24. x = rand() % 1000;
  25. y = rand() % 1000;
  26. cout<<x<<" + "<<y<<" = ";
  27. cin>>z;
  28. }
  29. }

49、从52个数里选13个数

  1. int main()
  2. {
  3. int i, k, a[52], b[13];
  4. for (i = 0; i < 52; i++)
  5. a[i] = i + 1;
  6. srand(time(0));
  7. for (i = 0; i < 13; i++)
  8. {
  9. k = rand() % (52 - i);
  10. b[i] = a[k];
  11. swap(a[k], a[51 - i]);
  12. }
  13. for (i = 0; i < 13; i++)
  14. cout << b[i] << " ";
  15. }

50、求100!

  1. #include <fstream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5. const long MOD = 10000;
  6. int main()
  7. {
  8. int t, t0 = time(0);
  9. int len, n = 100000;
  10. unsigned long a[200000];
  11. a[1] = 1;
  12. len = 1;
  13. for (int k = 2; k <= n; k++)
  14. {
  15. long carry = 0;
  16. for (int i = 1; i <= len; i++)
  17. {
  18. unsigned long s = a[i] * k + carry;
  19. carry = s / MOD;
  20. a[i] = s % MOD;
  21. }
  22. while (carry > 0)
  23. {
  24. len++;
  25. a[len] = carry % MOD;
  26. carry = carry / MOD;
  27. }
  28. }
  29. t = time(0);
  30. int w = (len - 1) * 4 + int(log10(double(a[len]))) + 1;
  31. ofstream fout("factorial.txt");
  32. fout << n << "! = \n"
  33. << a[len];
  34. fout << setfill('0');
  35. for (int i = len - 1; i >= 1; i--)
  36. fout << setw(4) << a[i];
  37. fout << endl;
  38. fout << "用时:" << t - t0 << "秒" << endl;
  39. fout << "数组元素个数:" << len << " 阶乘值位数:" << w << endl;
  40. return 0;
  41. }