首页 > 编程语言 > 详细

算法笔记--简单编程训练

时间:2020-01-15 13:49:37      阅读:86      评论:0      收藏:0      [点我收藏+]

【codeup 1934】找x

当然也可利用 i == n 判断是否有x存在

#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】跟奥巴马一起编程

#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】 日期差值

#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;
}

  

算法笔记--简单编程训练

原文:https://www.cnblogs.com/zgqcn/p/12196005.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!