21xrx.com
2025-04-11 16:49:53 Friday
文章检索 我的文章 写文章
"C++入门基础题目"
2023-07-05 12:00:24 深夜i     4     0
C++ 入门 基础 题目 练习题

C++是一种广泛使用的计算机编程语言,它是一种通用的编程语言,可以用于构建各种类型的应用程序,从简单的命令行程序到复杂的图形用户界面应用程序。在学习C++编程的过程中,掌握基础的语法和算法是非常重要的。这里提供一些C++入门基础题目,帮助初学者熟悉关键的编程概念和算法。

1. Hello World 程序

输出一条信息“Hello World”到控制台。

#include <iostream>
using namespace std;
int main()
  cout << "Hello World!" << endl;
  return 0;

2. 加法计算器

输入两个数字并输出它们之和。

#include <iostream>
using namespace std;
int main()
{
  int a, b, sum;
  cout << "Enter the first number: ";
  cin >> a;
  cout << "Enter the second number: ";
  cin >> b;
  sum = a + b;
  cout << "The sum is: " << sum << endl;
  return 0;
}

3. 阶乘计算器

输入一个数字,计算并输出它的阶乘。

#include <iostream>
using namespace std;
int main()
{
  int n, fac = 1;
  cout << "Enter a number: ";
  cin >> n;
  for(int i=1; i<=n; i++)
  {
    fac = fac * i;
  }
  cout << n << "! = " << fac << endl;
  return 0;
}

4. 判断奇偶性

输入一个数字,判断它是偶数还是奇数。

#include <iostream>
using namespace std;
int main()
{
  int n;
  cout << "Enter a number: ";
  cin >> n;
  if(n%2==0)
  
    cout << n << " is even." << endl;
  
  else
  
    cout << n << " is odd." << endl;
  
  return 0;
}

5. 计算最大公约数

输入两个数字,计算它们的最大公约数并输出。

#include <iostream>
using namespace std;
int main()
{
  int a, b, gcd;
  cout << "Enter the first number: ";
  cin >> a;
  cout << "Enter the second number: ";
  cin >> b;
  for(int i=1; i<=a && i<=b; i++)
  {
    if(a%i==0 && b%i==0)
    
      gcd = i;
    
  }
  cout << "The gcd of " << a << " and " << b << " is: " << gcd << endl;
  return 0;
}

这些题目不仅涵盖了C++基础的语法和算法,而且涉及了常见的编程问题。通过解决这些题目,初学者可以更好地理解C++编程语言和算法,提高编程能力和应用能力。同时,这些题目也是C++编程面试的常见问题,解决这些问题也有助于提高应聘者职位的竞争力。

  
  

评论区