21xrx.com
2025-03-26 12:03:43 Wednesday
文章检索 我的文章 写文章
「C++编程」六个有趣的代码片段,让你爱上编程之美!
2023-07-03 07:04:10 深夜i     7     0
C++ 编程 代码片段 有趣

编程是一项非常有趣和富有挑战性的活动,它让人们可以通过代码来创造出各种有用的应用程序和工具。在这篇文章中,我们将分享六个有趣的C++编程代码片段,向你展示编程之美的精髓。

1. Fibonacci序列

Fibonacci序列是以斐波那契数列命名的一种数列,它的前两个数字是0和1,以后每一个数字都是前两个数字的和。下面这段代码可以生成指定数量的Fibonacci序列数字:

int main()
{
  int count, i, t1 = 0, t2 = 1, nextTerm;
  cout << "Enter the number of terms: ";
  cin >> count;
  cout << "Fibonacci Series: ";
  for (i = 1; i <= count; ++i)
  {
    cout << t1 << " ";
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
  }
  return 0;
}

2.颠倒字符串

颠倒一个字符串是一个有趣的挑战,下面这段代码可以颠倒一个字符串中的每一个字符:

#include <iostream>
#include <string.h>
using namespace std;
void reverse(string str)
{
  int length = str.length();
  for (int i = length - 1; i >= 0; i--)
  {
    cout << str[i];
  }
}
int main()
{
  string str;
  cout << "Enter a string: ";
  cin >> str;
  cout << "The reversed string is: ";
  reverse(str);
  return 0;
}

3.检查一个数是否是质数

下面这段代码可以检查一个数是否是质数:

#include <iostream>
using namespace std;
int main()
{
  int n, i;
  bool isPrime = true;
  cout << "Enter a positive integer: ";
  cin >> n;
  for (i = 2; i <= n / 2; ++i)
  {
    if (n % i == 0)
    
      isPrime = false;
      break;
    
  }
  if (isPrime)
    cout << "This is a prime number";
  else
    cout << "This is not a prime number";
  return 0;
}

4. 计算一个数的平方根

下面这段代码可以计算一个数的平方根:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  double num, root;
  cout << "Enter a number to find the square root of: ";
  cin >> num;
  root = sqrt(num);
  cout << "Square root of " << num << " is " << root;
  return 0;
}

5. 计算两个矩阵的乘积

下面这段代码可以计算两个矩阵的乘积:

#include <iostream>
using namespace std;
int main()
{
  int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
  cout << "Enter the number of rows and columns of matrix A: ";
  cin >> r >> c;
  cout << "Enter the elements of matrix A: " << endl;
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
    {
      cin >> a[i][j];
    }
  cout << "Enter the number of rows and columns of matrix B: ";
  cin >> r >> c;
  cout << "Enter the elements of matrix B: " << endl;
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
    {
      cin >> b[i][j];
    }
  // Multiplying matrices a and b and
  // storing result in result matrix
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
    {
      mul[i][j] = 0;
      for (k = 0; k < c; ++k)
      {
        mul[i][j] += a[i][k] * b[k][j];
      }
    }
  // Displaying the multiplication of two matrices
  cout << endl
     << "Output Matrix: " << endl;
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
    {
      cout << mul[i][j] << " ";
      if (j == c - 1)
        cout << endl;
    }
  return 0;
}

6. 使用二进制数翻转一个整数

下面这段代码可以使用二进制数翻转一个整数:

#include <iostream>
using namespace std;
unsigned int reverseBits(unsigned int n)
{
  unsigned int rev = 0;
  while (n > 0)
  {
    rev <<= 1;
    if (n & 1 == 1)
      rev ^= 1;
    n >>= 1;
  }
  return rev;
}
int main()
{
  unsigned int n;
  cout << "Enter an integer: ";
  cin >> n;
  cout << "Reversed integer is: " << reverseBits(n) << endl;
  return 0;
}

这些有趣的C++编程代码片段展示了编程语言的灵活性和强大的功能。无论你是一位编程新手还是有经验的开发人员,这些代码都可以激发你对编程之美的热情。

  
  

评论区

请求出错了