c - 逆序/正序输出每位.
#include <stdio.h>
#include <math.h> /*
判断一个正整数的位数,并按正序,逆序输出他们的位.
*/ int
invert(int); void
order(int, int); int
main(void) {
int n = ;
printf("original:%d\n", n);
int bitCont = invert(n);
printf("\nbits: %d\n", bitCont);
order(n, bitCont);
return ;
} //逆序输出每一位并记录位数.
int
invert(int n) {
int bitCount = ; //记录位数.
do
{
++bitCount;
printf("%d ", n % ); //逆序输出每一位.
n /= ;
} while (n); //终止条件是"n为0".
return bitCount;
} //正序输出每一位.
void
order(int n, int bitCount) {
int tmp = ; //存储即将减去的级别,如当n为"1234"时,存储"1000";当n为"234"时,存储"100".
int h = ; //存储最高位的位.
while(n) {
tmp = (int)pow((double), --bitCount);
h = n / tmp;
printf("%d ", h);
n -= h * tmp;
}
}
output:
original:56789
9 8 7 6 5
bits: 5
5 6 7 8 9 请按任意键继续. . .
//二,使用(求余数方法)
void
count_vMod(int input) {
//分别表示'个位,十位,百位,千位,万位'上的数字.
int one, ten, hundred, thousand, ten_thousand; ten_thousand = input / ; //除1万.
thousand = input % / ; //取余1万后除1千.
hundred = input % / ; //取余1千后除1百.
ten = input % / ; //取余1百后除1十.
one = input % ; if(ten_thousand)
printf("共有5位数,%d %d %d %d %d\n", one, ten, hundred, thousand, ten_thousand);
else if(!ten_thousand && thousand)
printf("共有4位数,%d %d %d %d\n", one, ten, hundred, thousand);
else if(!ten_thousand && !thousand && hundred)
printf("共有3位数,%d %d %d\n", one, ten, hundred);
else if(!ten_thousand && !thousand && !hundred && ten)
printf("共有2位数,%d %d\n", one, ten);
else if(!ten_thousand && !thousand && !hundred && !ten && one)
printf("共有1位数,%d\n", one);
}
虽然第二中方法(求余数)在求某位数的时候,有用,但是感觉比较笨拙(主要是对这个题目,特别是if判断时).
c - 逆序/正序输出每位.的更多相关文章
- C#数组的排序(正序逆序)
C#数组的排序(正序逆序) 这种排序 超级简单的 ! using System; using System.Collections.Generic; using System.Linq; using ...
- oracle 正序 逆序 排序查询
正序:从小到大 order by t.id asc 逆序:从大到小 order by t.id desc
- C语言-正序输出一个一个多位数
//正序输出一个多位数,所有的数字中间用空格分隔 int main() { ;//是可变化的 ; int d; int t =x; //先计算x的位数 ){ t /= ; mask *=; } pri ...
- Comparable 接口学习:对对象List进行比较和排序(正序和逆序)
Comparable 接口只有一个 int compareTo(T o) 方法 1.int compareTo(T o) 方法 方法说明: 比较此对象和规定的对象,如果此对象大于,等于,小于规定对象, ...
- Python九九乘法表(正序和逆序)
正序: for i in range(1,10): for j in range(1,i+1): print(str(i)+"*"+str(j)+"="+str ...
- Java之选择排序(正序、逆序)
public class SelectSort { public static void main(String[] args) { /** * @author JadeXu * @// TODO: ...
- (C++)读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔)
1 /* 2 程序功能:读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔). 3 例如:当输入985这个数字时,显示如下信息: 4 985是一个3位数字! ...
- compare正序与逆序
//list:在数据查询出来的Record集合 //juli:是需要比较的字段 //实现一个Comparator接口 //后面减去前面是正序 前面减去后面是倒叙 //我这里做的一个距离排序 R ...
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...
随机推荐
- Flume笔记--示例(使用配置文件)
例子参考资料:http://www.aboutyun.com/thread-8917-1-1.html 自定义sink实现和属性注入:http://www.coderli.com/flume-ng-s ...
- 更改xcode上iphone模拟器颜色的方法--备用
到模拟器的目录下修改图片即可——在Finder中显示,显示模拟器包内容,修改Contents/Resources/frame.png图片!
- 转:CFile.Open()的使用说明
在程中碰到这个一段代码: 讲的是CFILE类的文件操作,故参考MSDN系统学习一下(翻译了一下英文): CFile file;CFileException fe; //打开文件if(!file.Ope ...
- spark1.1.0源码阅读-executor
1. executor上执行launchTask def launchTask( context: ExecutorBackend, taskId: Long, taskName: String, s ...
- Android系统的进程分类
1.前台进程:即当前正在前台运行的进程,说明用户当前正在与通过该进程与系统进行交互,所以该进程为最重要的进程,除非系统的内容已经到不堪重负的情况,否则系统是不会将改进程终止的.2.可见进程:一般还是显 ...
- vector,list和deque区别
stl提供了三个最基本的容器:vector,list,deque. vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即[]操作符,但由 ...
- Java: for(;;) vs. while(true)
What is the difference between a standard while(true) loop and for(;;)? Is there any, or will both b ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- linux中断处理程序
Linux进行中断处理的4个步骤: 1.当中断产生,跳到统一入口IRQ_SVC 2.获取中断号 3.根据中断号找到irq_desc结构 4.从irq_desc结构中取出事先注册好的中断处理函数 Lin ...
- hackerrank【Lego Blocks】:计数类dp
题目大意: 修一个层数为n,长度为m的墙,每一层可以由长度为1.2.3.4的砖块构成. 每一层都在同一个长度处出现缝隙是方案非法的,问合法的方案数有多少种 思路: 先求出总方案,再减去所有非法的方案数 ...