14922 - CheatSheet (I2P2-Mid1)   

Description

Basic I/O

printf("%d", n);
scanf("%d", &n);

printf() Format Specifiers

Format Argument Type Description
%d, %i int Decimal integer
%u unsigned int Unsigned decimal
%x unsigned int Hexadecimal
%#x unsigned int Hexadecimal with prefix 0x
%f double Floating point
%Lf long double Long double
%e, %E double Scientific notation
%c int To print a character
%s char * String (character array ended with '\0')
%p void * Print memory address
%g, %G double %f or %e depending on the length

 

scanf() Format Specifiers

Format Argument Type Description
%d int * &n, store the input integer in n
%ld long * Read long
%lld long long * Read long long
%u unsigned int * Read unsigned int
%f float * Read float
%lf double * Read double
%Lf long double * Read long double
%c char * Read characters (e.g. Read 3 characters : %3c)
%s char * Read a string until whitespace
%n int * with %s, to get string length

 


Operators

Category Operators
Logical ! && ||
Comparison == != > < >= <=
Arithmetic + - * / %

2D Array Declaration

int a[5][5];
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]
a[3][0] a[3][1] a[3][2] a[3][3] a[3][4]
a[4][0] a[4][1] a[4][2] a[4][3] a[4][4]

Pointer and Array Equivalence

*(a+i) is equivalent to a[i]

(a+i) is equivalent to &a[i]


How to read the following data?

1 2 3 4 5 e
#include <stdio.h>
int main(void)
{
    int x;
    while (scanf("%d", &x) == 1) {   
     printf("x=%d\n", x);
    }
    return 0;
}

How to read the following data?

2
L 5 2
D 5 3
#include <stdio.h>
int main(void)
{
   char ch;
   int i, n, row, col;
   scanf("%d", &n);
   for (i=0; i<n; i++) {
      while(getchar()!='\n');
      scanf("%c%d%d", &ch, &row, &col);
   }
   return 0;
}

Using for loops to print a two-dimensional array

for(i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
        printf("%5d", A[i][j]);
    }
    printf("\n");
}

Using bubble sort to rearrange an array ap

for (i = 0; i < n-1; i++)
{
    for (j = 0; j < n-1-i; j++)
    {
        if (ap[j] > ap[j + 1])
        {
            temp = ap[j];
            ap[j] = ap[j + 1];
            ap[j + 1] = temp;
        }
    }
}

FREQUENTLY USED FUNCTIONS

#include <string.h>
char str[10];
scanf("%s", str);
fgets(str, 10, stdin);
gets(str);

Basic String Operations

Function Description
strlen(str) to get the string length
strcmp(str1, str2) == 0 if equal to compare two strings
strncmp(str1, str2, n) == 0 if equal to compare the first n chars of two strings
strcpy(str1, str2) to copy str2 to str1
strcat(str1, str2) to append a copy of str2 to str1
strncpy(str1, str2, n) to copy the first n chars of str2 to str1 (remember to add '\0' to str1)
strtok(str1, delim) to get the first token in str1 splitted by delim, and use loop to get next token
strstr(str1, str2) to check if str2 is str1's substring, return pointer to the first character of the substring in str1

<ctype.h> Functions

isspace(ch) → check if ch is whitespace (space, tab, newline, etc.)

islower(ch) → check if ch is a lowercase letter

isupper(ch) → check if ch is an uppercase letter

isdigit(ch) → check if ch is a digit (0–9)

isalpha(ch) → check if ch is an alphabet letter (a–z, A–Z)

toupper(ch) → convert ch to uppercase

tolower(ch) → convert ch to lowercase


How to avoid common errors and how to debug for OJ

  1. Put the arrays in the global area. Set their size bigger than required. Avoid using variable-length arrays (e.g. int arr[n];). Keep the array size fixed (e.g. int arr[1000];).
  2. After writing the code for reading input data, you may print out the data to check if your code reads them correctly. Do not proceed to write subsequent code before you confirm that.
  3. If your program crashes, usually it is caused by memory-related errors. Check the ranges of for-loops to see if your code attempts to read or write elements out of the arrays’ boundary.

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss