GESP 2级 2026年6月编程题题解
2026-06-30 17:33:33
发布于:福建
第一题:完全平方数
代码如下
#include<bits/stdc++.h>
using namespace std;
bool i2(int i){
if(i==1){
return 0;//1是完全平方数
}
int sqrt_i=(int)sqrt(i)+0.5;//+0.5防止微小误差 sqrt返回值是小数,要用(int)
if(sqrt_i*sqrt_i==i){ //判断是否是完全平方数 如3*3=9,2*2=4等
return 1;
}else{
return 0;//不等于就不是了 如4*4!=15 3*3!=8等
}
}
int main(){
int n,m;
cin>>n>>m;
int cnt=0;
for(int i=n;i<=m;i++){
if(i2(i)==1){//调用i2函数判断是否是完全平方数
cnt++;
}
}
cout<<cnt;
return 0;
}
第二题:打印菱形
本题我打算用分段的形式来解
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
// ========== 第一段:上半部分 n 行 ==========
for (int i = 0; i < n; i++)
{
// 前置空格
for (int k = 0; k < n - 1 - i; k++)
cout << " ";
cout << "+";
// 中间空格
for (int m = 0; m < 2 * i - 1; m++)
cout << " ";
// 只有不是第一行,才输出右边的+
if (i != 0)
cout << "+";
cout << endl;
}
// ========== 第二段:下半部分 n-1 行 ==========
for (int i = n - 2; i >= 0; i--)
{
// 前置空格
for (int k = 0; k < n - 1 - i; k++)
cout << " ";
cout << "+";
// 中间空格
for (int m = 0; m < 2 * i - 1; m++)
cout << " ";
if (i != 0)
cout << "+";
cout << endl;
}
return 0;
}
因为6月27日我上过考场,当时分数直接公布好吧
| 编程题第1题 | 编程题第二题 |
|---|---|
| 25分 | 25分 |
这里空空如也






















有帮助,赞一个