2014-09-09 3 views
0

나는 IDE에서 정답을 얻고 있지만, 온라인 판사는 다음과 같은 오류를 제공합니다 :"잘못된 문자 오류"가있는 이유는 무엇입니까?

Traceback (most recent call last): 
    File "/tmp/editor_trsource_1410281976_804149.py", line 10, in 
    n=int(raw_input('')) 
ValueError: invalid literal for int() with base 10: '100 10' 

문제 링크 : http://www.hackerearth.com/problem/golf/minimal-combinatorial/

def fact(x): 
    f=1 
    while x>0: 
     f=f*x 
     x-=1 
    return f 
T=int(raw_input('')) 
while T>0: 
    n=int(raw_input('')) 
    r=int(raw_input('')) 
    ans=fact(n)/(fact(r)*fact(n-r)) 
    print str(ans) + "\n" 

    T-=1 

답변

6

nr가 같은 줄에 입력 할 수 있습니다.

100 10 

프로그램에서 두 줄로 입력해야합니다.

100 
10 
0

@ John Kugelman이 이미 지적했듯이 프로그램에서는 n과 r이 같은 줄에 있어야합니다. sys 모듈을 사용하여 입력을 읽는 것이 더 낫습니다. 그것은 다음과 같이 작동합니다 :

import sys 

def fact(x): 
    f=1 
    while x>0: 
     f=f*x 
     x-=1 
    return f 

T=int(sys.stdin.readline().strip()) 

while T>0: 

    nr = map(int, sys.stdin.readline().strip().split()) 
    #Or you can use nr directly while computing ans 
    n = nr[0] 
    r = nr[1] 
    ans=fact(n)/(fact(r)*fact(n-r)) 
    print str(ans) + "\n" 

    T-=1 

샘플 실행 :

$ python fact.py 
1 
5 2 
10 

희망이 도움이!

+0

여전히 오류 : 트레이스 백 (지난 최근 통화) 파일 "/tmp/editor_trsource_1410455624_65093.py"라인 (16), N = NR [0]에 IndexError : 범위 밖의리스트 인덱스 –

관련 문제