Algorithms notes - simple programming training

[Codeup 1934] find x

Of course, use may also be determined whether i == n x exists

#include<cstdio>
int main () {
    const int max = 210;
    int a[max] = {0};
    int n, j = -1;
    scanf("%d", &n);

    for(int i=0; i < n; i++){
        scanf("%d", &a[i]);
    }

    int x;
    scanf("%d", &x);

    for(int i = 0; i < n; i++){
        if(a[i] == x){
            printf("%d", i);
            j = 0;
            break;
        }
    }

    if(j == -1){printf("-1");}

    return 0;
}

  

[PAT B1036] programmed together with Obama

#include<cstdio>
int main () {

    int row;
    int col;
    char character;
    do{
    scanf("%d %c", &col, &character);
    }while(col > 20 || col < 3);

    if(col % 2 == 0){
        row = col / 2;
    }else{
        row = col / 2 + 1;
    }

    for(int i=0; i < col; i++){
        printf("%c", character);
    }
    printf("\n");
    for(int i=0; i < row; i++){
        printf("%c", character);
        for(int j=0; j < col - 2; j++){
            printf(" ");
        }
        printf("%c\n", character);
    }
    for(int i=0; i < col; i++){
        printf("%c", character);
    }

    return 0;
}

 

Codeup 1928] [Date difference

#include<cstdio>

int month[13][2] = {
    {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30},
     {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30},  {31, 31}};

bool isLeap(int year){
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

void change(int* y1, int* y2){
    int temp;
    if(*y1 > *y2){
        temp = *y1;
        *y1 = *y2;
        *y2 = temp;
    }
    return;
}

int main () {

    int counts = 1;
    int data1, year1, month1, day1;
    int data2, year2, month2, day2;

    while(scanf("%d%d", &data1, &data2) != EOF){
        change(&data1, &data2);
    }

    year1  = data1 / 10000;
    month1 = data1 % 10000 / 100;
    day1   = data1 % 100;

    year2  = data2 / 10000;
    month2 = data2 % 10000 / 100;
    day2 = data2 100%;

    while(year1 < year2 || month1 < month2 || day1 < day2){
        day1 ++;
        if(day1 == month[month1][isLeap(year1)] + 1){
            month1 ++;
            day1 = 1;
        }
        if(month1 == 13){
            year1 ++;
            month1 = 1;
        }
        counts ++;

    }
    printf("%d\n", counts);

    return 0;
}

  

Guess you like

Origin www.cnblogs.com/zgqcn/p/12196005.html