Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python io #320

Open
yangshao opened this issue Sep 6, 2020 · 5 comments
Open

python io #320

yangshao opened this issue Sep 6, 2020 · 5 comments

Comments

@yangshao
Copy link

@yangshao yangshao commented Sep 6, 2020

请问能不能提供一个python a+b的solution,不知道python的io该如何处理

@virusdefender
Copy link
Contributor

@virusdefender virusdefender commented Sep 7, 2020

代码写起来没啥区别吧,就是读文件,但是安全策略那边目前没适配 Python,可能会 re

@teensgeeker
Copy link

@teensgeeker teensgeeker commented Sep 19, 2020

f = open("ab.in")
line = f.readline()
a, b = line.split()
f.close()
f = open("ab.out", "w")
f.write(str(int(a)+int(b)))
f.close()

会报Runtime Error

C++就正常通过,代码如下:

@teensgeeker
Copy link

@teensgeeker teensgeeker commented Sep 19, 2020

#include
#include
using namespace std;
int main(){
int a, b;
ifstream fin ("ab.in");
fin>>a>>b;
ofstream fout ("ab.out");
fout<<a+b<<endl;
return 0;
}

@virusdefender
Copy link
Contributor

@virusdefender virusdefender commented Sep 19, 2020

安全策略那边目前没适配 Python,可能会 re

这个问题争取下周解决一下

@luojiahai
Copy link

@luojiahai luojiahai commented Oct 12, 2020

前提:输入是两个用空格隔开的整数,输出是一个整数

Standard IO的话,我用的是以下模板,参考LeetCode那种请求用户用函数返回作为答案:

//PREPEND BEGIN
import sys
//PREPEND END

//TEMPLATE BEGIN
def solution(a: int, b: int) -> int:
  // 答案代码如下
  return a + b
//TEMPLATE END

//APPEND BEGIN
if __name__ == '__main__':
  for line in sys.stdin:
    a, b = line.split(' ')
    ret = solution(int(a), int(b))
    print(ret)
//APPEND END

如果不想用函数返回作为答案,可以这样请求用户print:

//PREPEND BEGIN
import sys
//PREPEND END

//TEMPLATE BEGIN
for line in sys.stdin:
  a, b = line.split(' ')
  // 答案代码如下
  solution = int(a) + int(b)
  print(solution)
//TEMPLATE END

//APPEND BEGIN
//APPEND END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.