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 ...
随机推荐
- 【http】https加速优化
目录 前言 HTTPS 的连接很慢 https 步骤简要划分 握手耗时 证书验证 CRL OCSP 硬件优化 软件优化 软件升级 协议优化 证书优化 会话复用 会话票证 预共享密钥 前言 主要记录 h ...
- 使用Visual Studio 2019将ASP.NET Core发布为linux-arm64程序
前言 前段时间入手了一台树莓派4B,一直闲置未使用,最近工作需要,要在上面跑下.NET Core程序,由于树莓派4B使用的是ARM架构,并且支持64位操作系统,为了充分发挥树莓派性能,我的这台树莓派安 ...
- 远程设备管理opendx平台搭建-server,agent以及front实际搭建
本系列文章讲述的是一个系列的第二部分,最终可以搭建一整套设备远程管理平台,与stf不同的是,opendx搭建较为简单,而且由于底层是appium来支持的,所以,较容易支持ios,也容易支持更高版本的安 ...
- vue+element初始化创建项目
初始化 步骤1:选择开发框架并创建 步骤1:vue create shop 回车步骤2:安装方式选择第二个自定义步骤3:安装模块: (*) Babel ( ) TypeScript ( ) Pro ...
- 查看Git提交的代码统计
1,提交Top5: git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5 2,某用户提交的代码统计 git log ...
- C#与dotNET项目想要另存为一个新项目sln文件丢了怎么办
如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到. 很简单的一个思路是反正sln是多余的 ...
- Arduino uno r3 使用 ESP8266 UART-WiFi 透传模块
一.所需硬件材料 1.ESP8266:01s某宝上3.5块钱 2.杜邦线:某宝几块钱一组40P,这里只需要三根,用于连接 树莓派与继电器 3.烧录器 二.ESP8266 AT固件烧录 ESP8266主 ...
- [GYCTF2020]Easyphp
知识点 反序列化pop链 反序列化字符逃逸 解题过程 www.zip 备份文件获取源码 审计代码构造pop链 <?php Class UpdateHelper{ public $id; publ ...
- 3、使用ListOperations操作redis(List列表)
文章来源:https://www.cnblogs.com/shiguotao-com/p/10560354.html 方法 c参数 s说明 List<V> range(K key, l ...
- myeclipse maven web打包
1.在当前的项目pom.xml的文件上,如下图所示:鼠标右键->run As->Maven Build...