📜  如何开始竞争性编程?

📅  最后修改于: 2021-06-26 10:26:51             🧑  作者: Mango

在竞争性编程的开始,几乎没有人知道要遵循的编码风格。下面是一个示例,可以帮助您了解如何在竞争性编程中处理问题。

竞争性程序设计

让我们以下面的问题陈述为例。
问题陈述:

线性搜索:给定一个整数数组和一个元素x,查找元素是否存在于数组中。如果存在element,则打印其首次出现的索引。其他打印-1。

输入:
第一行包含一个整数,即测试用例的数量“ T”。每个测试用例应为整数。第二行中数组“ N”的大小。在第三行中,在由空格分隔的一行中输入数组的整数元素。元素X应该在第四行输入,即在输入数组元素之后。对于多个测试用例,请从第二行开始重复上述步骤。

输出:
在单独的行中打印输出,返回元素X的索引。如果不存在该元素,则打印-1。

限制条件:
1 <= T <= 100
1 <= N <= 100
1 <= Arr [i] <= 100

程序的示例输入和输出:

Input:
2
4
1 2 3 4
3
5
10 90 20 30 40 
40
Output:
2
4

解释:

There are 2 test cases (Note 2 at the beginning of input)
Test Case 1: Input: arr[] = {1, 2, 3, 4},  
                    Element to be searched = 3.
             Output:  2
             Explanation: 3 is present at index 2.

Test Case 2: Input: arr[] = {10, 90, 20, 30, 40}, 
                    Element to be searched = 40.
             Output:  4
             Explanation: 40 is present at index 4.
C
// A Sample C program for beginners with Competitive Programming
#include
  
// This function returns index of element x in arr[]
int search(int arr[], int n, int x)
{
    int i;
    for (i = 0; i < n; i++)
    {
       // Return the index of the element if the element
       // is found
       if (arr[i] == x)
         return i;
    }
  
    //return -1 if the element is not found
    return -1;
} 
  
int main()
{
    // Note that size of arr[] is considered 100 according to
    // the constraints mentioned in problem statement.
    int arr[100], x, t, n, i;
  
    // Input the number of test cases you want to run
    scanf("%d", &t); 
  
    // One by one run for all input test cases
    while (t--)
    {
        // Input the size of the array
        scanf("%d", &n); 
  
        // Input the array
        for (i=0; i


C++
// A Sample C++ program for beginners with Competitive Programming
#include
using namespace std;
  
// This function returns index of element x in arr[]
int search(int arr[], int n, int x)
{
    for (int i = 0; i < n; i++)
    {
        // Return the index of the element if the element
        // is found
        if (arr[i] == x)
            return i;
    }
  
    // return -1 if the element is not found
    return -1;
}
  
int main()
{
    // Note that size of arr[] is considered 100 according to
    // the constraints mentioned in problem statement.
    int arr[100], x, t, n;
  
    // Input the number of test cases you want to run
    cin >> t;
  
    // One by one run for all input test cases
    while (t--)
    {
        // Input the size of the array
        cin >> n;
  
        // Input the array
        for (int i=0; i> arr[i];
  
        // Input the element to be searched
        cin >> x;
  
        // Compute and print result
        cout << search(arr, n, x) << endl;
    }
    return 0;
}


Python
# A Sample Python program for beginners with Competitive Programming
  
# Returns index of x in arr if it is present,
# else returns -1
def search(arr, x):
    n = len(arr)
    for j in range(0,n):
        if (x == arr[j]):
            return j
    return -1
  
# Input number of test cases
t = int(raw_input())
  
# One by one run for all input test cases
for i in range(0,t):
  
    # Input the size of the array
    n = int(raw_input())
  
    # Input the array
    arr = map(int, raw_input().split())
  
    # Input the element to be searched
    x = int(raw_input())
  
    print(search(arr, x))
  
    # The element can also be searched by index method
    # But you need to handle the exception when element is not found
    # Uncomment the below line to get that working.
    # arr.index(x)


Java
// A Sample Java program for beginners with Competitive Programming 
import java.util.*; 
import java.lang.*; 
import java.io.*; 
  
class LinearSearch 
{ 
    // This function returns index of element x in arr[] 
    static int search(int arr[], int n, int x) 
    { 
        for (int i = 0; i < n; i++) 
        { 
            // Return the index of the element if the element 
            // is found 
            if (arr[i] == x) 
                return i; 
        } 
  
        // return -1 if the element is not found 
        return -1; 
    } 
  
    public static void main (String[] args) throws IOException
    { 
        // Note that size of arr[] is considered 100 according to 
        // the constraints mentioned in problem statement. 
        int[] arr = new int[100]; 
  
        // Using BufferedReader class to take input 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
          
        int t = Integer.parseInt(br.readLine()); 
          
        // String Buffer to store answer
        StringBuffer sb = new StringBuffer();
  
        // One by one run for all input test cases 
        while (t > 0) 
        { 
            // Input the size of the array 
            int n = Integer.parseInt(br.readLine()); 
  
            // to read multiple integers line 
            String line = br.readLine(); 
            String[] strs = line.trim().split("\\s+"); 
              
            // Input the array 
            for (int i = 0; i < n; i++) 
                arr[i] = Integer.parseInt(strs[i]); 
  
            // Input the element to be searched 
            int x = Integer.parseInt(br.readLine()); 
  
            // Compute and print result 
            sb.append(search(arr, n, x)+"\n");
  
            t--; 
        }
          
        System.out.print(sb);
    } 
}


初学者的常见错误

  1. 程序不应打印任何多余的字符。编写类似printf("Enter value of n")的语句将在任何平台上导致拒绝。
  2. 输入和输出格式规范必须仔细阅读。例如,大多数问题都要求在每个输出之后都有新的一行。因此,如果我们没有在针对所有测试用例运行的循环中编写printf(“ \ n”)或等效语句,则该程序将被拒绝。

如何开始练习?
您可以从上述问题本身开始。尝试在此处提交上述解决方案之一。建议解决“实践”中的任何编码面试问题。

现在您知道了如何在竞争性编程环境中编写第一个程序,可以从竞争性编程的学校实践问题或竞争性编程的基本实践问题开始。

如何开始学习?
竞争性编程的十大算法和数据结构。
请访问此处,确定哪个类别更适合您。
请参阅此以获得更多针对初学者的常见问题解答。

如何为ACM – ICPC做准备?

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。