python程序的輸入輸出(acm的幾個小程序)


1,  A+B Problem : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1000

#!/usr/bin/env python  
# coding=utf-8  
  
a=[]  
  
for x in raw_input().split():  
    a.append(int(x))  
  
print sum(a)  

下面的代碼只有一行,,可惜不是我想出來的!!!!:

print sum(int(x) for x in raw_input().split()) 

 

2, A+B for Input-Output Practice (I) : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1010

while True:  
    a=[]  
    line = raw_input()  
    if line:  
        for x in line.split():  
            a.append(int(x))  
        print sum(a)  
    else:  
        break

 

3, A+B for Input-Output Practice (II)  :  http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1011

#!/usr/bin/env python
#coding:utf8

t = int(raw_input())

while t>0:
    a=[]
    for x in raw_input().split():
        a.append(int(x))
    print sum(a)
    t=t-1

 

4, A+B for Input-Output Practice (III) : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1012

#!/usr/bin/env python
#coding:utf8

while True:
    line = raw_input()
    a=[]
    for x in line.split():
        a.append(int(x))
    if a[0]==0 and a[1]==0:
        break
    print sum(a)

5, A+B for Input-Output Practice (IV) : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1013

這段代碼應該算是被我水過的,我是直接將這一行讀取,然后所有的數相加,然后再減去 t (應該讀入的數字的個數),並沒有按照題目要求來,,呵呵,,先這樣吧,

#!/usr/bin/env python
#coding:utf8

while True:
    num=[]
    line = raw_input()
    for x in line.split():
        num.append(int(x))
    if num[0]==0:
        break
    a=num[0]
    print sum(num)-a

 

6, A+B for Input-Output Practice (V) : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1014

#!/usr/bin/env python
#coding:utf8

t = int(raw_input())

while t>0:
    if t==0:
        break
    line = raw_input()
    num=[]
    for x in line.split():
        num.append(int(x))
    print sum(num)-num[0]
    t = t-1

 

7, A+B for Input-Output Practice (VI) : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1015

#!/usr/bin/env python
#coding:utf8

while True:
    line = raw_input()
    num=[]
    for x in line.split():
        num.append(int(x))
    print sum(num)-num[0]

 

8, A+B for Input-Output Practice (VII) : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1016

被打敗了,被'\r'打敗了,,輸出一個空行,,不解釋

#!/usr/bin/env python
#coding:utf8

while True:
    a=[]
    line = raw_input()
    for x in line.split():
        a.append(int(x))
    print sum(a)
    print '\r'          #輸出一個空行

附 : python轉義字符:

在需要在字符中使用特殊字符時,python用反斜杠(\)轉義字符。如下表:

原始字符串有時我們並不想讓轉義字符生效,我們只想顯示字符串原來的意思,這就要用r和R來定義原始字符串。      如:         print r'\t\r'          實際輸出為“\t\r”。

 

轉義字符 描述
\(在行尾時) 續行符
\\ 反斜杠符號
\' 單引號
\" 雙引號
\a 響鈴
\b 退格(Backspace)
\e 轉義
\000
\n 換行
\v 縱向制表符
\t 橫向制表符
\r 回車
\f 換頁
\oyy 八進制數yy代表的字符,例如:\o12代表換行
\xyy 十進制數yy代表的字符,例如:\x0a代表換行
\other 其它的字符以普通格式輸出
 
 
 
#!/usr/bin/env python
#coding:utf8

t = int(raw_input())

while t>0:
    line = raw_input()
    num = []
    for x in line.split():
        num.append(int(x))
    print sum(num)-num[0]
    t = t-1
    if t!=0:
        print '\r'

 

恩,,用python  AC ,另一番風味。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM