关于循环的一道题- -#

NewManC 2008-06-06 11:17:44
是C Primer plus 上的一道题...想了很久还是写不出来,,
所以来这里求助下..

原题:
例如...输入大写字母E 输出
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

知道思路..但就是写不出来,,
int i,space;
char c,ch;
输入的字母存放在c中,,,
for(i='A';i<=c-'A';i++) //外循环控制行,,
{
for(space=c-'a';space>0;space--) //内循环1控制空格..
{
printf(" ");
for("ch='A';ch<=c-'A';ch++) //内循环2控制升序字母..
{
printf("%c",ch);
for(.....) //内循环3控制降序字母. .
这里我写不出来了,,,想了很久,,...
小菜鸟请教大家帮下忙.,,
...全文
130 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
IanFang 2008-06-07
  • 打赏
  • 举报
回复

#include <stdio.h>

int main(int argc, char *argv[])
{
int total_line;
int cur_line = 0;
int space_counter;

int c_begin, c_end;


printf("It can display n Lines,line:");

scanf("%d", &total_line);

if(total_line > 26 || total_line < 0)
{
fprintf(stderr, "error!\n");
exit(1);
}

while(cur_line <= total_line)
{
c_begin = 'A';
c_end = c_begin + cur_line;

space_counter = total_line - cur_line;

while(space_counter--)
{
printf(" ");
}

while(c_begin < c_end)
{
printf("%c", c_begin++);
}

--c_begin;

while(--c_begin >= 'A')
{
printf("%c", c_begin);
}

printf("\n");

++cur_line;
}

system("pause");

return 0;
}

have a try..
lin_style 2008-06-06
  • 打赏
  • 举报
回复
俺的可以随便指定开始字符。中间字符。和最终字符。。甚至控制增量
visame 2008-06-06
  • 打赏
  • 举报
回复

1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 char ch;
7 cin>>ch;
8 for (int i=1;i<=ch-'A'+1;++i)
9 {
10 for (int j=0;j<=ch-'A'-i;++j)
11 cout<<' ';
12 for (int j=1;j<=i;++j)
13 {
14 cout<<char('A'+j-1);
15 }
16 for (int j=i-2;j>=0;--j)
17 {
18 cout<<(char)('A'+j);
19 }
20 for (int j=0;j<=ch-'A'-i;++j)
21 cout<<' ';
22 cout<<endl;
23 }
24 }
gezichong 2008-06-06
  • 打赏
  • 举报
回复
F
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
Press any key to continue
lin_style 2008-06-06
  • 打赏
  • 举报
回复
chEnd就是你的最终字符

楼上几个不厚道。。。
欺负递归。。
gezichong 2008-06-06
  • 打赏
  • 举报
回复

#include <stdio.h>
int main(){
char input;
int i,num,j;

scanf("%c",&input);

if ((input <'A')||(input >'Z'))
{
printf("input error\n");
return 1;
}

num = input - 'A' + 1;

//n=5;
for(i=0;i<num;i++ )
{
for(j=0;j <(num-i);j++)
printf(" ");
for(j=0;j <=i;j++)
printf("%c",(char)('A'+j));
for(j--;j>0;j--)
printf("%c",(char)('A'+j-1));
printf("\n");
}


return 0;
}
lin_style 2008-06-06
  • 打赏
  • 举报
回复
void forPrint(char ch)
{
char chFlag='a';
int i=ch-chFlag, j;
if( ch==chEnd+1 )
{
return ;
}
for(j=0; j<i; ++j)
{
cout<<chFlag++;
}
cout<<ch;
for(j=0; j<i; ++j)
{
cout<<--chFlag;
}
cout<<endl;
forPrint(++ch);
}

int main()
{
forPrint('a');

return 0;
}


a
aba
abcba
abcdcba
abcdedcba
Press any key to continue


应该可以了。没做其他测试
自己加下空格,即最终字母减去当前中间的字母
xhd3767 2008-06-06
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
#define N 5
int main(){
for(int i=0;i<N;i++){
for(int j =0;j<N-i;j++){
cout<<" ";
}
for(int m=0;m<i;m++){

cout<<(char)('A'+m);
}
for(int n = 0;n<=m;n++){

cout<<(char)(('A'+m)-n);
}
cout<<endl;
}

return 0;
}


C++实现的....
gezichong 2008-06-06
  • 打赏
  • 举报
回复
#include <stdio.h>
int main(){
char input;
int i,num,j;

scanf("%c",&input);

if ((input <'A')||(input >'Z'))
{
printf("input error\n");
return 1;
}

num = input - 'A' + 1;

//n=5;
for(i=0;i<num;i++ )
{
for(j=0;j <(num-i);j++)
printf(" ");
for(j=0;j <=i;j++)
printf("%c",(char)('A'+j));
for(j--;j>0;j--)
printf("%c",(char)('A'+j-1));
printf("\n");
}


return 0;
}
iu_81 2008-06-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
char ch;
cout<<"please input character:";
cin>>ch;
int n=ch-'A';
for(int i=0;i<=n;i++)
{
cout<<setw(n-i+1);
for(int j=0;j<=i;j++)
{
cout<<char('A'+j);
}
for(j=i;j>0;j--)
{
cout<<char('A'+j-1);
}
cout<<setw(n-i+1);
cout<<endl;
}
return 0;
}
iu_81 2008-06-06
  • 打赏
  • 举报
回复

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
char ch;
cout<<"please input character:";
cin>>ch;
int n=ch-'A';
for(int i=0;i<=n;i++)
{
cout<<setw(n-i);
for(int j=0;j<=i;j++)
{
cout<<char('A'+j);
}
for(j=i;j>0;j--)
{
cout<<char('A'+j-1);
}
cout<<setw(n-i);
cout<<endl;
}
return 0;
}
NewManC 2008-06-06
  • 打赏
  • 举报
回复
怎么对不齐......
输出时是打印个三角形的.
NewManC 2008-06-06
  • 打赏
  • 举报
回复
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

这样的..

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧