练习python,,Codeforces


Codeforces Round #276 (Div. 2) A. Factory

链接http://codeforces.com/contest/485/problem/A

我刚学python,刚好用这道水题练习一下python的输入和list的使用

输入用到了map(int, raw_input().split()),这里的map是映射。

list 的赋初值的方式是s = [0] * M ,其中M是常量。

同时while和if的基本写法,都是才用缩进的方式。

python代码:

 1 M = 100010 2 a, m = map(int, raw_input().split()) 3 s = [0] * M 4 while s[a % m] == 0: 5     s[a % m] = 1 6     a += a % m 7 if s[0]: 8     print ‘Yes‘ 9 else:10     print ‘No‘

python的优点就是动态类型,所以代码行数比c++的少很多,

但减少了代码行数的同时,python的运行时间却比c++多了很多。

还有就是内存自动管理机制,同时对字符串的支持很好,处理起来很方便。

c++代码:

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int M=100010; 6 int s[M]; 7  8 int main() 9 {10     int a,m;11     while(scanf("%d%d",&a,&m)!=EOF)12     {13         memset(s,0,sizeof(s));14         while(s[a%m]==0)15         {16             s[a%m]=1;17             a=(2*a)%m;18         }19         if(s[0])20         {21             printf("Yes\n");22         }23         else24         {25             printf("No\n");26         }27     }28 29 30     return 0;31 }

AC如下:

bubuko.com,布布扣

练习python

评论关闭