啊哈磊_编程从这里起步

标题: 【第四章第10节第1,2题】我用while做的 [打印本页]

作者: cdzjf    时间: 2013-8-20 14:51
标题: 【第四章第10节第1,2题】我用while做的
本帖最后由 cdzjf 于 2013-8-20 14:56 编辑

1.
我对此题做了小小的改动,让用户自己输入要打印多少行星号,这个行是指的正序部分的行数

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,x,y,z,t;
    a=1,c=1;
    printf("请输入要打印几行星号");
    scanf("%d",&t);
    y=t-1;
    system("cls"); //目的是把上面喊用户输入的那几个去掉,影响美观。
    while(a<=t)
     {
      b=0;
      while(b<y)
      {
       printf(" ");
       b++;
      }
     y--;
     x=1;
     while(c>=x)
      {
       printf("*");
       x++;      
      }
     c=c+2;
     printf("\n");
     a++;
     }
    a=1,b=t,c=c-4;  //如果C-2的话,将会出现同样的星号数量,这里开始要递减一个,当然也可以从上面那个就少一个也可以的。本来是递减,但上面执行完程序后,C的值是t次加2的,其值是比最后一行的星星还多2个。
    while(a<t)         //只能小于,不可以小于等于,因为这样后面多对一个空行。
     {
      x=0,y=t+1;      //t+1的目的也是让空格从这行就开始执行。
      while(y>b)
      {
      printf(" ");
      y--;
      }
      b--;
      z=c;       //本来是用C的值,但如果直接用,等下面的Z--(x--)执行完以后C就成0了,星号那个循环就再也进不去了,所有只有用一个跳板
       while(z>x)
       {
        printf("*");
        z--;            
       }
      c=c-2;
      printf("\n");
      a++;
     }     
         system("pause");
        return 0;
}



作者: cdzjf    时间: 2013-8-20 14:52
2.九九乘法表
#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b,c,t;
    a=1;
    b=1;
   // t=10;
    while(a<=9)
     {   
    t=a;
    b=1;
      while(t>=1)
       {
        c=a*b;
        printf("%d*%d=%d ",a,b,c);     
        b++;  
        t--;
       }   
       a++;
       printf("\n");
     }
        system("pause");
        return 0;
}



作者: cdzjf    时间: 2013-8-20 17:31
那个九九乘法表用for做的,开始调试了很久,后来发现for后面不可以有;符号


#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b,c,t,x=9;
    for(a=1;a<=9;a++)
     {
      //t=x,
      b=1;
      for(t=1;t<=a;t++)
       {
       c=a*b;
       printf("%d*%d=%d ",a,b,c);     
       b++;  
       }
      x--;
      printf("\n");
     }
        system("pause");
        return 0;
}

作者: 787199599    时间: 2013-8-31 00:10
膜拜下,盗走源码查看研究。
作者: k70192001    时间: 2014-12-16 16:34
#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b,c;
    for(a=1;a<=9;a++)
    {
                for(b=1;b<=9;b++)
        {
                        c=b*a;
                        printf("%d*%d=%d \n",a,b,c);
        }
    }
   
   
        system("pause");
        return 0;
}

作者: k70192001    时间: 2014-12-16 16:47
擅自改了一些 cdzjf 製作的.

#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b,c,x;
        for(a=1;a<=9;a++)
     {      
                b=1;
                for(x=1;x<=a;x++)
                {
                        c=a*b;
                        printf("%d*%d=%d        ",a,b,c);     
                        b++;  
                }
                printf("\n");
     }
        system("pause");
        return 0;
}
作者: wuyish    时间: 2015-4-29 13:43
[mw_shl_code=c,true]#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b,c;
    for(a=1;a<=9;a++)
        {
                if(a<=5)
                {
                        for(b=1;b<=5-a;b++)
                        {
                                printf(" ");
                }
                for(c=1;c<=2*a-1;c++)
                {
                        printf("*");
                        }
                        printf("\n");
                }
                if(a>5)
                {
                        for(b=1;b<=a-5;b++)
                        {
                                printf(" ");
                }
                for(c=1;c<=19-2*a;c++)
                {
                        printf("*");
                        }
                        printf("\n");
                }
    }
        system("pause");
        return 0;
}[/mw_shl_code]
作者: wuyish    时间: 2015-4-29 13:48
[mw_shl_code=c,true]#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b;
    for(a=1;a<=9;a++)
        {
                for(b=1;b<=a;b++)
                {
                        printf("%d*%d=%d ",b,a,b*a);
                }
                printf("\n");
    }
        system("pause");
        return 0;
}[/mw_shl_code]
作者: 你敬爱的小学夯    时间: 2017-2-14 14:37
cdzjf 发表于 2013-8-20 17:31
那个九九乘法表用for做的,开始调试了很久,后来发现for后面不可以有;符号

定义的x以及后面的x--有什么意义嘛




欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2