C++之无子数
题目如下:

1 #include <iostream>
2
3 using namespace std;
4
5
6 bool isThisNumhaveChild(int num);
7
8
9 int main()
10 {
11 int begin_num1,end_num1,nochild_count1 = 0;
12 int begin_num2,end_num2,nochild_count2 = 0;
13 cin >> begin_num1 >> end_num1;
14 cin >> begin_num2 >> end_num2;
15 //分别调用这个函数
16 for(int i = begin_num1; i <= end_num1;i++)
17 {
18 bool flag = isThisNumhaveChild(i);
19 if(!flag)
20 {
21 nochild_count1++;
22 }
23 }
24 for(int i = begin_num2; i <= end_num2;i++)
25 {
26 bool flag = isThisNumhaveChild(i);
27 if(!flag)
28 {
29 nochild_count2++;
30 }
31 }
32 cout << "无子数是" << nochild_count1 << "个" << endl;
33 cout << "无子数是" << nochild_count2 << "个" << endl;
34 /*
35 int flag1 = isThisNumhaveChild(61);
36 int flag2 = isThisNumhaveChild(62);
37 int flag3 = isThisNumhaveChild(63);
38 int flag4 = isThisNumhaveChild(64);
39 int flag5 = isThisNumhaveChild(65);
40 cout << flag1 << endl;
41 cout << flag2 << endl;
42 cout << flag3 << endl;
43 cout << flag4 << endl;
44 cout << flag5 << endl;
45 */
46 }
47
48
49 bool isThisNumhaveChild(int num)
50 {
51 bool flag = false;
52 //i是D(x),i*num是X
53 int the_xnum;
54 //接收一下是第几个数让他成为有子数
55 int suppose_i;
56 for(int i = 1;i < 1000;i++)
57 {
58 int a,b,c,d,e = 0;//个十百千万
59 the_xnum = i*num;
60 if(the_xnum/10000 >= 1)
61 {//超过5位数 包括5位数
62 e = the_xnum/10000;
63 d = (the_xnum-e*10000)/1000;
64 c = (the_xnum-e*10000-d*1000)/100;
65 b = (the_xnum-e*10000-d*1000-c*100)/10;
66 a = (the_xnum-e*10000-d*1000-c*100 -b*10);
67 suppose_i = a + b + c + d + e;
68 }else if(the_xnum/1000 >= 1)
69 {//4位数
70
71 d = the_xnum/1000;
72 c = (the_xnum-d*1000)/100;
73 b = (the_xnum-d*1000-c*100)/10;
74 a = (the_xnum-d*1000-c*100 -b*10);
75 suppose_i = a + b + c + d;
76 }else if(the_xnum/100 >= 1)
77 {//3位数
78 c = the_xnum/100;
79 b = (the_xnum-c*100)/10;
80 a = (the_xnum-c*100 -b*10);
81 suppose_i = a + b + c;
82 }else if(the_xnum/10 >= 1)
83 {//2位数
84 b = the_xnum/10;
85 a = (the_xnum -b*10);
86 suppose_i = a + b;
87 }else
88 {//1位数
89 a = the_xnum;
90 suppose_i = a;
91 }
92
93 if(suppose_i == i)
94 {
95 //D(x) 和 应该D(X)的是一样的 break出来
96 //flag = i;
97 flag = true;
98 break;
99 }
100 }
101 return flag;
102 }
notes:
1.从line11到line33的代码冗余太多,且可修改性不高,如果要改成接收再多行的数据就捉襟见肘,待优化。
2.判断他是否有子的算法太简单了,时间复杂度大,算法待优化。
3.判断他是几位数时要注意不要忘记 = 了!!我就是这里错了结果多花了好多时间===
C++之无子数的更多相关文章
- thinkphp5控制器
// 定义应用目录 define('APP_PATH', __DIR__ . '/../app/'); // 定义配置文件目录和应用目录同级 define('CONF_PATH', __DIR__.' ...
- Echarts关于tree树数据渲染图例最新实例
最近做项目接到新的需求,根据本身系统结构数据做一个图形化展示,要求好看易用,有交互,就说了这么多,然后就要求两天给一版瞅瞅,MMP,真把前端当神了(你倒是把待遇提到神的地位啊...) 唉,吐槽归吐槽, ...
- UESTC 618 无平方因子数 ( 莫比乌斯)
UESTC 618 题意:求1到n中无平方因子数的个数 Sample Input 3 1 10 30 Sample Output 1 7 19 思路:与前面的BZOJ 2440相似 #inc ...
- cogs 2056. 无平方因子数
2056. 无平方因子数 ★☆ 输入文件:non.in 输出文件:non.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 给出正整数n,m,区间[n,m]内的无 ...
- Java实现 LeetCode 689 三个无重叠子数组的最大和(换方向筛选)
689. 三个无重叠子数组的最大和 给定数组 nums 由正整数组成,找到三个互不重叠的子数组的最大和. 每个子数组的长度为k,我们要使这3*k个项的和最大化. 返回每个区间起始索引的列表(索引从 0 ...
- [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- uestc 1720无平方因子数
求素数 然后容斥原理// n之内有平方因子的数的个数sum =n/(2^2) + n/(3^2)+……+n/(k^2) - n/(2^2 * 3^2)-……+……. // #pragma commen ...
- 最大连续子数组问题2-homework-02
1) 一维数组最大连续子数组 如第homework-01就是一维数组的最大子数组,而当其首位相接时,只需多考虑子数组穿过相接的那个数就行了! 2)二维数组 算法应该和第一次的相似,或者说是将二维转化为 ...
- CodeChef - SQRGOOD:Simplify the Square Root (求第N个含平方因子数)
Tiny Wong the chef used to be a mathematics teacher in a senior high school. At that time, he always ...
随机推荐
- (三)lamp环境搭建之编译安装php
1,PRC (People's republic of China) timezone中设置的时间为中国时间. 2,php的官方镜像源,使用linux时可以直接下载的 http://cn2.php.n ...
- ShardedJedisPipeline 源码分析
一.什么是pipeline?什么是ShardedJedis? 由于pipeline和ShardedJedis的介绍和源码分析在网上已经有了,本文就不再赘述,直接给出链接: pipeline的介绍: h ...
- SSH服务器拒绝了密码。请再试一次。怎么改都不行
使用Xshell连接服务器,之前还好好的,突然之间就报"SSH服务器拒绝了密码.请再试一次"的错误. 1.检查 检查了IP.连接端口.用户.密码.网络是否正确? 本机情况:能够pi ...
- hive 权限排查
show grant role role_username id username
- IntelliJ IDEA 的 Bean validation 里有什么用
IntelliJ IDEA 的 Bean validation 是指右侧的框. 平时都是缩起来的,今天心血来潮.研究下这个是干嘛的?怎么用. 三个按钮全按下的话,下面的项目就会有三个菜单可选项. C ...
- Java中禁止浏览器开启缓存的方法命令
响应头里添加禁止浏览器缓存的内容 Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0 其中,C ...
- FZU ICPC 2020 寒假训练 4 —— 模拟(一)
P1042 乒乓球 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华 ...
- [loj3364]植物比较
结论:设$b_{i}$满足该限制,则$a_{i}$合法当且仅当$\forall i\ne j,a_{i}\ne a_{j}$且$\forall |i-j|<k,[a_{i}<a_{j}]= ...
- 洛谷 P4484 - [BJWC2018]最长上升子序列(状压 dp+打表)
洛谷题面传送门 首先看到 LIS 我们可以想到它的 \(\infty\) 种求法(bushi),但是对于此题而言,既然题目出这样一个数据范围,硬要暴搜过去也不太现实,因此我们需想到用某种奇奇怪怪的方式 ...
- Codeforces 1392H - ZS Shuffles Cards(DP+打表找规律)
Codeforces 题面传送门 & 洛谷题面传送门 真·两天前刚做过这场的 I 题,今天模拟赛就考了这场的 H 题,我怕不是预言带师 提供一种奇怪的做法,来自于同机房神仙们,该做法不需要 M ...