首页 > 其他分享 >AcWing 727. 菱形

AcWing 727. 菱形

时间:2023-05-02 16:24:58浏览次数:37  
标签:int 曼哈顿 727 菱形 include AcWing

AcWing 727. 菱形


1. 地址

    https://www.acwing.com/problem/content/description/729/

2. 题解

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;


// 这道题需要用到曼哈顿距离
// 通过找规律发现,如果某一点跟中心点(n/2,n/2)的曼哈顿距离<=n/2的话,那么就是*,否则就是空格
// 曼哈顿距离:(x1,y1)(x2,y2) =>  |x2-x1| + |y2-y1| 
int main(){
    int n;
    scanf("%d",&n);
    int cx = n/2,cy = n/2;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(abs(cx-i) + abs(cy-j) <= n/2){
                printf("*");
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    
    return 0;
}

标签:int,曼哈顿,727,菱形,include,AcWing
From: https://www.cnblogs.com/gao79135/p/17367821.html

相关文章

  • AcWing 726. 质数
    AcWing726.质数1.地址https://www.acwing.com/problem/content/728/2.题解//此题跟完全数这道题差不多#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;intmain(){intcount;intnumber;intflag=1;sc......
  • AcWing 725. 完全数
    AcWing725.完全数1.地址https://www.acwing.com/problem/content/description/727/2.题解#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;//注意:这道题如果暴力解法一定TLE//因此,我们需要对其进行优化intmain(){intn;......
  • AcWing 4086 分糖果
    关于这道题我当时大意了https://www.acwing.com/problem/content/description/4089/关于我的某个变量没有初始化这件事,唯一想法,敲死得了,谁懂?其实就是一道简简单单的数学分析题,和大佬们不一样,萌新只会简简单单的小学数学(本人初二!)分析走起! 一道典型的数学问题() 虽然我WA了,......
  • 使用曼哈顿距离画菱形
    输入样例:5输出样例:*************importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt();intcx=n/2,cy=n/2......
  • AcWing 656. 钞票和硬币
    AcWing656.钞票和硬币1.地址https://www.acwing.com/problem/content/658/2.解答#include<iostream>#include<cstdio>usingnamespacestd;intmain(){intmoney[6]={100,50,20,10,5,2};doublecoins[6]={1.0,0.50,0.25,0.10,0.05,0.01};......
  • for循环打印菱形
    for循环打印菱形今天我们来看一个使用双重for循环打印菱形的经典题目总体来说还是要找到规律,用外层循环控制行数。内层循环控制空格和星星的数量关键是要找到需要打印多少个空格和星星#include<stdio.h>intmain(){/*我们就先写一个7行的菱形然后再写n行的菱形*/......
  • PSYCH 727 shell 实现
    LAB1(S1,2023)OUTLINEFORTHISLABThepurposeofthislaboratorysessionistofamiliariseyouwiththeLinuxenvironmentandtheshell,MATLAB,andthedataweareworkingwith(fMRIimages).Todaywewillcover:1.SettingUpRemoteAccesstotheLabVM2.......
  • AcWing 242. 一个简单的整数问题 / 树状数组区间修改区间查询模板题
    AcWing242.一个简单的整数问题//实例化是抽象的天敌,是抽象的克星//通过公式sn=(i从1~n求积)di*(1+n)-(i从1~n求积)i*di//来计算前缀和,又(i从1~n求积)i*di不能由(i从1~n求积)di*(1+n)推出//所以除了维护d数组,还需维护......
  • 在VS中怎么查看单类布局报告?看了就可以直接看多态和菱形的类的继承情况了!
    1,打开文件所在路径  2.复制文件路径  3.打开vs开发者命令工具 4.进入到源文件目录   5.查看命令:cd /d1reportSingleClassLayout类名 "xxx.cpp" ......
  • Acwing 3728-城市通电 / 最小生成树,建图,超级源点
    AcWing3728.城市通电做出来就凭之前的一句感悟:把每个动态选择变为与超级源点连的一条边,把这条边加入图里面跑最小生成树就相当于考虑了每个动态选择......