上机指南2.2模拟

在这里插入图片描述
在这里插入图片描述

#include<stdio.h>
int main(){
    int h;
    scanf("%d",&h);
    for(int i=1;i<=h;i++){
        for(int j=1;j<=3*h-2;j++){
            if(j <= 2*(h-i))
                printf(" ");
            else
                printf("*");
        }
        printf("\n");
    }

    return 0;
}



在这里插入图片描述

注意,scanf在连续输入的时候不能带空格

#include<stdio.h>
int main(){
    int n;
    char a[80][80];
    char A,B;
    
    scanf("%d%c%c",&n,&B,&A);
	/*
	scanf("%d",&n);
    getchar();
    scanf("%c",&B);
    getchar();
    scanf("%c",&A);
	*/

    a[0][0]=a[0][n-1]=' ';// reset the four corner
    a[n-1][0]=a[n-1][n-1]=' ';

    int even = ((n-1)/2)%2;//1->A  0->B

    char edge=A;
    if (even == 0){
        edge=B;
    }
    else{
        edge=A;
    }

    //the outest level
    for(int i=1;i<=n-2;i++){
            a[0][i]=edge;
            a[n-1][i]=edge;
            a[i][0]=edge;
            a[i][n-1]=edge;
        }

    for(int c=2;c<=(n+1)/2;c++){
        if(edge == A){
            edge = B;
        }
        else{
            edge = A;
        }
        for(int i=c-1;i<=n-c;i++){
            a[c-1][i]=edge;
            a[n-c][i]=edge;
            a[i][c-1]=edge;
            a[i][n-c]=edge;
        }
    }
    for(int i=0;i<n;i++){
        for(int j =0;j<n;j++){
            printf("%c",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

牛客网1161:

题目:
The input contains multiple test cases.
The first line of each case is an integer N, representing the size of the template is NN (N could only be 3, 4 or 5).
Next N lines describe the template.
The following line contains an integer Q, which is the Scale Level of the picture.
Input is ended with a case of N=0.
It is guaranteed that the size of one picture will not exceed 3000
3000.
输入包含多个测试用例。

每种情况的第一行都是一个整数N,表示模板的大小为NN (N只能是3、4或5)。
接下来的N行描述模板。
下一行包含一个整数Q,它是图片的缩放级别
输入以N=0的情况结束。
保证一张图片的尺寸不超过3000
3000。

输入:
3
# #
 # 
# #
1
3
# #
 # 
# #
3
4
 OO 
O  O
O  O
 OO 
2
0
输出:
# #
 # 
# #
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
   # #               # #   
    #                 #    
   # #               # #   
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
         # #   # #         
          #     #          
         # #   # #         
            # #            
             #             
            # #            
         # #   # #         
          #     #          
         # #   # #         
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
   # #               # #   
    #                 #    
   # #               # #   
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
     OO  OO     
    O  OO  O    
    O  OO  O    
     OO  OO     
 OO          OO 
O  O        O  O
O  O        O  O
 OO          OO 
 OO          OO 
O  O        O  O
O  O        O  O
 OO          OO 
     OO  OO     
    O  OO  O    
    O  OO  O    
     OO  OO     

习题2.5 hello world for u

#include <stdio.h>
#include <string.h>
#define N 27
int main(void)
{
    char str[81];
    char out[N][N];
    int n1,n2,n3;
    while(scanf("%s",str) != EOF){
        int len=strlen(str);
        int a = len/3;
        int b = len%3;
        if(b!=0){
            n1 = a;
            n2 = a + b;
        }
        else{
            n1 = a-1;
            n2 = a+2;
        }
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++){
                    out[i][j]=' ';
            }
        for(int i =0; i<n1;i++)
            out[i][0]=str[i];
        for(int i = 0; i<n2; i++)
            out[n1][i]=str[i+n1];
        for(int i = 0;i < n1;i++)
            out[n1-1-i][n2-1]=str[n1+n2+i];
        for(int i=0;i<=n1;i++){
            for(int j=0;j<n2;j++){
                printf("%c",out[i][j]);
            }
            printf("\n");
        }
   }

    return 0;
}

习题2.6 今年的第几天
注意 闰年判断和月份判断
闰年:能被4整除但不能被100整除 or 能被400整除

#include <stdio.h>
#include <string.h>
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int runYear(int a){
        if((!(a%4) && (a%100)) || !(a%400))
            return 1;
        return 0;
}
int main(void)
{
    int year,month,day,sum;
    sum=0;
    while(scanf("%d%d%d", &year,&month,&day) != EOF)
    {   getchar();

        for(int i=1;i<month;i++){
            sum += mon[i];
        }

        if(runYear(year) && month>2)
            sum += day+1;
        else
            sum += day;

        printf("%d\n",sum);
        sum=0;

    }
    return 0;
}

例题2.7 打印日期
注意输出格式:个位数时1显示为01, 5显示为01, printf("%02d", month);

#include <stdio.h>
#include <string.h>
int nis[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int is[13]={0,31,60,91,121,152,182,213,244,274,305,335,366};
int runYear(int a){
        if((!(a%4) && (a%100)) || !(a%400))
            return 1;
        return 0;
}
int main(void)
{
    int n,year,month,day,sum;
    sum=0;
    while(scanf("%d%d", &year,&n) != EOF)
    {   getchar();

        if(runYear(year))
            for(int i=1;i<=12;i++){
                if(n <=is[i]){
                    month = i;
                    day = n-is[i-1];
                    break;
                }
                else if(n > is[i] && n<is[i+1]){
                    month = i+1;
                    day = n-is[i];
                    break;
                }
                else
                    continue;
            }
        else{
            for(int i=1;i<=12;i++){
                if(n <=nis[i]){
                    month = i;
                    day = n-nis[i-1];
                    break;
                }
                else if(n > nis[i] && n<nis[i+1]){
                    month = i+1;
                    day = n-nis[i];
                    break;
                }
                else
                    continue;
            }
        }
        printf("%04d-%02d-%02d\n",year,month,day);
    }
    return 0;
}

这个代码更简洁

#include <stdio.h>
#include <string.h>
int daytab[2][13]={
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
    };
int runYear(int a){
        if((!(a%4) && (a%100)) || !(a%400))
            return 1;
        return 0;
}
int main(void)
{
    int n,year,month,day;
    while(scanf("%d%d", &year,&n) != EOF)
    {   getchar();
        month = 0;

        int row = runYear(year);
        //确定月
        while(n > daytab[row][month]){
            n -= daytab[row][month];
            month++;
        }
        //确定日
        day = n;
        printf("%04d-%02d-%02d\n",year,month,day);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值