21xrx.com
2024-09-20 05:37:48 Friday
登录
文章检索 我的文章 写文章
C/C++笔试练习题目
2023-07-04 22:23:37 深夜i     --     --
C语言题目 C++语言题目 笔试练习 编程题目 算法练习

C/C++是计算机科学专业必修的编程语言之一,也是许多企业招聘计算机编程岗位的必备技能。为了提高自己的C/C++编程能力,学生们需要经常进行练习和笔试。

以下是一些C/C++笔试练习题目,可以作为学习和练习的参考:

1. 打印一个菱形图案


#include<iostream>

using namespace std;

int main()

{

  for(int i=1;i<=5;i++)

  {

    for(int j=1;j<=5-i;j++)

      cout<<" ";

    for(int j=1;j<=2*i-1;j++)

      cout<<"*";

    cout<<endl;

  }

  for(int i=4;i>=1;i--)

  {

    for(int j=1;j<=5-i;j++)

      cout<<" ";

    for(int j=1;j<=2*i-1;j++)

      cout<<"*";

    cout<<endl;

  }

  return 0;

}

2. 判断一个数是质数还是合数


#include<iostream>

using namespace std;

int main()

{

  int n;

  cin>>n;

  bool flag=true;

  for(int i=2;i<n;i++)

  {

    if(n%i==0)

    

      flag=false;

      break;

    

  }

  if(flag==true)

    cout<<n<<"是质数"<<endl;

  else

    cout<<n<<"是合数"<<endl;

  return 0;

}

3. 实现链表的反转


#include<iostream>

using namespace std;

struct Node{

  int val;

  Node *next;

};

Node *reverseList(Node *head)

{

  Node *pre=NULL;

  Node *cur=head;

  while(cur!=NULL)

  {

    Node *next=cur->next;

    cur->next=pre;

    pre=cur;

    cur=next;

  }

  return pre;

}

int main()

{

  Node *head=new Node(1);

  Node *node1=new Node(NULL);

  Node *node2=new Node(NULL);

  head->next=node1;

  node1->next=node2;

  Node *newhead=reverseList(head);

  while(newhead!=NULL)

  

    cout<<newhead->val<<" ";

    newhead=newhead->next;

  

  return 0;

}

4. 找到字符串中第一个不重复的字符


#include<iostream>

using namespace std;

char firstNotRepeatingChar(string str)

{

  int cnt[26]={0};

  for(int i=0;i<str.size();i++)

    cnt[str[i]-'a']++;

  for(int i=0;i<str.size();i++)

    if(cnt[str[i]-'a']==1)

      return str[i];

  return ' ';

}

int main()

{

  string str="leetcode";

  char res=firstNotRepeatingChar(str);

  cout<<res<<endl;

  return 0;

}

以上是四道C/C++笔试练习题目的解答,希望对初学者有所帮助,也希望大家能够不断练习和提高编程能力。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复