C++.Primer.Plus.第五版.中文版.pdf 第五章编程练习题第9题
C++.Primer.Plus.第五版.中文版.pdf 第五章编程练习第9题;
就是147页最后的那道题,
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int x;
cout << "Enter number of rows: \n";
cin >> x;
for(int y=0;y<x;y++)
{
for(int z=0;z<x-y-1;z++)
cout << '.';
for(int q=0;q<=y;q++)
cout << '*';
cout << endl;
}
return 0;
}
是这种意思么?还有比我的这个更简明的么;。
------解决思路----------------------
还可以减少一个变量:
------解决思路----------------------
[img=http://my.****.net/my/album/detail/1337793][/img]
就是147页最后的那道题,
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int x;
cout << "Enter number of rows: \n";
cin >> x;
for(int y=0;y<x;y++)
{
for(int z=0;z<x-y-1;z++)
cout << '.';
for(int q=0;q<=y;q++)
cout << '*';
cout << endl;
}
return 0;
}
是这种意思么?还有比我的这个更简明的么;。
------解决思路----------------------
还可以减少一个变量:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int n, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i - 1; j++)
cout << '.';
for(j = 0; j <= i; j++)
cout << '*';
cout << endl;
}
return 0;
}
------解决思路----------------------
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int n, i, j;
cout << "Enter number of rows: ";
cin >> n;
i = j = 0;
while(i<n) { // n记录行数
j=++i; //++i先自增后引用,记录输出的"-"的个数。
while(j<n) {
cout<<"-";
j++;
}
j=i; //表示输出"*"的个数。
while(j>0) {
cout<<"*";
j--;
}
cout<<endl;
}
return 0;
}
[img=http://my.****.net/my/album/detail/1337793][/img]