#include<iostream>
using namespace std;
int main()
{
int n,m=1;
cout<<"输入n:";
cin>>n;
int a[n][n],i=0,j=n/2;
while(m<=n*n)
{
a[i][j]=m;i--;j++;m++;
if(m>2&&(m-1)%n==0){i+=2;j--;}
if(i<0)i=n-1;
if(j>n-1)j=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
汉诺塔
#include<iostream>
using namespace std;
void d(int n,char a,char b,char c)
{
if(n==1)cout<<n<<':'<<a<<"-->"<<c<<endl;
else
{
d(n-1,a,c,b);
cout<<n<<':'<<a<<"-->"<<c<<endl;
d(n-1,b,a,c);
}
}
int main()
{
int n;cout<<"输入n:";
cin>>n;
d(n,'!','@','$');
return 0;
}
红色爱心
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main(int argc,char *argv[])
{
float x,y,a;
for(y=1.5;y>-1.5;y-=0.1)
{
for(x=-1.5;x<1.5;x+=0.05)
{
a=x*x+y*y-1;
putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');
}
system("color 0c");
putchar('\n' );
}
printf("爱你哦YY\n");
printf(" ----天天");
return 0;
}
变色爱心
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <conio.h>
int main()
{
for (float y = 1.5f; y > -1.5f; y -= 0.1f)
{
for (float x = -1.5f; x < 1.5f; x += 0.05f)
{
float a = x * x + y * y - 1;
putchar(a * a * a - x * x * y * y * y <= 0.0f ? '*' : ' ');
}
putchar('\n');
}
for(;!kbhit();) //kbhit()位于conio.h中,检测是否有键按下
{
char str[10];
sprintf(str,"color %x",rand() % 16); //形成一条color X的字符串,注意要用%x
system(str);
sleep(0.5);
}
printf("YY\n");
printf(" ----天天");
return 0;
}
动态爱心
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>
float f(float x, float y, float z) {
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z) {
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f)
return y;
return 0.0f;
}
int main() {
HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
_TCHAR buffer[25][80] = { _T(' ') };
_TCHAR ramp[] = _T(".:-=+*#%@");
for (float t = 0.0f;; t += 0.1f) {
int sy = 0;
float s = sinf(t);
float a = s * s * s * s * 0.2f;
for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
_TCHAR* p = &buffer[sy++][0];
float tz = z * (1.2f - a);
for (float x = -1.5f; x < 1.5f; x += 0.05f) {
float tx = x * (1.2f + a);
float v = f(tx, 0.0f, tz);
if (v <= 0.0f) {
float y0 = h(tx, tz);
float ny = 0.01f;
float nx = h(tx + ny, tz) - y0;
float nz = h(tx, tz + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
*p++ = ramp[(int)(d * 5.0f)];
}
else
*p++ = ' ';
}
}
for (sy = 0; sy < 25; sy++) {
COORD coord = { 0, sy };
SetConsoleCursorPosition(o, coord);
WriteConsole(o, buffer[sy], 79, NULL, 0);
}
Sleep(33);
}
}
圣诞树
#include<stdio.h>
#include<stdlib.h>
void hang (int *p,int n,int s){
for(int i=0;i<n;i++){
*(p+i)=s;
}
}
int main(){
system("color 0A");//控制台和字体颜色
system("mode con cols=102 lines=47"); //控制台大小
int tree[100][99]={0};
//--------星形坐标
tree[0][49]=1;tree[1][47]=1;tree[1][48]=1;tree[1][49]=1;tree[1][50]=1;tree[1][51]=1;tree[2][48]=1;tree[2][50]=1;
//----
hang(&tree[2][49],1,2);hang(&tree[3][47],5,2);hang(&tree[4][45],9,2);hang(&tree[5][43],13,2);
hang(&tree[6][47],5,2);hang(&tree[7][45],9,2);hang(&tree[8][43],12,2);hang(&tree[9][41],17,2);hang(&tree[10][39],21,2);hang(&tree[11][37],25,2);
hang(&tree[12][43],12,2);hang(&tree[13][41],17,2);hang(&tree[14][39],21,2);hang(&tree[15][37],24,2);hang(&tree[16][35],28,2);hang(&tree[17][32],35,2);
hang(&tree[18][37],25,2);hang(&tree[19][35],28,2);hang(&tree[20][32],35,2);hang(&tree[21][29],40,2);hang(&tree[22][26],47,2);hang(&tree[23][23],53,2);
//树根
hang(&tree[24][47],5,3);hang(&tree[25][45],5,3);hang(&tree[26][47],5,3);hang(&tree[27][47],5,3);hang(&tree[28][47],5,3);hang(&tree[29][47],5,3);hang(&tree[30][47],5,3);
hang(&tree[31][47],5,3);hang(&tree[32][47],5,3);hang(&tree[33][47],5,3);hang(&tree[34][47],5,3);hang(&tree[35][47],5,3);hang(&tree[36][47],5,3);hang(&tree[37][47],5,3);
//树上装饰点
tree[24][23]=4;tree[25][22]=5;tree[24][35]=4;tree[25][34]=6;tree[8][50]=6; tree[19][58]=6;tree[16][40]=7;tree[15][55]=8;tree[21][39]=9;tree[12][49]=10;//树上装饰点
//---------
tree[37][67]=6;tree[36][67]=6;tree[37][68]=6;tree[36][68]=6;tree[35][68]=12;tree[35][69]=11;
//
for(int i=0;i<40;i++){
for(int j=0;j<100;j++){
if(tree[i][j]==1){
printf("\033[40;33;1m*\033[1m");
}
else if(tree[i][j]==2){
printf("\033[40;32;1m*\033[1m");
}else if(tree[i][j]==3){
printf("\033[40;33;5m*\033[5m");
}else if(tree[i][j]==4){
printf("\033[40;31;5m|\033[5m");
}else if(tree[i][j]==5){
printf("\033[40;31;5m●\033[5m");
}else if(tree[i][j]==6){
printf("\033[40;31;5m■\033[5m");
}else if(tree[i][j]==7){
printf("\033[40;35;5m●\033[5m");
}else if(tree[i][j]==8){
printf("\033[40;36;5m■\033[5m");
}else if(tree[i][j]==9){
printf("\033[40;37;5m●\033[5m");
}else if(tree[i][j]==10){
printf("\033[40;33;5m☆\033[5m");
}else if(tree[i][j]==11){
printf("\033[40;31;5m/\033[5m");
}else if(tree[i][j]==12){
printf("\033[40;31;5m\\\033[5m");
}else if(tree[i][j]==13){
printf("\033[40;33;5m/\033[5m");
}else if(tree[i][j]==14){
printf("\033[40;33;5m\\033[5m");
}else{
printf(" ");
}
}
printf("\n");
}
system("pause");
return 0;
}