提取数字从一个整数,未sprintf的()或模 [英] Extract Digits From An Integer Without sprintf() Or Modulo

查看:137
本文介绍了提取数字从一个整数,未sprintf的()或模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做的要求,因为这最终将是(a GPU)实现的机器有所限制。

我有一个无符号整数,我试图提取每个数字。

如果我是在C ++中正常硬件和放大器这样做;性能不是大问题,我可能会做这样的:

(不要恨上我,这code,它只是一个样本,以说明该方法)

 的#define _CRT_SECURE_NO_WARNINGS

#包括< cstdlib>
#包括<字符串>
#包括<的iostream>
#包括<算法>
使用名字空间std;

诠释的main()
{
    INT someVal = 1234;

    焦炭stringVal [256] = {0};
    sprintf的(stringVal,%016D,someVal);

    INT位[16] = {0};
    的for(int i = 0; I< strlen的(stringVal); ++ I)
    {
        数字[I] = stringVal [Ⅰ]  - '0';
    }

    COUT<< 整数值=<< someVal<< ENDL;
    COUT<< 提取位数=;
    副本(放大器;数字[0],和放大器;数字[16],ostream_iterator< INT>(COUT, - ));
    COUT<< ENDL;

    返回0;
}
 

我试图找到一种方法来提取这些数字有以下限制:

  1. 请不要整数转换为字符串
  2. 请不要使用模运算(浮点除法是罚款)
  3. 在考虑中的值是一个32位无符号整数

我在寻找一种算法,不一定是具体的code。但具体的code将是巨大的。我最熟悉的是很好的转化为我的目标硬件的语言是C ++,C和汇编。

任何想法?

编辑:下面是与我实现了一个基于注释和放大器的算法的更新;下面的链接。感谢所有。

 的#define _CRT_SECURE_NO_WARNINGS

#包括< cstdlib>
#包括<字符串>
#包括<的iostream>
#包括<算法>
#包括< CMATH>
使用名字空间std;

诠释的main()
{
    无符号someVal = 12345678;
    静态常量无符号numDigits = 10;
    无符号数位[numDigits] = {0};

    为(无符号​​I = 0,温度= someVal; I< numDigits ++我,温度/ = 10)
    {
        数字[numDigits-I-1] =温度 -  10 *(温度/ 10)/ *温度%10 * /;
    }


    COUT<< 整数值=<< someVal<< ENDL;
    COUT<< 提取位数=;
    副本(放大器;数字[0],和放大器;数字[numDigits],ostream_iterator< INT>(COUT, - ));
    COUT<< ENDL;

    返回0;
}
 

解决方案

记住,模运算符实际上可以实现为:

  MOD(A,N)= A  -  N *楼(A / N)
 

因此​​,你可以使用自己喜欢的模数的算法。您可以通过类型转换模拟地板本身。

The requirements of this are somewhat restrictive because of the machinery this will eventually be implemented on (a GPU).

I have an unsigned integer, and I am trying to extract each individual digit.

If I were doing this in C++ on normal hardware & performance weren't a major issue, I might do it like this:

(Don't hate on me for this code, it's just a sample to illustrate the method)

#define _CRT_SECURE_NO_WARNINGS

#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int someVal = 1234;

    char stringVal[256] ={0};
    sprintf(stringVal, "%016d", someVal);

    int digits[16] = {0};
    for( int i = 0; i < strlen(stringVal); ++i )
    {
        digits[i] = stringVal[i] - '0';
    }

    cout << "Integer Value = " << someVal << endl;
    cout << "Extracted Digits = ";
    copy( &digits[0], &digits[16], ostream_iterator<int>(cout, "-") );
    cout << endl;

    return 0;
}

I'm trying to find a method to extract these digits with the following restrictions:

  1. Don't convert the integer to a string
  2. Don't use the modulus operator (floating point division is fine)
  3. The value in question is a 32-bit unsigned integer

I'm looking for an algorithm, not necessarily specific code. But specific code would be great. The languages I'm most familiar with that translate well to my target hardware are C++, C and assembler.

Any ideas?

EDIT: Here's an update with the algorithm I implemented based on the comments & links below. Thanks all.

#define _CRT_SECURE_NO_WARNINGS

#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
    unsigned someVal = 12345678;
    static const unsigned numDigits = 10;
    unsigned digits[numDigits] = {0};

    for( unsigned i = 0, temp = someVal; i < numDigits; ++i, temp /= 10 )
    {
        digits[numDigits-i-1] = temp - 10 * (temp/10)      /*temp % 10*/;
    }


    cout << "Integer Value = " << someVal << endl;
    cout << "Extracted Digits = ";
    copy( &digits[0], &digits[numDigits], ostream_iterator<int>(cout, "-") );
    cout << endl;

    return 0;
}

解决方案

Remember that the modulo operator can actually be implemented as:

mod(a, n) = a - n * floor(a / n)

Hence, you can use your favorite modulo based algorithm. You can simulate floor itself by typecasting.

这篇关于提取数字从一个整数,未sprintf的()或模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆