一、📘《GESP二级备考10道拆数模拟题》
第1题:数字士兵报数(求各位数字之和)
1、🌈故事
(1)数字王国来了一队士兵:
345
(2)其实是:
3号士兵
4号士兵
5号士兵
(3)国王想知道:
👉 这个队伍,士兵编号加起来是多少?
3+4+5=12
2、🧠思路
每次拆出一个数字,加到累加器里,最后输出累加器的值。
3、💻参考代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int sum = 0;
while(n)
{
sum += n % 10;
n /= 10;
}
cout << sum;
}
第2题:数一数有多少士兵(数字个数)
1、🌈故事
(1)数字王国来了一队士兵:
7896
(2)士兵编号:
7号士兵
8号士兵
9号士兵
6号士兵
(3)国王想知道:
👉 有多少士兵?
(4)数一数,出结果
4
2、🧠思路
拆一次,计数器就加1次。
3、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int count = 0;
while(n)
{
count++;
n /= 10;
}
cout << count;
}
第3题:镜子魔法(反转数字)
1、🌈故事
数字照镜子:
123
变成
321
2、🧠思路
每次拼接:
新数 = 新数×10 + 当前位
3、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int rev = 0;
while(n)
{
rev = rev * 10 + n % 10;
n /= 10;
}
cout << rev;
}
第4题:寻找数字 7
1、🌈故事
国王喜欢数字7!
输入:
1725
发现有7 → 输出 YES
2、🧠思路
拆出数字,来判断,看看有没有7。
3、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
bool found = false;
while(n)
{
if(n % 10 == 7)
found = true;
n /= 10;
}
if(found)
cout<<"YES";
else
cout<<"NO";
}
第5题:找最大编号的士兵
1、🌈故事
(1)一队士兵:
4829
(2)最大编号:
9
2、🧠思路
拆出数字,打擂台,找最大的一个。
3、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int mx = 0;
while(n)
{
int d = n % 10;
if(d > mx)
mx = d;
n /= 10;
}
cout << mx;
}
第6题:找最小士兵
1、输入:
4829
2、输出:
2
最小的一个是2
3、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int mn = 9;
while(n)
{
int d = n % 10;
if(d < mn)
mn = d;
n /= 10;
}
cout << mn;
}
第7题:偶数士兵有多少
1、输入:
123456
2、偶数:
2 4 6
共3个
3、🧠思路
先拆数,再判断,符合条件再计数。
4、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int count = 0;
while(n)
{
if(n % 10 % 2 == 0)
count++;
n /= 10;
}
cout << count;
}
第8题:各位数字乘积
1、输入:
1234
2、输出:
24
3、因为:
1×2×3×4
4、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int mul = 1;
while(n)
{
mul *= n % 10;
n /= 10;
}
cout << mul;
}
第9题:回文数判断(重点)
1、🌈故事
(1)回文数:
121
像镜子一样
(2)非回文:
123
2、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int original = n;
int rev = 0;
while(n)
{
rev = rev * 10 + n % 10;
n /= 10;
}
if(original == rev)
cout<<"YES";
else
cout<<"NO";
}
第10题:统计数字0的个数(考试常考)
1、输入:
10020
2、输出:
3
3、💻代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int count = 0;
while(n)
{
if(n % 10 == 0)
count++;
n /= 10;
}
cout << count;
}
二、🎯拆数问题万能模板
while(n)
{
int d = n % 10;
//处理d
n /= 10;
}
三、🎯GESP二级考试最常考拆数题类型
各位数字和 ⭐⭐⭐⭐⭐
反转数字 ⭐⭐⭐⭐⭐
回文数 ⭐⭐⭐⭐⭐
数字个数 ⭐⭐⭐⭐
找某个数字 ⭐⭐⭐⭐
最大最小 ⭐⭐⭐⭐
偶数统计 ⭐⭐⭐
四、🎁记忆口诀
拆数字,很简单
%10取尾最关键
/10删除往前走
while循环到尽头
五、往届GESP二级拆数字真题:



1、参考程序(优美的数字):
#include <bits/stdc++.h>
using namespace std;
int n, ans;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int v = i % 10, t = i / 10, chk = 1;
while (t)
{
if (t % 10 != v) chk = 0;
t /= 10;
}
ans += chk;
}
printf("%d\\n", ans);
return 0;
}



2、参考程序(数位和):
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n; // 读入数字个数
int max_sum = 0; // 用来保存当前最大的数位和
for (int i = 0; i < n; i++) {
long long num;
cin >> num; // 读入一个数字
int digit_sum = 0;
// 拆解数字,逐位加和
while (num > 0) {
digit_sum += num % 10; // 取出当前个位
num /= 10; // 去掉当前个位
}
// 更新最大值
if (digit_sum > max_sum) {
max_sum = digit_sum;
}
}
cout << max_sum << endl; // 输出最终结果
return 0;
}




3、参考程序(数位之和):
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n; // 输入待判断的整数个数
// 循环处理 n 个数
for(int i = 1; i <= n; i++){
int x;
cin >> x; // 输入当前要判断的整数
int tot = 0; // 存储各位数字之和
// 拆分每一位数字并求和
while(x){
tot += (x % 10); // 加上最低位
x /= 10; // 去掉最低位
}
// 判断数位和是否是7的倍数
if(tot % 7 == 0)
cout << "Yes\\n";
else
cout << "No\\n";
}
return 0;
}



4、参考程序(计数):
#include <iostream>
using namespace std;
// 函数 check(x, y):统计一个整数 x 中有多少位是数字 y
int check(int x, int y) {
int cnt = 0; // 统计 y 出现的次数
while (x > 0) { // 逐位处理 x 中的每一位
int tmp = x % 10; // 提取当前最低位
if (tmp == y) { // 如果这一位等于 y
cnt++; // 统计次数加一
}
x = x / 10; // 去掉最低位,继续判断下一位
}
return cnt; // 返回在这个数中 y 出现的次数
}
int main() {
int n, k;
cin >> n >> k; // 输入 n 和 k
int ans = 0; // 最终统计结果
for (int i = 1; i <= n; i++) {
ans += check(i, k); // 对每个 1~n 的数字调用 check 累加结果
}
cout << ans << endl; // 输出最终统计次数
return 0;
}



5、参考程序(数字黑洞):
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
int count=0;
while(n!=495)
{
int a[3];
a[0]=n/100;
a[1]=n/10%10;
a[2]=n%10;
sort(a,a+3);
int min=a[0]*100+a[1]*10+a[2];
int max=a[2]*100+a[1]*10+a[0];
n=max-min;
count++;
}
cout<<count;
}
2023年6月GESP二级考试第二题:



6、参考程序(自幂数):
#include <iostream>
using namespace std;
int main() {
int m = 0;
cin >> m; // 输入要判断的数字个数
for (int i = 0; i < m; i++) {
int n = 0;
cin >> n; // 输入要判断的一个整数
// 第一步:计算 n 的位数 l
int t = n, l = 0;
while (t > 0) {
t /= 10;
l++; // 每除一次,说明多一位
}
// 第二步:计算每位数字的 l 次方之和 sum
int sum = 0;
t = n; // 重新还原 t = n
while (t > 0) {
int d = t % 10; // 取最低位
t /= 10; // 去掉最低位
// 计算 d 的 l 次方
int mul = 1;
for (int j = 0; j < l; j++)
mul *= d;
sum += mul;
}
// 第三步:判断是否为自幂数
if (sum == n)
cout << "T" << endl;
else
cout << "F" << endl;
}
return 0;
}
网硕互联帮助中心







评论前必须登录!
注册