打印菱形图形
编程打印下列图形:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
ABCDEFGHIJKLMNO
ABCDEFGHIJKLMNOPQ
ABCDEFGHIJKLMNOPQRS
ABCDEFGHIJKLMNOPQ
ABCDEFGHIJKLMNO
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
View Code #include <tchar.h>
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string strFull = "ABCDEFGHIJKLMNOPQRS";
size_t charNum = strFull.length();
int i = 1; // how many chars on a row.
int cnt = 0; // how many rows we have printed.
bool bGrow = true; // string increase or decrease direction flag.
while (cnt < charNum)
{
int spaceNum = (int)(charNum - i) / 2; // how many spaces we need to print.
string strSpace(spaceNum, ' ');
string realStr = strFull.substr(0, i);
cout << strSpace << realStr << strSpace << endl;
if(i == charNum) // after printing the whole string, reverse the print rule.
{
bGrow = false;
}
if(bGrow)
i += 2;
else
i -= 2;
cnt++;
}
return 0;
}
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string strFull = "ABCDEFGHIJKLMNOPQRS";
size_t charNum = strFull.length();
int i = 1; // how many chars on a row.
int cnt = 0; // how many rows we have printed.
bool bGrow = true; // string increase or decrease direction flag.
while (cnt < charNum)
{
int spaceNum = (int)(charNum - i) / 2; // how many spaces we need to print.
string strSpace(spaceNum, ' ');
string realStr = strFull.substr(0, i);
cout << strSpace << realStr << strSpace << endl;
if(i == charNum) // after printing the whole string, reverse the print rule.
{
bGrow = false;
}
if(bGrow)
i += 2;
else
i -= 2;
cnt++;
}
return 0;
}


浙公网安备 33010602011771号