#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 - 逆序/正序输出每位.的更多相关文章

  1. C#数组的排序(正序逆序)

    C#数组的排序(正序逆序) 这种排序 超级简单的 ! using System; using System.Collections.Generic; using System.Linq; using ...

  2. oracle 正序 逆序 排序查询

    正序:从小到大 order by t.id asc 逆序:从大到小 order by t.id desc

  3. C语言-正序输出一个一个多位数

    //正序输出一个多位数,所有的数字中间用空格分隔 int main() { ;//是可变化的 ; int d; int t =x; //先计算x的位数 ){ t /= ; mask *=; } pri ...

  4. Comparable 接口学习:对对象List进行比较和排序(正序和逆序)

    Comparable 接口只有一个 int compareTo(T o) 方法 1.int compareTo(T o) 方法 方法说明: 比较此对象和规定的对象,如果此对象大于,等于,小于规定对象, ...

  5. Python九九乘法表(正序和逆序)

    正序: for i in range(1,10): for j in range(1,i+1): print(str(i)+"*"+str(j)+"="+str ...

  6. Java之选择排序(正序、逆序)

    public class SelectSort { public static void main(String[] args) { /** * @author JadeXu * @// TODO: ...

  7. (C++)读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔)

    1 /* 2 程序功能:读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔). 3 例如:当输入985这个数字时,显示如下信息: 4 985是一个3位数字! ...

  8. compare正序与逆序

    //list:在数据查询出来的Record集合 //juli:是需要比较的字段   //实现一个Comparator接口 //后面减去前面是正序   前面减去后面是倒叙 //我这里做的一个距离排序 R ...

  9. Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法

    1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...

随机推荐

  1. 给C++初学者的50个忠告(好文转载)

    给C++初学者的50个忠告       1.把C++当成一门新的语言学习(和C没啥关系!真的.):  2.看<Thinking In C++>,不要看<C++变成死相>: 3. ...

  2. google protocol buffer -2-.proto 定义规则

    essage为主要关键字,类似于java中的class.定义简单message类型 SearchRequest.proto定义了每个查询请求的消息格式,每个请求都会有查询关键词query,查询结果的页 ...

  3. 转:十条不错的编程观点。(出处:酷 壳 – CoolShell.cn)

    在Stack Overflow上有这样的一个贴子<What’s your most controversial programming opinion?>,翻译成中文就是“你认为最有争议的 ...

  4. keybd_event 转载

    转自 http://apps.hi.baidu.com/share/detail/14468670 Option Explicit Private Declare Sub keybd_event Li ...

  5. Solr Dataimport配置

    参考资料: https://cwiki.apache.org/confluence/display/solr/Uploading+Structured+Data+Store+Data+with+the ...

  6. win7 64位 python3.4&opencv3.0配置安装

    参考:http://blog.csdn.net/sun7_she/article/details/50051249 一.安装Python 下载Python3.4.2 网址:https://www.py ...

  7. C语言学习笔记--指针与字符串

    字符类型 char(character)是一种整数,也是一种特殊的类型:字符.这是因为 ① 用单引号表示的字符字符字面量:‘a’,'1' ②‘’也是一个字符 ③printf和scanf里用%c来输入. ...

  8. IntelliJ Idea取消Could not autowire. No beans of 'xxxx' type found的错误提示

    1.问题描述 在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示.但程序的编译和运行都是没有问题的, ...

  9. 【转】java byte转long、double、float、int、short,或者long、double、float、int、short转byte

    原文网址:http://www.xuebuyuan.com/988752.html java byte与其他数据类型的转换主要用于二进制数据的编码和解码,主要用于网络传输,读写二进制文件,java和c ...

  10. Android中PopupWindow中有输入框时无法弹出输入法的解决办法

    PopupWindow window=new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //必须让p ...