获取一个数的每一位数
1,数字转换为字符串获取其每一位
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
char a[10000];
int main()
{int n;cin >> n;string s;s = to_string(n);int l = s.size();for (int i = 0; i < l; i++){a[i] = s[i];}for (int i = 0; i < l; i++)cout << a[i] << " ";return 0;
}
2,循环整除加取余;
#include<bits/stdc++.h>
using namespace std;int main()
{int num;int i = 0;cin>>num;while(num>0){i = num % 10;//计算每一位上的数字printf("%d\n", i);//打印每一位数字num = num / 10;//实现位与位之间的遍历}return 0;
}
循环条件改为n>0;
#include<bits/stdc++.h>
using namespace std;int a[10000];int main()
{int n;int s;int i=0;cin>>n;while(n/10){a[i++]=n%10;n/=10;}a[i]=n%10;//取到首位时循环已经跳出;for(int j=i;j>=0;j--)cout<<a[j]<<" "; return 0;
}
3,若位数可知用整除法从前向后取,为正序。
Despite the glorious fa ll colors in the midwest, there is a great deal of time to spend while on a train from St. Louis to Chicago. On a recent trip, we passed some time with the following game.
We start with a positive integer S. So long as it has more than one digit, we compute the product of its digits and repeat. For example, if starting with 95, we compute 9 × 5 = 45. Since 45 has more than one digit, we compute 4 × 5 = 20. Continuing with 20, we compute2 × 0 = 0. Having reached 0, which is a single-digit number, the game is over.
As a second example, if we begin with 396, we get the following computations:
3 × 9 × 6 = 162
1 × 6 × 2 = 12
1 × 2 = 2
and we stop the game having reached 2.
代码:
#include<bits/stdc++.h>
using namespace std;void ffo(int n)//数为个位前进行循环;
{int r;while(n)
{
int t = n;
int s = 1;while(t){r = t%10;s*=r;t/=10; } n = s;if(n/10==0){cout<<" "<<n;break;}cout<<" "<<s; }
}int main()
{
int n;
while(cin>>n)
{ if(n==0)break;cout<<n;if(n>=10)
{ffo(n);
}
cout<<endl;}
return 0;
}
获取一个数的每一位数相关推荐
- 获取两个数百分比的值
1 /** 2 * 获取两个数百分比的值 3 * 4 * @param num1 5 * @param num2 6 * @param retain 保留小数位数 7 * @return 8 */ 9 ...
- pandas使用query函数和sample函数、使用query函数筛选dataframe中的特定数据行并使用sample函数获取指定个数的随机抽样数据
pandas使用query函数和sample函数.使用query函数筛选dataframe中的特定数据行并使用sample函数获取指定个数的随机抽样数据(query dataframe and ran ...
- jquery的实时触发事件(textarea实时获取中文个数)
jquery的实时触发事件(textarea实时获取中文个数) (2014-09-16 11:49:50) 转载▼ 标签: 实时触发事件 中文个数 onpropertychange oninput o ...
- python统计字符在文件中出现的次数_python字符串中字符出现次数(python获取字符串个数)...
今天要说的是Python的字符串函数还是很方便的,只需要调用 count() 方法即可. 最近,我在一个网站上看到了一个自然语言处理课程.我详细解释了一些方法的使用,用例很容易理解,所以我想通过我的博 ...
- php获取字符串商都_php strlen获取字符串字节长度和mb_strlen获取字符串个数长度的区别(strlen获取中文长度)...
strlen获取字符串字节长度和mb_strlen获取字符串个数长度的区别,如果字符串是数字或者英文字母组成的话,它们2个的结果上体现不出区别,可是如果字符串是汉字组成的话它们2个的结果差异很大 重点 ...
- 随机生成1024个数,存入一段内存,用指针实现获取1024个数的最大数地址,最小数地址
http://blog.csdn.net/itcastcpp//details/39277193 题目:随机生成1024个数,存入一段内存,用指针实现获取1024个数的最大数地址,最小数地址,具体实现 ...
- 输入5个整数,找出5个数中的两位数
输入: 输入5个整数,找出5个数中的两位数,并输出,并输出两位数的平均数,如果没有两位数就不输出.用while 解决 5 6 20 9 10 输出: 20 10 15
- python_获取两个数,打印中间值
python_获取两个数,打印中间值 """ 获取一个开始值,再获取一个结束值.打印中间值5 10 --> 6 7 8 910 5 --> 9 8 7 6 & ...
- 获取三个数的中间值 宏
日常遇到较多的时间使用都是比较两个数的最大值 或者最小值,这两个处理过程很简单,都可以使用 宏MAX/MIN 来计算. 今天碰到获取三个数的中间值使用情况,花费些时间思考,将个人思考的两种方法进行总结 ...
最新文章
- mysql多字段数据统计_超详细的mysql元数据sql统计--information_schema
- Python超详细的字符串用法大全
- 25个必须记住的SSH命令
- 代码生成器的存在价值 选择自 mechiland 的 Blog
- php excelreader 中文,如何解决php excel reader导出excel中文乱码?
- 兴图视频指挥系统如何配置服务器,兴图新科指挥调度终端
- tar [-zxcvfpP]语法
- axis1 c# 接口 调用_java axis 调用C# webService的例子
- 通过AccessKey调用阿里云CDN接口刷新CDN资源案例
- 网管日志-06.07.13
- redis 获取类似key的所i有的值_高可用 Redis 分布式锁实施方案!
- 麒麟V10系统密码策略修改
- 服务器内存条故障显示器,内存故障与分析
- Hulu热招 | 用户身份认证团队
- configure配置调试
- 解决CSS样式重复定义带来的冲突问题
- C语言中“.”与-的区别
- 人均劳动生产率的单位_国家统计局解读2016年工资数据:与劳动生产率基本同步增长...
- 【Unity】 Unity设置材质的渲染模式(RenderingMode)
- 博图程序的归档和恢复
热门文章
- 康涅狄格大学计算机科学排名,学校排名|Rankings
- MongoDB--- 客户端操作 与 复制集
- [Vue]如何实现一个简单的表格数据筛选查找 (根据ElementUI表格展示)(数组筛选)
- R/RStudio 常用函数
- Https单向认证和双向认证
- 利用Excel和ren命令批量重命名文件技巧
- Javascript(JS) leetcode 796. 旋转字符串
- 查询NBiot模块下发的指令,提取有效部分的数据进行判断
- 计算机毕业设计(附源码)python裕民镇养老院信息管理系统
- Photoshop教程_ps样式怎么导入?