c - 逆序/正序输出每位.

 1 #include <stdio.h>
 2 #include <math.h>
 3
 4 /*
 5 判断一个正整数的位数,并按正序,逆序输出他们的位.
 6 */
 7
 8 int
 9 invert(int);
10
11 void
12 order(int, int);
13
14 int
15 main(void) {
16     int n = 56789;
17     printf("original:%d\n", n);
18     int bitCont = invert(n);
19     printf("\nbits: %d\n", bitCont);
20     order(n, bitCont);
21     return 0;
22 }
23
24 //逆序输出每一位并记录位数.
25 int
26 invert(int n) {
27     int bitCount = 0;    //记录位数.
28     do
29     {
30         ++bitCount;
31         printf("%d ", n % 10);    //逆序输出每一位.
32         n /= 10;
33     } while (n);    //终止条件是"n为0".
34     return bitCount;
35 }
36
37 //正序输出每一位.
38 void
39 order(int n, int bitCount) {
40     int tmp = 0;    //存储即将减去的级别,如当n为"1234"时,存储"1000";当n为"234"时,存储"100".
41     int h = 0;    //存储最高位的位.
42     while(n) {
43         tmp = (int)pow((double)10, --bitCount);
44         h = n / tmp;
45         printf("%d ", h);
46         n -= h * tmp;
47     }
48 }

output:

original:56789
9 8 7 6 5
bits: 5
5 6 7 8 9 请按任意键继续. . .
时间: 2024-08-24 15:06:27

c - 逆序/正序输出每位.的相关文章

算法-整数的正序和逆序输出

其实上一篇文章用递归实现了整数的正序输出,思考了整数还是可以逆序输出,不过大同小异,没有太多差别: 正序输出 -(void)printOutNumber:(NSInteger)number{ //取整,不断的递归取整,之后取余 if (number>10) { [self printOutNumber:number/10]; } NSLog(@"数值%ld",number%10); } 逆序输出 循环输出,这个比较简单容易理解: -(void)reverseNumber:(NSI

Ka递归的编程练习 Part2|做到吐的正序逆序输出、等差数列和

1 #include<stdio.h> 2 void PP(int n) 3 { 4 if(n==0) return; 5 PP(n/10); 6 printf("%d",n%10); 7 } 8 void NP(int n) 9 { 10 if(n==0) return; 11 printf("%d",n%10); 12 PP(n/10); 13 } 14 int Ad(int n) 15 { 16 if(n==0) return 0; 17 if(n

【c语言】将一个数的二进制序列逆序,然后输出逆序之后的二进制序,所对应的数

// 将一个数的二进制序列逆序,然后输出逆序之后的二进制序,所对应的数 #include <stdio.h> // 从原数拿出最低位,放到mid中,mid左移,原数右移 int reverse(int a) { int mid = 0; int bit; int n = 31; for (; n > 0; --n) { bit = a & 1; mid |= bit; mid <<= 1; a >>= 1; } return mid; } int main

用结构体指针存储数据__正序_逆序下的输入

逆序输入 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #define maxn 1000 5 6 //邻接表(点很多,边较少) 7 //共有n个点,点编号为1~n,m条边 8 //无向图 9 10 struct node 11 { 12 long value; 13 struct node *next; 14 }*d[maxn+1]; 15 16 int main() 17 { 1

正序逆序生成单链表

typedef struct LNode{ int key; struct LNode *next; }LNode,*List; //正序生成单链表 void CreateList1(List &L,int n){ L=(List)malloc(sizeof(LNode)); L->next=NULL; LNode *q=L; for(int i=1;i<=n;i++) { LNode *p=(LNode *)malloc(sizeof(LNode)); scanf("%d&

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

这种排序 超级简单的 ! using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { string[] str = { "d","h","a","c",&

SortedDictionary&lt;TKey,TValue&gt;正序与反序排序

SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SortDictionary { class Program { static void Main(string[] args) { TestDictionarySort()

python找出一个正序反序都相等的数字(例如28682)

直接上代码 #coding:utf-8 def f1(x): #定义一个函数,查找正序反序都相等的数字 if type(x) !=int: #如果函数参数不是整型,退出程序 exit('must a int type') x=str(x) lix=list(x) str1='' i=len(lix)-1 while i <len(lix) and i >=0: #循环的作用是将字符串从尾到头重新组合相加一次 str1=str1+lix[i] i=i-1 if x==str1: #如果源字符串和

android listview反序和正序显示

本人很喜欢晚上睡觉用暴风语音看电影,如图: 现在写个demo 演示下 MainActivity.java package com.example.listview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickLis