202008xx給定一個整數n(1<=n<=1000000000),要求從個位開始分離出它的每一位數字,從個位開始按照從低位到高位的順序依次輸出每一位數字(奧賽一本通p64 4題)


//給定一個整數n(1<=n<=1000000000),要求從個位開始分離出它的每一位數字,從個位開始按照從低位到高位的順序依次輸出每一位數字(奧賽一本通p64 4題)
//第一種解法 ,用到了stringstream,沒用to_string,因為dev5.92版本不支持這個命令
# include <iostream>
# include <sstream>
# include<cstdio>
# include<string>
using namespace std;
int main()
{
  stringstream ss;
  int b,c;
  int n;
  cin>>n;
  string str;
  //n的值先轉換到ss里存儲,再從ss賦給str
  ss<<n;
  ss>>str;
  b=str.length();
  // cout<<b<<endl;
  for(int e=b-1;e>=0;e--)
    {
    cout<<str.substr(e,1)+" ";
    }

  return 0;
}

//第二種解法
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int x;//用於存儲求出來的值
  int n;
  cin>>n;
  do
    {
     x=n%10;
     n/=10;
     printf("%d ",x);
  }while(n>0);

  return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM