假设我想检查一个数字n = 123是否有重复的数字。我尝试了以下代码:
#include <iostream>
using namespace std;
int main() {
int n = 123;
int d1 = n % 10;
int d2 = ( n / 10 ) % 10;
int d3 = ( n / 100 ) % 10;
if( d1 != d2 && d1 != d3 && d2 != d3 ) {
cout << n << " does not have duplicate digits.\n";
}
}
有没有更快的解决方法?
更新
抱歉没有表述清楚。上面的代码只是为了描述目的而编写的C ++代码。我必须在TI-89中解决这个问题,该数字由9位数字组成。由于内存和速度的限制,我正在寻找最快的方法。
TI-89仅具有几个条件关键字:
- If
- If ... Then
- when(
- For ... EndFor
- While ... EndWhile
- Loop ... EndLoop
- Custom ... EndCustom
谢谢,
Chan
n = 1
将被拒绝,因为它具有重复的数字(前导零)。 - Thilo