注册 登录
编程论坛 C++教室

逆序数字 新手题

叶落归苏 发布于 2019-10-16 18:34, 2298 次点击
有多组数据,每组数据只有一个不多于5位的正整数,如321
output
几位数
输出每位数,以逗号相隔
按逆序打印各位数,如原数321,应输出123
如输入321
输出
3
3,2,1

[此贴子已经被作者于2019-10-16 19:34编辑过]

9 回复
#2
rjsp2019-10-17 08:23
你的代码呢?
#3
叶落归苏2019-10-17 20:18
回复 2楼 rjsp
程序代码:
#include<stdio.h>
int main()
{

 int long x;

 scanf("%d",&x);
if(x<=9)

 {
  printf("%d\n",1);
  printf("%d\n",x);
  int a;
  printf("%d",a=x);

 }
if(x<99&&x>9)

 {
  printf("%d\n",2);
  printf("%d,%d\n",x/10,x%10);
  int a;
  printf("%d",a=x%10*10+x/10);

 }
if(x<999&&x>99)

 {
  printf("%d\n",3);
  printf("%d,%d,%d\n",x/100,(x%100)/10,x%10);
  int a;
  printf("%d",a=x%10*100+(x%100)/10*10+x/100);
if(x<9999&&x>999)

 {
  printf("%d\n",4);
  printf("%d,%d,%d,%d\n",x/1000,(x%1000)/100,(x%100)/10,x%10);
  int a;
  printf("%d",a=x%10*1000+(x%100)/10*100+(x%1000)/100*10+x/1000);

 }
if(x<99999&&x>9999)

 {
  printf("%d\n",5);
  printf("%d,%d,%d,%d,%d\n",x/10000,(x%10000)/1000,(x%1000)/100,(x%100)/10,x%10);
  int a;
  printf("%d",a=x/10000+(x%10000)/1000*10+(x%1000)/100*100+(x%100)/10*1000+x%10*10000);

 }
}
}





[此贴子已经被作者于2019-10-17 21:14编辑过]

#4
叶落归苏2019-10-17 20:20
回复 2楼 rjsp
加个循环就动不了了···
逆序那个也有点错···
#5
rjsp2019-10-18 09:39
程序代码:
#include <stdio.h>

int main( void )
{
    unsigned x;
    if( scanf("%u",&x)!=1 || x==0 || x>=100000 ) // 不多于5位的正整数
        return 1;

    unsigned n = 0;
    unsigned r = 0;
    for( unsigned t=x; t!=0; t/=10 )
    {
        ++n;
        r = r*10 + t%10;
    }

    printf( "%u\n", n );
    for( unsigned i=0,t=r; i!=n; ++i,t/=10 )
        printf( "%u%c", t%10, ",\n"[i+1==n] );
    printf( "%u\n", r );
}
#6
雪影辰风2019-10-19 19:59
可以用递归写:
前置0版:
程序代码:
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
void doit(long long n) {
    if(n==0)
        return;
    cout<<n%10;
    doit(n/10);
}
int main() {
    ll n;
    cin>>n;
    doit(n);
    return 0;
}

无前置0版:
程序代码:
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
ll ans=0;
void doit(long long n) {
    if(n==0)
        return;
    ans=ans*10+n%10;
    doit(n/10);
}
int main() {
    ll n;
    cin>>n;
    doit(n);
    cout<<ans;
    return 0;
}

不过我还是温馨的提示一下你,如果是做题的话最好要自己做,当不懂的时候可以查查题解,问出一个答案是无意义的,这样就失去了C++或者说所有编程的乐趣了
#7
雪影辰风2019-10-19 20:01
回复 6楼 雪影辰风
增补:
前置0版:倒序后存在前置0,例如:1230->0321
无前置0版:倒序后不存在前置0,例如:1230->321
#8
uouo992019-10-20 11:27
我也提供一种思路吧,可能不是效率最高的,但是实现起来到也简单易懂:

程序代码:
     #include<iostream>
     #include <algorithm>
     using namespace std;
     int main() {
       int iNum = 12345;
      char s[6];//因为最大位5位数,字符串最后以'/0'结尾,所以设置位6.
      string sNum = itoa(iNum, s,10);
      reverse(sNum.begin(), sNum.end());
      int iResNum = atoi(sNum.c_str());
      //cout << iResNum << endl;
       for (int i = 0;i<sNum.size();i++)
       {
        if (i < sNum.size()-1)
          cout << sNum[i] << ",";
        else
          cout << sNum[i];
       }
     }



[此贴子已经被作者于2019-10-20 11:38编辑过]

#9
rjsp2019-10-21 08:38
回复 8楼 uouo99
itoa 是VC的,不是C/C++标准的
C++11标准有 std::to_string
C++17标准有 std::from_chars / std::to_chars

#10
uouo992019-10-21 18:27
回复 9楼 rjsp
谢谢,学习了~
1