50道C/C++编程练习题 复习必备(31-40)

C/C++编程练习题 (31-40)

  • 31. 编一程序,输入一行字符串,统计其中的小写英文字母的个数
  • 32. 编一程序,输入一行字符串,将其中的大写英文字母改为小写,再输出
  • 33. 打印杨辉三角形(帕斯卡三角形),打印10行
  • 34. 打印一个九九乘法表
  • 35. 掷骰子10000次,统计得到各点数的次数
  • 36. 编写函数distance,计算两点(x1,y1)和(x2,y2)之间的距离
  • 37. 写一个程序,进行体操评分,依次输入10名评委所评分数,去除一个最高分和一个最低分,再算出平均分作为选手的得分
  • 38. 写一函数,将一数组中的元素反转
  • 39. 写一函数,在一个数组中找出最大元素的位置
  • 40. 找出一个二维数组中的鞍点,即该元素在该行上最大,在该列上最小

使用C/C++两种语言完成50道题目,体会两种语言之间的不同。编译环境都是在VS2019,完成时间2020.06.21-2020.06.22 。由于C++ 对C的兼容性,部分main()函数没有写return 0 也可以执行成功,但是不提倡,最好加上,否则其他编译器可能报错。C语言不需要严格对齐,因此不对CSDN 的格式做过多的纠正。

31. 编一程序,输入一行字符串,统计其中的小写英文字母的个数

C

#include
int main()
{
 char st[100];
 printf("请输入字符串:");
 scanf_s("%s", st, 100);
 int i = 0, num = 0;
 while (st[i] != '\0')
 {
  if (st[i] >= 'a' and st[i] <= 'z')
   num=num+1;
  i++;
 }
 printf("%d",num);
}

C++

#include
using namespace std;
int main()
{
 char s[100];
 cin.getline(s,100);
 int i = 0, num = 0;
 while (s[i]!='\0')
 {
  if (s[i] >= 'a' and s[i] <= 'z')
   num++;
  i++;
 }
 cout << num;
}

32. 编一程序,输入一行字符串,将其中的大写英文字母改为小写,再输出

C

#include
int main()
{
 char s[100];
 scanf_s("%s", s, 100);
 int i = 0, num = 0;
 while (s[i] != '\0')
 {
  if (s[i] >= 'A' and s[i] <= 'Z')
   s[i] += 32;
  i++;
 }
 i = 0;
 while (s[i]!='\0')
 {
  printf("%c", s[i]);
  i++;
 }
}

C++

#include
using namespace std;
int main()
{
 char s[100];
 cin.getline(s, 100);
 int i = 0, num = 0;
 while (s[i] != '\0')
 {
  if (s[i] >= 'A' and s[i] <= 'Z')
   s[i]+=32;
  i++;
 }
 cout << s<<endl;
}

33. 打印杨辉三角形(帕斯卡三角形),打印10行

C

#include
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; 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++)
   printf("%d \t", a[i][j]);
  printf("\n");
 }
}

C++

#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; 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 << setw(4) << a[i][j];
  cout << endl;
 }
}

34. 打印一个九九乘法表

C

#include
int main()
{
 int i, j;
 for (i = 1; i < 10; i++)
 {
  for (j = 1; j <= i; j++)
  {
   printf("i*j=%d\t", i * j);
  }
  printf("\n");
 }
 return 0;
}

C++

#include
using namespace std;
int main()
{
 int i, j;
 for (i = 1; i < 10; i++) 
 {
  for (j = 1; j <= i; j++)
  {
   cout << i<<"*"<< "="<< i*j<< " ";
     }
  cout << endl;
    }
}

35. 掷骰子10000次,统计得到各点数的次数

C

#include
#include
#include
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++)
    printf("%d : %d\n", i, a[i]);
}

C++

#include
using namespace std;
#include
#include
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 << i << ": " << a[i] << endl;
}

36. 编写函数distance,计算两点(x1,y1)和(x2,y2)之间的距离

C

#include
#include
double dis(double, double, double, double);
int main()
{
 double(d);
 double x1 = 0, x2 = 4, y1 = 0, y2 = 3;
 d = dis(x1, x2, y1, y2);
 printf("%lf",d);
}
double dis(double x1, double x2, double y1, double y2)
{
 double d = sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2));
 return d;
}

C++

#include
using namespace std;
double dis(double, double,double, double);
int main()
{
 double(d);
 double x1 = 0, x2 = 4, y1 = 0, y2 = 3;
 d = dis(x1, x2, y1, y2);
 cout << d;
}
double dis(double x1, double x2, double y1,double y2)
{
 double d = sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2));
 return d;
}

37. 写一个程序,进行体操评分,依次输入10名评委所评分数,去除一个最高分和一个最低分,再算出平均分作为选手的得分

C

#include
int main()
{
 int a, max1, min1;
    float total, ave;
 printf("请输入十位评委的分数");
 scanf_s("%d",&a);
 max1 = min1 = total = a;
 for (int i = 0; i < 9; i++)
 {
  scanf_s("%d", &a);
  if (max1 < a) max1 = a;
  if (min1 > a)  min1 = a;
  total = total + a;
 }
 total = total - min1 - max1;
 ave = total / 8;
 printf( "%lf",ave);
 return 0;
}

C++

#include
using namespace std;
int main()
{
 float a,max, min, total, ave;
 cout << "请输入十位评委的分数" << endl;
 cin >> a;
 max = min =total= a;
 for (int i=0;i<9;i++)
 {
  cin >> a;
  if (max < a) max = a;
  if (min > a)  min = a;
  total = total + a;
 }
 total = total - min - max;
 ave = total / 8;
 cout << ave;
}

38. 写一函数,将一数组中的元素反转

C

#include
void  reverse(int*, int);
int main()
{
 int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
 int n = sizeof(a) / sizeof(a[0]);
 reverse(a, n);
 for (int i = 0; i < 10; i++)
   printf("%d ",a[i]);
}
void  reverse(int* a, int n)
{
 int temp;
 for (int i = 0; i < n / 2; i++)
 {
  temp = a[i];
  a[i] = a[n - 1 - i];
  a[n - 1 - i] = temp;
 }
}

C++

#include
using namespace std;
void  reverse(int *,int);
int main()
{
 int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
 int n = sizeof(a)/sizeof(a[0]);
 reverse(a,n);
 for (int i = 0; i < 10; i++)
  cout << a[i]<<" ";
}
void  reverse(int* a,int n)
{
 int temp;
 for (int i = 0; i < n / 2; i++)
 {
  temp = a[i];
  a[i] = a[n - 1 - i];
  a[n - 1 - i] = temp;
 }
}

39. 写一函数,在一个数组中找出最大元素的位置

C++

#include
using namespace std;
void  findmax(int*, int);
int main()
{
 int a[10] = { 1,2,3,4,25,16,97,8,9,10 };
 int n = sizeof(a) / sizeof(a[0]);
 findmax(a, n);
}
void findmax(int * a,int n)
{
 int max = a[0];
 int k=0;
 for (int i = 1; i < n; i++)
 {
  if (a[i] > max) 
  {
   max = a[i];
   k = i;
  }
 }
 cout << "位置在第" << k + 1<<"个";
}

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

C

#include 
#include 
int main()
{
 int a[3][3];
 int i, j, k, p, q, max, min;
 for (i = 0; i < 3; i++) {
  for (j = 0; j < 3; j++)
   scanf_s("%d", &a[i][j]);               //输入数组
 }
 for (i = 0; i < 3; i++) {
  for (j = 0, max = 0, p = 0; j < 3; j++) {         //判断出该行元素的最大值
   if (a[i][j] > max) {
    max = a[i][j];
    p = j;
   }
   else {
    max = max;
    p = p;
   }
  }
  for (k = 0, min = a[i][p], q = i; k < 3; k++) {   //判断最大值在该列是否为最小值
   if (a[k][p] < min) {
    min = a[k][p];
    q = k;
   }
   else {
    min = min;
    q = q;
   }
  }
  if (i == q) {
   printf("%d is the saddle point!\n", a[i][p]);  //如果该最大值行号没有改变,输出鞍点
   break;
  }
 }
 if (i == 3 && i != q)
  printf("There is no saddle point!\n");             //输出没有鞍点
 system("pause");
 return 0;
}

C++

#include
using namespace std;
int main()
{
 int a[3][3];
 int i, j, k, p, q, max, min;
 for (i = 0; i < 3; i++) {
  for (j = 0; j < 3; j++)
   cin>>a[i][j];               //输入数组
 }
 for (i = 0; i < 3; i++) {
  for (j = 0, max = 0, p = 0; j < 3; j++) {         //判断出该行元素的最大值
   if (a[i][j] > max) {
    max = a[i][j];
    p = j;
   }
   else {
    max = max;
    p = p;
   }
  }
  for (k = 0, min = a[i][p], q = i; k < 3; k++) {   //判断最大值在该列是否为最小值
   if (a[k][p] < min) {
    min = a[k][p];
    q = k;
   }
   else {
    min = min;
    q = q;
   }
  }
  if (i == q) {
   cout << a[i][p] <<" is the saddle point!";  //如果该最大值行号没有改变,输出鞍点
   break;
  }
 }
 if (i == 3 && i != q)
  cout<<"There is no saddle point!"<<endl;             //输出没有鞍点
}

你可能感兴趣的:(C语言)