C/C++ 數值(int) 拆成 二個數值(int)

2009-03-24 10:08 am
請問各位 C/C++ 的高手前輩們,

我想要把一個數值,分別拆成二個數值,

例如 A=24 ,我想要把"24" 變成 B=2 ; C=4;

要把一個整數 (int) ,拆成二個數值 (int)

並且要分別存入到變數中。

想請教一下大大,我該如何寫呢?謝謝。

回答 (3)

2009-03-24 12:30 pm
✔ 最佳答案
#include<iostream.h>
#pragma argsused
int main(int argc, char* argv[])
{
int num,a,b;
cout<<"Enter a num of 10~99:";
cin>>num;
a=num/10;
b=num%10;
cout<<a<<endl;
cout<<b<<endl;
system("pause");
return 0;
}

10位數用除10找
個位數用%10找
參考: 我
2009-03-24 5:32 pm
我有另類想法QQ
應該可以輸入時用字元分別讀取
在要分別存入變數時強制變成整數存入
2009-03-24 10:56 am
#include <stdio.h>

void main()
{
int n=24;
int x,y;

x = 24 %10;
y = 24 - x * 10;
}

/*
you can also use itoa() to convert integer to string and use atoi() to convert that back to integer after parsing it into smaller strings.
*/


收錄日期: 2021-04-30 12:53:19
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090324000015KK01096

檢視 Wayback Machine 備份