练习python

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         else
24         {
25             printf("No\n");
26         }
27     }
28 
29 
30     return 0;
31 }

AC如下:

 

转载于:https://www.cnblogs.com/sunjieee/p/4080065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值