Merry Christmas

本文介绍了一个简单的程序,该程序接收两个整数输入:高度和树干长度,并使用这些参数来绘制一个文本形式的圣诞树。通过巧妙地利用循环和字符串操作,程序能够在控制台上输出一个由点构成的圣诞树图案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. /*   <Merry Christmas>                                                                                       **
  2. **  input:The input consists of multiple test cases. Each test case consists of two positive intergers m,n.  **
  3. **        And m means the height of the triangle, n means the length of the trunk.                           **
  4. **                                                                                                           **
  5. **  output:The picture of the tree like the sample shows. You shouldn't add any extra blanks after each line,**
  6. *************************************************************************************************************/
  7. //code begin
  8. #include <iostream>
  9. #include <string>
  10. using namespace std;
  11. int main()
  12. {
  13.     int i_height,i_trunk,length,i,j,m,n;
  14.     string input;
  15.     while(1)
  16.     {
  17.         getline(cin,input);
  18.         const char*pstr = input.c_str();
  19.         i_height = *(pstr)-48;
  20.         i_trunk = *(pstr+2)-48;
  21.     
  22.         if(i_height == 0||i_trunk == 0) break;//there is 0 in input,then end the while.
  23.     
  24.         length = i_height;
  25.     
  26.         //output the triangle
  27.         for(i = 1;i <= i_height;i++)
  28.         {
  29.             for(j = 0;j < length;j++)
  30.             {
  31.                 if(j < length-2*i+1)    
  32.                     cout<<" ";
  33.                 else 
  34.                     cout<<".";
  35.             }
  36.             cout<<endl;
  37.             length++;
  38.         }
  39.         //output the trunk
  40.         for(m = 0;m < i_trunk;m++)
  41.         {   for(n = 0;n < i_height-1;n++)
  42.                 cout<<" ";
  43.             cout<<"|"<<endl;
  44.         }
  45.     }
  46.     return 0;
  47. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值