字母打印机 循环中标记变量的更改

来源:互联网 发布:淘宝商城咨询电话 编辑:程序博客网 时间:2024/06/01 10:46

字母打印机
Time Limit: 1000MS Memory Limit: 512KB
Submit Statistic
Problem Description

bLue 有一个神奇的机器,这个机器会根据获得的一个数字 n,然后根据这个获得数字打印一串包含 n 个字符的字符串。
打印规则:从 ‘a’ 开始,按字母递增的顺序打印,到 ‘z’ 之后调转方向,按递减顺序打印,回到 ‘a’ 后再递增,如此循环往复。即按照 ‘a’, ‘b’, ‘c’, …, ‘x’, ‘y’, ‘z’, ‘y’, ‘x’, …’b’, ‘a’, ‘b’, … 的顺序打印,直到打印够 n 个字符停止。
这个机器就是这么的神奇,但是 bLue 的打印机坏掉了,你能帮他修一下吗?
Input

输入数据有多组(数据组数不超过 50),到 EOF 结束。
对于每组数据,输入一行,包含 1 个整数 n (1 <= n <= 10^6)。
Output

对于每组数据,输出一行字符串,表示打印结果。
Example Input

5
60
Example Output

abcde
abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcbabcdefghij

#include <stdio.h>int main(){    int i,j,k;    long long int n;    while(scanf("%lld",&n)!=EOF)    {        int end=25,sta=0,x=0;        for(i=1; i<=n; i++)\\分情况输出        {            if(i<=26)            {                printf("%c",'a'+i-1);            }            else\\>26后分两种情况             {                end--;sta++;x++;                if(x<25) printf("%c",'a'+end);                if(x==25)                {                    printf("%c",'a'+end);                    end=25;sta=0; \\初始化                }                if(x>25&&x<50)                {                    printf("%c",'a'+sta);                }                if(x==50)                {                    printf("%c",'a'+sta);                    x=0;end=25;sta=0;\\初始化                }            }        }        printf("\n");    }    return 0;}

way.2

#include <stdio.h>#include <stdbool.h>//有真假的函数int main(){    int n;    while (~scanf("%d", &n))    {        int num = 0,i;        char c = 'a';        bool flag = true;        for (i = 0; i < n; i++)        {            printf("%c", c);            if (flag)            {                if (c == 'z')                {                    flag = false;//循环到z时,标记变量更改。                    c -= 2;//照顾后边的c++.                }                c++;            }            else            {                if (c == 'a')                {                    flag = true;                    c += 2;                }                c--;            }        }        printf("\n");    }    return 0;}
0 0