倒置輸入的整數(C、Python)

 

 

C語言:python

 1 # include <stdio.h>
 2 
 3 void f(int num)
 4 {
 5     int i=0, j=0, yu;
 6     
 7     printf("轉置後:");
 8     while (num > 0)
 9     {
10         yu = num % 10; //餘數
11         num = num / 10;        
12         if (yu == 0)
13             i++; //統計0的個數
14         else
15             j++; //統計非0的個數
16         if (j > 0)
17             printf("%d", yu); //若是非0個數大於0,表示餘數不爲0,須要輸出
18     }
19     printf("\n\n");
20     printf("%d,%d\n", i, j); //0個非0個數
21     return;
22 }
23 
24 int main(void)
25 {
26     int a;
27     printf("請輸入一個正數:");
28     scanf("%d", &a);
29     printf("\n轉置前:%d\n\n", a);
30     f(a);
31 
32     return 0;
33 }

 

Python:app

s = 123450
def rev(s):
    li = []
    li2 = []
    for i in str(s):
        li.append(i)
    n = len(li)-1
    flag = 0
    while(n>=0):
        if li[n]=='0' and flag==0:
            n -= 1
            continue
        else:
            flag=1
            li2.append(li[n])
            n -=1
    li_re=''.join(li2)
    return li_re
s = rev(s)
print(s)

相關文章
相關標籤/搜索