package com.zeph.j2se.alg;
創(chuàng)新互聯(lián)建站憑借在網(wǎng)站建設(shè)、網(wǎng)站推廣領(lǐng)域領(lǐng)先的技術(shù)能力和多年的行業(yè)經(jīng)驗(yàn),為客戶提供超值的營銷型網(wǎng)站建設(shè)服務(wù),我們始終認(rèn)為:好的營銷型網(wǎng)站就是好的業(yè)務(wù)員。我們已成功為企業(yè)單位、個(gè)人等客戶提供了成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),以良好的商業(yè)信譽(yù),完善的服務(wù)及深厚的技術(shù)力量處于同行領(lǐng)先地位。
public class Heart {
int HighLevel(int wide) {
int i = 0, j = 0, k = 0, t = 0, m = 0, n = 0, count = 1;// i控制循環(huán)內(nèi)高度,j控制每行前面空格,k控制輸出的*數(shù)
// t控制高層星號(hào)中間空格,m記錄高層最底行的星數(shù),以下逐行增4
// n記錄頂行空個(gè)數(shù),以下逐行減4;count記錄高層高度,與high無關(guān)
m = (wide - 4) / 2;
do {
count++;
m -= 4;
} while (m 4);
if ((wide - 4) % 2 == 0)// 區(qū)別對(duì)待奇偶寬度,奇數(shù)中間最小空1個(gè),偶數(shù)最小空兩個(gè)
{
n = 2 + 4 * (count - 1);
m--;
} else
n = 1 + 4 * (count - 1);
for (i = 0; i count; i++) {
for (j = (count - i) * 2; j 0; j--)
System.out.print(" ");
for (k = 0; k m; k++)
System.out.print("*");
for (t = 0; t n; t++)
System.out.print(" ");
for (k = 0; k m; k++)
System.out.print("*");
m += 4;
n -= 4;
System.out.print("\n");
}
return 0;
}
int LowLevel(int wide) {
int i = 0, j = 0, k = 0;// i控制輸出行,j控制輸出每行前的空格,k控制輸出*
int high = 0, tmp = wide;
do// 計(jì)算所需高度
{
high++;
tmp -= 4;
} while (tmp 4);
high += 1;
for (i = 0; i high; i++) {
for (j = 0; j 2 * i; j++)
System.out.print(" ");
for (k = wide - 4 * i; k 0; k--)
System.out.print("*");
System.out.print("\n");
}
return 0;
}
public static void main(String[] args) {
int wide = 30;
Heart heart = new Heart();
heart.HighLevel(wide);
heart.LowLevel(wide);
}
}
public class Test{
static float count(float x, float y) {
return (float) (Math.pow((x * x + y * y - 1), 3) - x * x * y * y * y);
}
public static void printEmpty() {
int height = 50, width = 100;
int heart[][] = new int[height][width];
float hx = 0.0f, hy = 0.0f;
for (int y = height / 2; y -height / 2; y--) {
for (int x = -width / 2; x width / 2; x++) {
hx = (float) x / (float) (width / 2.8f);
hy = (float) (y) / (float) (height / 2.8f);
if (count(hx, hy) = 0.0f) {
heart[height / 2 - y][x + width / 2] = 1;
}
}
}
for (int y = 1; y height - 1; y++) {
for (int x = 1; x width - 1; x++) {
if (heart[y][x] == 1
!(heart[y][x - 2] == 1 heart[y][x + 2] == 1
heart[y - 1][x] == 1 heart[y + 1][x] == 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
printEmpty();
}
}
用方程的話肯定輸出不會(huì)和你的圖完全一致
心形線方程如圖
用上面行第二個(gè)比較簡(jiǎn)單,程序如下
#include?stdio.h
bool?draw(float?x,?float?y)
{
float?a?=?x?*?x?+?y?*?y?-?1.0;
float?b?=?x?*?x?*?y?*?y?*?y;
return?a?*?a?*?a?-?b?=?0;
}
int?main(int?argc,?char*?argv[])
{
for?(float?y?=?1.5;?y?=?-1.5;?y?-=?0.1)
{
for?(float?x?=?-1.2;?x?=?1.2;?x?+=?0.05)
{
if?(draw(x,?y))
{
bool?left?=?draw(x?-?0.05,?y);
bool?right?=?draw(x?+?0.05,?y);
bool?up?=?draw(x,?y?+?0.1);
bool?down?=?draw(x,?y?-?0.1);
if?(left??right??up??down)
printf("?");
else
printf("*");
}
else
printf("?");
}
printf("\n");
}
return?0;
}
draw函數(shù)是判斷(x,y)坐標(biāo)是否在心形范圍內(nèi)
主函數(shù)循環(huán),y取值±1.5之間,步長(zhǎng)0.1,x取值±1.2之間,步長(zhǎng)0.05
如果(x,y)坐標(biāo)在心形范圍外打印空格
如果(x,y)坐標(biāo)在心形范圍內(nèi),由于這個(gè)心是空心的,要繼續(xù)判斷是不是心形邊緣,判斷周圍4個(gè)點(diǎn)坐標(biāo),如果都在范圍內(nèi),表示(x,y)坐標(biāo)不會(huì)是邊緣,打印空格,否則是邊緣,打印星號(hào)
最終結(jié)果如圖
分析:
1.首先愛心分成兩半前面三行左右一樣右邊就可以通過左邊反相得到
中間兩行是個(gè)長(zhǎng)方形,剩下的就是個(gè)倒等腰三角形
2.在或者,長(zhǎng)得不太像- -
System.out.println(" ** **");
for (int i = 4; i 10; i++) {
for (int a = 0; a i; a++) {
System.out.print(" ");
}
for (int b = 1; b = 2 * 9 - 2 * i - 1; b++) {
System.out.print("*");
}
System.out.print("\n");
}
** **
*********
*******
*****
***
*