如何开始竞争性编程?

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

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

问题陈述

线性搜索Linear Search):给定一个整数数组和一个元素x,查找元素是否存在于数组中。如果元素存在,则打印其首次出现的索引。否则打印-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<stdio.h> 
  
// 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<n; i++) 
           scanf("%d",&arr[i]); 
  
        // Input the element to be searched 
        scanf("%d", &x); 
  
        // Compute and print result 
        printf("%d\n", search(arr, n, x)); 
    } 
    return 0; 
} 

C++

// A Sample C++ program for beginners with Competitive Programming 
#include<iostream> 
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<n; i++) 
            cin >> 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”)或等效语句,则该程序将被拒绝。

六一编程网

Next Post

为什么你应该成为一个自学成才的程序员?

周六 8月 15 , 2020
您可能听说过杰克·多西(Twitter),马克·扎克伯格(Facebook),凯文·西斯特罗姆(In […]