题解 | 数字颠倒

#include <stdio.h>

int main() 
{
    int n;
    scanf("%d", &n);
    int count = 0;
    int input=n;
    while (input) 
    {
        count++;
        input = input / 10;
    }//计算数字位数
    if(count==0)
    {
        count=1;
    }
    char* arr = (char*)malloc((count+1)* sizeof(char));
    sprintf(arr, "%d", n); //将数字转化为字符串
    int left = 0;
    int right = count - 1;
    
    while (left <= right) 
    {
        char temp= arr[right];
        arr[right] = arr[left];
        arr[left] = temp;
        left++;
        right--;
    }
    arr[count]='\0';
    printf("%s\n",arr);
    return 0;
}

全部评论

相关推荐

双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
八极星:有什么不能问的,(/_\),这又不是多珍贵的机会,你有什么可失去的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务