Python程序反轉給定數字(2種不同方式)

Take input number from the user and print its reverse.

從用戶處獲取輸入號碼并打印其反面。

Example:

例:

    Input:
12345
Output:
54321

Here, we are implementing program to reversing a given number using 2 different ways.

在這里,我們正在實施程序,以使用2種不同的方式來反轉給定的數字

1) Famous approach for reversing the number: Take input from the user and typecast into an integer, then iterate in the loop till num is not become zero, inside the loop:

1) 逆轉數字的著名方法 :從用戶輸入并將其類型轉換為整數,然后在循環中循環直到num在循環內不為零:

  • Find out the remainder.

    找出其余的。

  • Using this: rev_num = rev_num * 10 + remainder.

    使用這個:rev_num = rev_num * 10 +余數。

  • Update that number by diving by 10.

    通過跳水10來更新該數字。

  • After coming out of the loop printing the reverse number.

    退出循環后,打印反向編號。

if __name__ == "__main__" :
# take string input from user
num = int(input('Enter a number: '))
rev_num = 0
# iterate the loop till num is not equal to zero
while(num) :
rem = num % 10
rev_num = rev_num* 10 + rem
num //= 10
print('Reverse number is: ', rev_num)

Output

輸出量

Enter a number: 12345
Reverse number is:  54321

2) Make a user-defined function for reversing the Number: Take input from the user and typecast into integer, thenreverseNum() function call.

2) 制作一個用于反轉Number的用戶定義函數 :從用戶那里輸入輸入并將其類型轉換為整數,然后調用verseNum()函數。

Inside the function:

函數內部:

  • Iterate in the loop till num does not become zero:

    在循環中迭代,直到num不為零:

  • Find out the remainder.

    找出其余的。

  • Using this: rev_num = rev_num * 10 + remainder.

    使用這個:rev_num = rev_num * 10 +余數。

  • Update that number by diving by 10.

    通過跳水10來更新該數字。

  • After coming out of the loop returning the reverse number to the main.

    退出循環后,將反向編號返回到主編號。

# define a function for finding 
# reverse of the number
def reverseNum(num) :
rev_num = 0
# iterate the loop till num is not equal to zero
while(num) :
rem = num % 10
rev_num = rev_num* 10 + rem
num //= 10
return rev_num
# Main() method
if __name__ == "__main__" :
# take string input from user
num = int(input('Enter a number: '))
print('Reverse number is: ', reverseNum(num))

Output

輸出量

Enter a number: 12345
Reverse number is:  54321

翻譯自: https://www.includehelp.com/python/program-to-reverse-a-given-number-2-different-ways.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/380055.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380055.shtml
英文地址,請注明出處:http://en.pswp.cn/news/380055.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

外匯期貨學習專帖(轉)

一篇好文,值得深思! (轉) 我的一些所看所想,和大家聊聊 ; d: R ^6 c* A2 e X" y8 y4 Bhttp://www.fx998.cn 說來慚愧,很久以前,俺在期貨公司混事,95年以前國內期貨公司從外盤棉花到咖啡....,外匯期指到原油.都能*作.是一派欣欣向榮之景.95年之后,按國家規定都轉了內…

《Pro ASP.NET MVC 3 Framework》學習筆記之四【領域模型介紹】

主題:應用領域驅動開發(Applying Domain-Driven Development) Domain Model是MVC程序的"心臟",其他的一切,包括Controllers和Views僅僅是用來跟Domain Model交互的一種方式,ASP.NET MVC并沒有限制使用在Domain Model上面…

Java——集合框架(List)

集合框架(List的三個子類的特點) A:List的三個子類的特點 ArrayList: 底層數據結構是數組,查詢快,增刪慢。 線程不安全,效率高。 Vector: 底層數據結構是數組,查詢…

一步一步學pwntools(適合新手)

序 pwntools是一個二進制利用框架。官方文檔提供了詳細的api規范。然而目前并沒有一個很好的新手教程。因此我用了我過去的幾篇writeup。由于本文只是用來介紹pwntools使用方法,我不會過于詳細的講解各種二進制漏洞攻擊技術。 Pwntools的“Hello World” 棧溢出無…

關于J2EE中死鎖問題的研究(1)

大多數重要的應用程序都涉及高度并發性和多個抽象層。并發性與資源爭用有關,并且是導致死鎖問題增多的因素之一。多個抽象層使隔離并修復死鎖環境的工作變得更加困難。 通常,當同時執行兩個或兩個以上的線程時,如果每個線程都占有一個資源并請…

python變量分配內存_Python | 聲明任何變量而不分配任何值

python變量分配內存Since, Python is a dynamic programming language so there is no need to declare such type of variable, it automatically declares when first time value assign in it. 由于Python是一種動態編程語言,因此無需聲明此類變量,它…

UVA 10004 - Bicoloring

模擬染色&#xff0c;因為只有兩種顏色&#xff0c;所以分別用 0、 1 代表這兩種顏色&#xff0c;然后從0開始深搜&#xff0c;如果 每個點都能染上色&#xff0c;且相鄰兩點的顏色不同&#xff0c;則符合要求。 #include<stdio.h>#include<string.h>#define MAXN …

標志寄存器:CF、PF、AF、ZF、SF、TF、DF、OF

注&#xff1a;下面說到的標志寄存器都是縮寫&#xff0c;C就是CF&#xff0c;其他也一樣 標志寄存器&#xff1a;C、P、A、Z、S、T、D、O的內容只會是0或1&#xff0c;0表示假&#xff0c;1表示真 O&#xff1a;溢出標志 一個寄存器如果存放的值超過所能表示的范圍&#xf…

揭秘:銷售人員26個致命弱點

銷售人員有許多積極的態度需要學習&#xff0c; 同時也有許多不良的習慣應該避免&#xff0c;以免影響個性及專業能力。仔細看看這些缺點&#xff0c;反省你自己&#xff0c;還需要改善的畫&#xff0c;直到你給自己一百分為止。找一位深知你的好 友&#xff0c;讓他誠實地給你…

Java——集合(練習題)

例題1&#xff1a;產生10個1~20之間的隨機數&#xff0c;要求隨機數不能重復 import java.util.HashSet; import java.util.Random; public class Test1 {/*** 產生10個1~20之間的隨機數&#xff0c;要求隨機數不能重復* * 分析&#xff1a;* 1,有Random類創建隨機數對象* 2&a…

模塊化 組件化 工程化_軟件工程中的模塊和軟件組件

模塊化 組件化 工程化The module in software is a small part of the software that is responsible for performing any kind of functionality. Sometimes, the term sub-program is also used to refer to the term module. 軟件中的模塊是軟件的一小部分&#xff0c;負責…

Firefox2狂占CPU解決辦法

https://images.cnblogs.com/cnblogs_com/Tisty/138006/o_firefox3.jpg 看了一下&#xff0c;不知道 "jpeg_free_large" 是干啥的&#xff0c;遂用 "Firefox jpeg_free_large" Google 一下&#xff0c;出來的一堆東西里有帖子說可能和 Apple 的 QuickTime …

PUSHAD和POPAD,以及PUSHA和POPA

PUSHAD PUSHAD也叫保護現場&#xff0c;就是把我們的寄存器壓入棧中 pushad是把eax&#xff0c;ecx&#xff0c;edx&#xff0c;ebx&#xff0c;esp、ebp&#xff0c;esi&#xff0c;edi依次壓入棧中&#xff0c;ESP會減少32&#xff0c;相當于&#xff1a; push eax push ec…

Java——n個數的全排列

例題&#xff1a; 輸入一串字符串&#xff0c;將該字符串中的字符元素進行全排列&#xff0c;然后&#xff0c;一串輸出結果。 例如&#xff1a; 輸入&#xff1a; ABCD 輸出&#xff1a; ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC BDCA CABD CADB CBAD CBDA…

一段個性化stringgrid的代碼

需要注意的是 該段程序使用了 canvas。 procedure TW_CkbTaiZhang.KhLstDrawCell(Sender: TObject; ACol,ARow: Integer; Rect: TRect; State: TGridDrawState);begin if ARowkhlst.Row then with khlst.Canvas do //畫 cell 的邊框 begin Pen.Color : $00ff0000; …

dp 扔雞蛋_使用動態編程(DP)的雞蛋掉落問題

dp 扔雞蛋Problem statement: You are given N floor and K eggs. You have to minimize the number of times you have to drop the eggs to find the critical floor where critical floor means the floor beyond which eggs start to break. Assumptions of the problem: 問…

MOVSX和MOVZX

MOVSX 先符號擴展,再傳送 格式&#xff1a; MOVSX 操作數A &#xff0c;操作數B //操作數B的空間小于A比如說我們使用命令&#xff1a; movsx eax&#xff0c;bxbx是16位&#xff0c;eax是32位&#xff0c;傳值過程&#xff1a; 先用bx的符號位把eax高16填滿&#xff0c;b…

統計學習以及支持向量機的國內外基本比較重要的書

1、支持向量機導論&#xff0c;此書乃是SVM方面的經典著作&#xff0c; 該書的作者也是近年來SVM、kernel methods學術圈內的活躍學者&#xff0c;對于這些領域均有過重要的貢獻。這本書從“線性機器、核方法、統計學習理論、凸優化”四個方面揭示了SVM的內在機理 --利用核…

Java——集合(TreeSet)

package com.wsq.set; //這里進行調用Person()方法&#xff0c;要進行導包 import java.util.TreeSet; import com.wsq.bean.Person; public class Demo3_TreeSet { /*** TreeSet集合是用來對元素進行排序的&#xff0c;同樣它也可以保證元素的唯一* 當compareTo()方法返…

setmonth_日期setMonth()方法以及JavaScript中的示例

setmonthJavaScript日期setMonth()方法 (JavaScript Date setMonth() method) setMonth() method is a Date class method, it is used to set the month to the Date object with a valid month value (between 0 to 11. 0 for January, 1 for February and so on). setMonth(…