holyya.com
2025-09-04 08:09:14 Thursday
登录
文章检索 我的文章 写文章
蓝桥杯C++初赛试题及答案
2023-07-05 00:24:37 深夜i     --     --
蓝桥杯 C++初赛试题 答案

近日,蓝桥杯C++初赛试题和答案被公布,引起了广泛的关注和讨论。蓝桥杯作为国内最具影响力的计算机竞赛之一,每年吸引着数百万计算机技术爱好者参加,而初赛则是选拔优秀选手进入复赛的重要一环。

据悉,蓝桥杯C++初赛试题主要涉及C++编程语言的相关知识,其中包括数据类型、流程控制、数组、函数等基本概念,以及指针、结构体、类等进阶内容。试题难度适中,考查了选手的编程能力和解题思路。

下面是部分试题和参考答案:

1. 输入一个字符串,输出其中字母的个数和数字的个数,要求分别使用两个函数实现。

解题思路:使用两个函数分别对字母和数字进行统计,并返回结果即可。代码如下:


#include <iostream>

#include <cstring>

using namespace std;

int countLetter(char str[]) {

  int count = 0;

  for (int i = 0; i < strlen(str); i++) {

    if (isalpha(str[i])) {

      count++;

    }

  }

  return count;

}

int countNumber(char str[]) {

  int count = 0;

  for (int i = 0; i < strlen(str); i++) {

    if (isdigit(str[i])) {

      count++;

    }

  }

  return count;

}

int main() {

  char str[100];

  cin >> str;

  cout << countLetter(str) << " " << countNumber(str) << endl;

  return 0;

}

2. 将一个二进制数转换为十进制数。

解题思路:依次对每个位的值进行累加即可。代码如下:


#include <iostream>

#include <cstring>

using namespace std;

int binaryToDecimal(char binary[]) {

  int decimal = 0, base = 1;

  for (int i = strlen(binary) - 1; i >= 0; i--) {

    decimal += (binary[i] - '0') * base;

    base *= 2;

  }

  return decimal;

}

int main() {

  char binary[30];

  cin >> binary;

  cout << binaryToDecimal(binary) << endl;

  return 0;

}

以上是部分试题和参考答案,希望对大家的学习有所帮助。同时也希望广大选手在备战蓝桥杯的过程中,继续努力,开拓创新,为国家的科技进步做出更大的贡献。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章