C++字母的顺序和逆序输出

试题描述

将26个小写字母在字母表中的顺序编号为1,2,3,……,26,现在给定两个正整数m和n(给定的数据保证0<m<n<27),分别顺序和逆序输出编号从m到n的小写字母。

输入

由一个空格隔开的两个正整数m和n

输出

两行,第一行为顺序字母表,第二行为逆序字母表

输入示例

1 8

输出示例

abcdefgh
hgfedcba

#include <iostream>
using namespace std;

int main(){
int low = 0, high = 0;
cin>>low>>high;
for(int i=low;i <= high;++i)
cout<<static_cast<char>('a' + i - 1);
cout<<endl;

for(int i=high;i >= low;--i)
cout<<static_cast<char>('a' + i - 1);
cout<<endl;

return 0;
}

 执行结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-28
#include <stdio.h>
int main()
{
int m,n,i;
scanf("%d %d", &m, &n );
for( i=m;i<=n;i++ )
printf("%c", 'a'+i-1 );
printf("\n");
for( i=n;i>=m;i-- )
printf("%c", 'a'+i-1 );
printf("\n");
return 0;
}

第2个回答  2014-11-28
#include<iostream>
using namespace std;

char *alpha="abcdefghijklmnopqrtsuvwxyz";

void printRange(const int start, const int end)
{
int s=start-1;
int e=end-1;

while (s<=e)
{
printf("%c",alpha[s]);
++s;
}

printf("\n");

s=end-1;
e=start-1;
while(s>=e)
{
printf("%c",alpha[s]);
--s;
}
}

int main()
{
int s=0,e=0;

cin>>s>>e;

printRange(s,e);

return 0;
}
相似回答