iOS 算法~写一个函数,返回数字1-N排列组成的字符串,数字之间通过空格分离.例如1-5.返回”1 2 3 4 5”

//联系人:石虎 QQ:1224614774 昵称:嗡嘛呢叭咪哄


问题:

1.写一个函数,返回数字1-N排列组成的字符串,数字之间通过空格分离.例如1-5.返回”1 2 3 4 5


一、效果图:

图1:

iOS 算法~写一个函数,返回数字1-N排列组成的字符串,数字之间通过空格分离.例如1-5.返回”1 2 3 4 5”


图2:

iOS 算法~写一个函数,返回数字1-N排列组成的字符串,数字之间通过空格分离.例如1-5.返回”1 2 3 4 5”


二、功能实现:


方法1:

#include <stdio.h>

int main(int argc, const char * argv[]) {

    //insert code here...

    

    char a[10];

    gets(a);

    for(int i=0;i<strlen(a);i++)

        printf("%c ",a[i]);

    return 0;

}



方法2:

#include <stdio.h>

int main(int argc, const char * argv[]) {

    //insert code here...

    

    int n,a[100],j,i=0;

    scanf("%d",&n);

    while(n)

    {

        a[i++]=n%10;

        n=n/10;

    }

    for(j=i-1;j>=0;j--)

        printf("%d ",a[j]);

    return 0;

}


谢谢!!!