7 条题解

  • 3
    @ 2023-6-23 9:04:12
    #include<iostream>
    #include<iomanip>
    using namespace std;
    int main(){
    	int m;
    	cin>>m;
    	while(m!=0){
    		cout<<m%10<<" ";
    		m = m/10;
    	}
    }
    
    • 2
      @ 2024-2-24 11:42:49

      三种方法


      1.数位拆分

      直接用a%10取到最后一位,a/10去除最后一位,直到a=0

      #include <bits/stdc++.h>
      using namespace std;
      int a;
      int main()
      {
          cin>>a;
          while (a){
              cout<<a%10<<" ";
              a/=10;
          }
          return 0;
      }
      

      不过这有缺点,如果a超出数据范围,就不能用。


      2.字符串

      直接输入字符串,在从后向前遍历字符,逐个输出

      #include <bits/stdc++.h>
      using namespace std;
      string s;
      int lens;
      int main()
      {
          cin>>s;
          lens=s.size();
          for (int i=lens-1;i>=0;i--){
              cout<<s[i]<<" ";
          }
          return 0;
      }
      

      可以输入的数的长度大大增加。


      3.字符

      逐个输入字符,从后向前输出。

      #include <bits/stdc++.h>
      using namespace std;
      vector<char> ve;
      char s;
      int main(){
          while (cin>>s){//cin如果得到输入会返回true,否则返回false
              ve.push_back(s);
          }
          for (int i=ve.size()-1;i>=0;i--)  cout<<ve[i]<<" ";
          return 0;
      }
      
      • 2
        @ 2023-7-17 0:30:27

        py:

        n = int(input())
        
        digits = []
        while n > 0:
            digit = n % 10
            digits.append(str(digit))
            n //= 10
        
        output = ' '.join(digits)
        print(output)
        
        
        • 1
          @ 2024-4-6 19:18:03
          #include<bits/stdc++.h>
          using namespace std;
          char n[10];
          int main()
          {
          	cin>>n;
              int len = strlen(n);
              for(int i=len-1;i>=0;i--) cout<<n[i]<<' ';
              return 0;
          }
          
          • 1
            @ 2023-7-22 19:28:23
            #include <iostream>
            int main(){
                int q;std::cin>>q;
                while(q>0){
                    std::cout<<q%10<<" ";
                    q/=10;}
                return 0;}
            
            • 1
              @ 2023-7-7 23:07:09

              一道分离数位的题

              在获取n以后,如果n > 0就通过n % 10来取出个位数并输出,然后将n /= 10用于去掉已经输出的个位数

              #include <iostream>
              using namespace std;
              
              int main()
              {
                  int n;
                  cin >> n;
                  while(n > 0)
                  {
                      cout << n % 10 << ' ';
                      n /= 10;
                  }
                  return 0;
              }
              
              • 0
                @ 2024-5-31 22:10:43
                #include <iostream>
                using namespace std;
                int main()
                {
                    int n ;
                    cin >>n;
                    while(n!=0)
                    {
                        cout << n %10<<" ";
                        n /=10;
                    }
                    return 0;
                }```分离数位可以先从各位开始依次输出,模10就行
                
                • 1

                信息

                ID
                184
                时间
                1000ms
                内存
                128MiB
                难度
                1
                标签
                递交数
                132
                已通过
                88
                上传者