Python语法(类比Java和c++)

定义

一维数组

全0,长度为N的数组
  1. c++

    #include <bits/stdc++.h>
    const int N = 10010;
    int a[N];
    
  2. java

    static final int N = 10010;
    static int [] a = new int[N];
    
  3. python

    N = 10010
    a = [0]*N
    

二维数组

全0,长度为N的二维数组
  1. c++

    #include <bits/stdc++.h>
    const int N = 10010;
    int a[N][N];
    
  2. java

    static final int N = 10010;
    static int [][] a = new int[N][N];
    
  3. python

    N = 10010
    a = [[0]*N for _ in range(N)]
    

输入

读入一个整数

  1. c++

    #include <bits/stdc++.h>
    int main()
    {
        int x;
        scanf("%d", &x);
    }
    

    或者:

    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        cin >> x;
    }
    
  2. java

    import java.util.Scanner;
    
    public class Main {
        static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {
            int x = sc.nextInt();
        }
    }
    
  3. python

    x = int(input())
    

读入一行内几个整数

  1. c++

    #include <bits/stdc++.h>
    int main()
    {
        int x,y,z;
        scanf("%d%d%d", &x,&y,&z);
    }
    

    或者:

    #include <iostream>
    using namespace std;
    int main()
    {
        int x,y,z;
        cin >> x >> y >> z;
    }
    
  2. java

    import java.util.Scanner;
    
    public class Main {
        static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {
            int x = sc.nextInt(),y = sc.nextInt(),z = sc.nextInt();
        }
    }
    
  3. python

    x,y,z = map(int ,input().split())
    #这里input().split()是把一行的字符串读入以空格切分为一个列表
    #map()的作用是将一个函数,作用于列表的每个元素上,并返回一个列表,第一个参数是函数名,不用加()
    #这里相当于把int()函数作用在读入的每一个字符上,再序列解包
    

不告诉输入规模,以指定字符结束读入(python和Java思路相同)

例题:AcWing 131. 直方图中最大的矩形 - AcWing

以n为0,作为输入结束符(即对每组数据,n不为0读入一组数据,n为0结束读入)
  1. c++

     #include <bits/stdc++.h>
     int main()
     {
         int n;
         while(scanf("%d", &n), n){
           //读入数据
         }
     }
    

    或者:

     #include <iostream>
     using namespace std;
     int main()
     {
         int n;
         while(cin>>n&&n){
           //读入数据
         }
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
             while (sc.hasNext()) {
                int n = sc.nextInt();
                if (n == 0) {
                    break;
                }
               //读入数据
             }
         }
     }
    
  3. python

    while True:
        line = input()
        if line == '0':
            break
        #读入数据
        nums = list(map(int, line.split()))
        n = nums[0]
        nums = nums[1:]
    

    或者:

    while True:
        try:
        		nums = []
            line = [int(x) for x in input().split()]
            n = line[0]
            if n not 0:
                nums.append(line[1:])
        except:
            break
    

例题:AcWing 1096. 地牢大师(c++/java/python) - AcWing

以几个数为0,作为输入结束符
  1. c++

     #include <bits/stdc++.h>
     int main()
     {
         int l,r,c;
     		while(cin>>l>>r>>c,l||r||c){
          	//读入数据
        }
     }
    

    或者:

     #include <iostream>
     using namespace std;
     int main()
     {
         int l,r,c;
         while (scanf("%d%d%d", &l, &r, &c), l || r || c){
           //读入数据
         }
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
             while (sc.hasNext()) {
                int l = sc.nextInt();
               	int r = sc.nextInt();
               	int c = sc.nextInt();
                if (n == 0||r==0||c == 0) {
                    break;
                }
               //读入数据
             }
         }
     }
    
  3. python

    while True:
        l, r, c = map(int, input().split())
        if l == 0:
            break
        #读入数据
    

不告诉输入行数,且无结束标记

例题:AcWing 3489. 星期几 - AcWing

  1. c++

     #include <bits/stdc++.h>
     int main()
     {
         int l,r,c;
     		while(cin>>l>>r>>c,l||r||c){
          	//读入数据
        }
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
             while (sc.hasNext()) {
               //读入数据
             }
         }
     }
    
  3. python

    while True:
        try:
            #读入数据
        except:
            break
    

输入1维数组

从a[0]开始读入
  1. c++

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1010;
     int main()
     {
         int n,a[N];
    		for(int i = 0;i<n;i++) cin>>a[i];
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
    				int n = sc.nextInt();
           int a[] = new int[n];
            for(int i = 0;i<n;i++){
              a[i] = sc.nextInt();
            }
         }
     }
    
  3. python

    a = []
    arr = list(map(int, input().split()))
    
从a[1]开始读入

例题:AcWing 795. 前缀和 - AcWing

  1. c++

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1010;
     int main()
     {
         int n,a[N];
    		for(int i = 1;i<=n;i++) cin>>a[i];
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
    				int n = sc.nextInt();
           int a[] = new int[n+1];
            for(int i = 1;i<=n;i++){
              a[i] = sc.nextInt();
            }
         }
     }
    
  3. python

    a = []
    a=[0]+list(map(int,input().split()))
    

输入二维数组

整数数组
  1. c++

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1010;
     int main()
     {
         int n,m,a[N][N];
       	cin>>n>>m;
    		for(int i = 0;i<n;i++){
          for(int j = 0;j<m;j++){
            cin>>a[i][j];
          }
        }
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
    				int n = sc.nextInt();
           int a[][] = new int[n][n];
    			for(int i = 0;i<n;i++){
          	for(int j = 0;j<m;j++){
            	a[i][j] = sc.nextInt();
          	}
        	}
         }
     }
    
  3. python

    n, m = map(int, input().split())
    g = [input().split() for _ in range(n)]
    #这种方法读入的是一个字符型的二维数组
    #无法用便捷的方式把二维字符数组转化为整数数组
    #可以在用的时候再用int()函数转化一下
    

    或者:

    #先定义后读入
    g = [[0]*N for _ in range(N)]
    
    

输出

将数组中的整数用空格分隔输出

  1. c++

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1010;
     int main()
     {
         int n,a[N];
       	cin>>n;
    		for(int i = 0;i<n;i++){
    			cout<<a[i]<<' ';
        }
       //换行
       cout<<endl;
     }
    
  2. java

     import java.util.Scanner;
     
     public class Main {
         static Scanner sc = new Scanner(System.in);
         public static void main(String[] args) {
    				int n = sc.nextInt();
           int a[] = new int[n];
    			for(int i = 0;i<n;i++){
    				System.out.println(a[i]+" ");
        	}
           //换行
           System.out.println();
         }
     }
    
  3. python

    n = int(input())
    a = []
    for i in a:
      print(i,end = " ")
    #换行
    print()
    

二维数组的输出(空格分割和换行的技巧)同理

数据结构

队列

集合

Map

优先级队列

邻接表

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐