解法一(暴力获取):

#include<stdio.h>
#include<stdlib.h> int main(void)
{
int n;
int times = ;
int num = ; printf("输入整数n: ");
scanf("%d", &n); for(int i = ; i <= n; i++)
{
for(int temp = i; temp > ; temp = temp / )
{
if(temp % == || temp % == || temp % == || temp % == )
{
times++;
num += i;
break;
}
}
} printf("%d\n", times);
printf("%d\n", num); printf("\n");
system("pause");
}

解法二:

解题计算思路

实现代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h> int get_level(int n) // 获取该整数的级别, <10, <100, ....
{
int n_level = ; while(n)
{
n_level++;
n /= ;
} return n_level;
} int get_sum(int level) // 获取 1+ 2+ 3 + 4 + ... 的值
{
int sum = ;
for(int i = ; i < pow(10.0, level - ); i++)
{
sum += i;
}
return sum;
} int main(void)
{
long long **info;
// info[i][j]
// j 代表相应位数的信息, 例如 j = 1代表个位数级, j = 2代表十位数级
// i = 0 存放着总数量和总和
// i = 1 代表对应位数中符合条件的数字的数量
// i = 2 代表对应位数中符合条件的数字的和
int n;
int n_level;
int special_number[] = {, , , }; // 存放那些特殊的值
//获取 1+ 2+ 3 + 4 + ... + 9 的值
int sum; // 存储 1+ 2+ 3 + 4 + ... 的总和值 printf("输入整数n: ");
scanf("%d", &n); n_level = get_level(n); // 建立相应的二维数组
info = (long long **)malloc( * sizeof(long long *));
for(int i = ; i <= ; i++)
{
info[i] = (long long *)malloc((n_level + ) * sizeof(long long));
// 初始化其值为 0
memset(info[i], , (n_level + ) * sizeof(long long));
} // 数值累计
for(int temp_level = ; temp_level <= n_level; temp_level++)
{
sum = get_sum(temp_level);
if(temp_level == )
{
for(int i = ; special_number[i] <= n && i < ; i++)
{//依次加 0, 1, 2, 9
info[][temp_level]++;
info[][temp_level] += special_number[i];
}
}
else{
for(int i = ; i * pow(10.0, temp_level - ) <= n && i < ; i++)
{
if(((i + ) * pow(10.0, temp_level - ) < n) && (i == || i == || i == ))
{// 符合条件时,将该位所有数值都加起来
// 将数值拆开 先计算最高位数上的值的和,再计算除了最高位数上其他的数值和
info[][temp_level] += pow(10.0, temp_level - );
info[][temp_level] += sum + i * pow(10.0, temp_level - ) * pow(10.0, temp_level - );
}
else if((i + ) * pow(10.0, temp_level - ) < n)
{
if(temp_level == )
{
info[][temp_level] += info[][temp_level - ];
info[][temp_level] += info[][temp_level - ] + i * pow(10.0, temp_level - ) * info[][temp_level - ];
}
else
{
info[][temp_level] += info[][temp_level - ] + pow(10.0, temp_level - );
info[][temp_level] += info[][temp_level - ] + i * pow(10.0, temp_level - ) * (info[][temp_level - ] + pow(10.0, temp_level - )) + get_sum(temp_level - );
}
}
else
{// 逐个加
for(int temp_n = i * pow(10.0, temp_level - ); temp_n <= n && temp_n < (i + ) * pow(10.0, temp_level - ); temp_n++)
{
for(int temp = temp_n; temp > ; temp = temp / )
{
if(temp % == || temp % == || temp % == || temp % == )
{
info[][temp_level]++;
info[][temp_level] += temp_n;
break;
}
}
}
}
}
}
} // 将所有总和加起来
for(int temp_level = n_level; temp_level > ; temp_level--)
{
info[][] += info[][temp_level];
info[][] += info[][temp_level];
} // 输出总和结果
printf("%d\n", info[][] - );
printf("%d\n", info[][]); printf("\n");
system("pause");
}

第十届蓝桥杯大赛-特别数的和-C++的更多相关文章

  1. 2019 第十届蓝桥杯大赛软件类省赛 Java A组 题解

    2019 第十届蓝桥杯大赛软件类省赛 Java A组 试题A 题解 ​ 题目最后一句贴心的提示选手应该使用 long (C/C++ 应该使用 long long). ​ 本题思路很直白,两重循环.外层 ...

  2. 第十届蓝桥杯大赛软件类省赛C/C++研究生组 试题I:灵能传输

    在游戏<星际争霸 II>中,高阶圣堂武士作为星灵的重要 AOE 单位,在游戏的中后期发挥着重要的作用,其技能"灵能风暴"可以消耗大量的灵能对一片区域内的敌军造成毁灭性的 ...

  3. 第十届蓝桥杯2019年C/C++ 大学B组省赛试题

    2019年第十届蓝桥杯大赛软件类省赛C/C++大学B组 试题 A:组队 本题总分:5分 [问题描述] 作为篮球队教练,你需要从以下名单中选出 1号位至 5号位各一名球员, 组成球队的首发阵容. 每位球 ...

  4. 2019第十届蓝桥杯C++B组题解(赛后重写的,不确保答案正确性,仅供参考)

    先说一下这次的感受吧,我们考场比较乱,开始比赛了,还有的电脑有故障,(向这些人发出同情),第一次认真参加比赛,真正比赛的时候感觉没有那么正式,很乱,各种小问题,(例如博主就没找到题目在哪里,找到后又不 ...

  5. 2019年第十届蓝桥杯【C++省赛B组】

    试题 A: 组队 本题总分:5 分 作为篮球队教练,你需要从以下名单中选出 1 号位至 5 号位各一名球员,组成球队的首发阵容.每位球员担任 1 号位至 5 号位时的评分如下表所示.请你计算首发阵容 ...

  6. 第十届蓝桥杯JavaC组省赛真题

    试题 A: 求和 本题总分:5 分 [问题描述] 小明对数位中含有 2.0.1.9 的数字很感兴趣,在 1 到 40 中这样的数包 括 1.2.9.10 至 32.39 和 40,共 28 个,他们的 ...

  7. Java实现第十届蓝桥杯旋转

    试题 F: 旋转 时间限制: 1.0s 内存限制: 512.0MB 本题总分:15 分 [问题描述] 图片旋转是对图片最简单的处理方式之一,在本题中,你需要对图片顺时 针旋转 90 度. 我们用一个 ...

  8. Java实现第十届蓝桥杯JavaC组第十题(试题J)扫地机器人

    扫地机器人 时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分 [问题描述] 小明公司的办公区有一条长长的走廊,由 N 个方格区域组成,如下图所 示. 走廊内部署了 K 台扫地机器人 ...

  9. Java实现第十届蓝桥杯外卖店优先级

    试题 G: 外卖店优先级 时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分 [问题描述] "饱了么"外卖系统中维护着 N 家外卖店,编号 1 ∼ N.每家外卖店 ...

随机推荐

  1. 小程序SetData

  2. MySQL中列别名为中文时,Order by 子句中使用别名时不要加引号

    暂时还不清楚原因 1.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SC表: 这里,当做总成绩处理 select sid, sum(score) as '总成绩', avg(score) ...

  3. 3-开发共享版APP(搭建指南)-修改手机验证码

    https://www.cnblogs.com/yangfengwu/p/11273743.html 请先看数据篇 或者参考 https://www.cnblogs.com/yangfengwu/p/ ...

  4. 网卡可以绑定cpu提高吞吐量

    请看大神帖子:https://blog.csdn.net/nawenqiang/article/details/82854929 需要做什么呢? 首先,确认你是否运行irqbalance,这个是nic ...

  5. map访问key不存在的情况下,用find。比[]直接访问的意思不一样,map[key]不返null

    key不存在的话则创建一个pair并调用默认构造函数 map<CGuid, CLibItem>::iterator iterItem = m_world->m_library_sce ...

  6. 回滚事件只是让原数据看起来不变,但是id还是会自增吗?

    回滚事件只是让原数据看起来不变,但是id还是会自增对吗? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  7. kubernetes --- weave

    #wget 'https://cloud.weave.works/launch/k8s/weavescope.yaml?k8s-service-type=NodePort&k8s-versio ...

  8. APISIX系列 | 使用 docker-apisix 安装APISIX服务

    官方仓库:https://github.com/iresty/docker-apisix 官方仓库 拉取 git clone git@github.com:iresty/docker-apisix.g ...

  9. CatBoost使用GPU实现决策树的快速梯度提升CatBoost Enables Fast Gradient Boosting on Decision Trees Using GPUs

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...

  10. [原]使用global mapper 修改影像数据DOM的投影变换(将数据转换成osgearth支持的投影)

    osgearth默认使用的投影基准面为: Geographic(Latitude/Longitude)的 WGS84 有这样一份数据需要修改: 1.在菜单栏种选择“工具”---->“配置” 2. ...