zoukankan      html  css  js  c++  java
  • 练习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
    ");
    22         }
    23         else
    24         {
    25             printf("No
    ");
    26         }
    27     }
    28 
    29 
    30     return 0;
    31 }

    AC如下:

  • 相关阅读:
    php max()函数 语法
    php min()函数 语法
    php mt_rand()函数 语法
    php rand()函数 语法
    php pi()函数 语法
    php trim()函数 语法
    php chop()函数 语法
    php rtrim()函数 语法
    php ltrim()函数 语法
    php is_file()函数 语法
  • 原文地址:https://www.cnblogs.com/sunjieee/p/4080065.html
Copyright © 2011-2022 走看看