kaprekar数字计算超过两位数失败

时间:2018-06-30 13:51:26

标签: c

为什么代码不起作用?我无法获得有效的输出。请帮我看看输入的是否是Kaprekar数字。我认为我做的太复杂了。对简单方法有何建议?

例如:

  • 9是一个Kaprekar数,因为9²= 81&8 + 1 = 9。
  • 297是一个Kaprekar数,因为297²= 88209&88 + 209 =297。下面是代码。

代码如下:

#include <stdio.h>

int main() {
    int n, a, temp, length, sum = 0, s, i, rem, temp1, length1, sum1[10], sum2[10];

    printf("n");
    scanf("%d", &n);

    a = n * n;
    printf("%d\n", a);

    temp = a;
    length = 0;

    while (temp > 0) {    
        length++;
        temp = temp / 10;    
    }

    temp1 = n;
    length1 = 0;

    while (temp1 > 0) {    
        length1++;
        temp1 = temp1 / 10;    
    }

    if (length1 == 1) {    
        for (i = 0; i < length; i++) {        
            rem = a % 10;
            sum = sum + rem;
            a = a / 10;
        }
        if (sum == n) {        
            printf("Kaprekar number");
        } else {        
            printf("not a kaprekar number");        
        }    
    }

    if (length1 > 1) {   
        s = length / 2; 

        for (i = 0; i < s; i++) {        
            printf("%d\n", sum1[i]);         
        }

        for (i = s; i < length; i++) {        
            printf("%d\n", sum2[i]);        
        }

        for (i = 0; i < length; i++) {        
            sum3[i] = sum1[i] + sum2[i];        
        } 
    }
    return 0;
}

3 个答案:

答案 0 :(得分:1)

这是我的建议,以简化您的代码,并希望使其更易于调试。

  1. 定义一个返回数字位数的函数。
  2. 在给定原始数字中的位数的情况下,定义将平方分为数字的函数。
  3. 计算平方的各个部分的总和,并将其与原始数字进行比较。

int getNumberOfDigits(int n)
{
   if ( n == 0 )
   {
      return 1;
   }

   int count = 0;
   while ( n / 10 > 0 )
   {
      n /= 10;
      ++count;
   }

   return count;
}

void divideSquare(int square, int originalDigitCount, int out[2])
{
    int left = square;
    int right = 0;
    for ( int = 0; i < originalDigitCount; ++i )
    {
       right  = right*10 + left%10;
       left /= 10;
    }
    out[0] = left;
    out[1] = right;
}

int main()
{
    for (int n = 0; n < 10000; ++n )
    {
       int square = n * n;
       int out[2];
       int count = getNumberOfDigits(n);
       divideSquare(square, count, out);
       if ( out[0] + out[1] == n )
       {
          printf("%d is a Kaprekar number\n", n);
       }
    }
 }

答案 1 :(得分:0)

您的代码存在一些错误,因为您尚未初始化用于打印和添加的sum1sum2数组。其次,未定义数组sum3[]。同样,如果您的数字大于2,147,483,647,则意味着如果a大于46340,您将溢出。

我对这个问题感兴趣,所以这是我的实现方式

#include <stdio.h>
#include <math.h>

int get_length(long long n){
    int s = 0;
    while (n>0){
        s++;
        n /= 10;
    }
    return s;
}

int is_kaprekar (long long n){
    long long t, n2=n*n;
    int i,s = get_length(n2);
    t = pow(10,s);
    for (i=0 ; i<s ; i++){
        long long left,right;
        left = n2/t;
        right = n2%t;
        if ((long long)(left+right) == n && left != t){
            printf("%lld\n",n); // print n
            printf("%lld\n",n2); // print n^2
            printf("%lld\n",left); 
            printf("%lld\n",right);
            printf("Kaprekar number\n");
            return 1;
        }
        t/=10;
    }
    printf("not a kaprekar number");
    return 0;
}

int main()
{
    long long n,i,n2;
    printf("n ");
    scanf("%d",&n); // get n
    is_kaprekar(n);

    return 0;
}

答案 2 :(得分:0)

根据此Wikipedia article,按以下方式定义Kaprekar数字:

  

在数学中,如果给定基数的平方表示形式可以分为两个部分,将其与原始数相加,则该非负整数被称为给定基数的 Kaprekar数。前提是由正方形的低位数字组成的部分必须为非零,尽管允许包含前导零。例如,45是一个Kaprekar数字,因为45 2 = 2025和20 + 25 =45。数字1在每个基数中都是Kaprekar,因为1 2 = 01在任何基数,且0 +1 =1。Kaprekar数字以DR Kaprekar命名。

您的方法似乎过于复杂且无法修复。这是一种更简单的方法:

  • 平方数。我建议使用更大的类型,例如unsigned long long,以避免在计算过程中出现算术溢出。
  • 循环迭代,将平方除以10的幂,直到除数小于10。如果余数不为零,并且平方的商和除数之和除以10的幂就等于原始乘方号,您有一个Kaprekar号。

这是一个简单的实现:

#include <stdio.h>

int test_kaprekar(unsigned long long n) {
    unsigned long long quo, rem, pow10, square = n * n;
    int i;

    for (i = 1, pow10 = 10;; i++, pow10 *= 10) {
        quo = square / pow10;
        rem = square % pow10;
        if (quo + rem == n && rem != 0) {
            printf("%llu is a Kaprekar number: %llu*%llu = %llu, %llu+%.*llu = %llu\n",
                   n, n, n, square, quo, i, rem, n);
            return 1;
        }
        if (quo < 10)
            break;
    }
    return 0;
}

int main() {
    unsigned long long i, start = 0, stop = 4294967295, count = 0;
    for (i = start; i <= stop; i++) {
        count += test_kaprekar(i);
    }
    printf("%llu Kaprekar numbers between %llu and %llu\n", count, start, stop);
    return 0;
}

输出:

1 is a Kaprekar number: 1*1 = 1, 0+1 = 1
9 is a Kaprekar number: 9*9 = 81, 8+1 = 9
45 is a Kaprekar number: 45*45 = 2025, 20+25 = 45
55 is a Kaprekar number: 55*55 = 3025, 30+25 = 55
99 is a Kaprekar number: 99*99 = 9801, 98+01 = 99
297 is a Kaprekar number: 297*297 = 88209, 88+209 = 297
703 is a Kaprekar number: 703*703 = 494209, 494+209 = 703
999 is a Kaprekar number: 999*999 = 998001, 998+001 = 999
2223 is a Kaprekar number: 2223*2223 = 4941729, 494+1729 = 2223
2728 is a Kaprekar number: 2728*2728 = 7441984, 744+1984 = 2728
4879 is a Kaprekar number: 4879*4879 = 23804641, 238+04641 = 4879
4950 is a Kaprekar number: 4950*4950 = 24502500, 2450+2500 = 4950
5050 is a Kaprekar number: 5050*5050 = 25502500, 2550+2500 = 5050
5292 is a Kaprekar number: 5292*5292 = 28005264, 28+005264 = 5292
7272 is a Kaprekar number: 7272*7272 = 52881984, 5288+1984 = 7272
7777 is a Kaprekar number: 7777*7777 = 60481729, 6048+1729 = 7777
9999 is a Kaprekar number: 9999*9999 = 99980001, 9998+0001 = 9999
17344 is a Kaprekar number: 17344*17344 = 300814336, 3008+14336 = 17344
22222 is a Kaprekar number: 22222*22222 = 493817284, 4938+17284 = 22222
38962 is a Kaprekar number: 38962*38962 = 1518037444, 1518+037444 = 38962
77778 is a Kaprekar number: 77778*77778 = 6049417284, 60494+17284 = 77778
82656 is a Kaprekar number: 82656*82656 = 6832014336, 68320+14336 = 82656
95121 is a Kaprekar number: 95121*95121 = 9048004641, 90480+04641 = 95121
99999 is a Kaprekar number: 99999*99999 = 9999800001, 99998+00001 = 99999
...
86358636 is a Kaprekar number: 86358636*86358636 = 7457814011780496, 74578140+11780496 = 86358636
88888888 is a Kaprekar number: 88888888*88888888 = 7901234409876544, 79012344+09876544 = 88888888
91838088 is a Kaprekar number: 91838088*91838088 = 8434234407495744, 84342344+07495744 = 91838088
94520547 is a Kaprekar number: 94520547*94520547 = 8934133805179209, 89341338+05179209 = 94520547
99999999 is a Kaprekar number: 99999999*99999999 = 9999999800000001, 99999998+00000001 = 99999999
234567901 is a Kaprekar number: 234567901*234567901 = 55022100179545801, 55022100+179545801 = 234567901
243902440 is a Kaprekar number: 243902440*243902440 = 59488400237953600, 5948840+0237953600 = 243902440
332999667 is a Kaprekar number: 332999667*332999667 = 110888778222110889, 110888778+222110889 = 332999667
432432432 is a Kaprekar number: 432432432*432432432 = 186997808245434624, 186997808+245434624 = 432432432
567567568 is a Kaprekar number: 567567568*567567568 = 322132944245434624, 322132944+245434624 = 567567568
665188470 is a Kaprekar number: 665188470*665188470 = 442475700620940900, 44247570+0620940900 = 665188470
667000333 is a Kaprekar number: 667000333*667000333 = 444889444222110889, 444889444+222110889 = 667000333
765432099 is a Kaprekar number: 765432099*765432099 = 585886298179545801, 585886298+179545801 = 765432099
867208672 is a Kaprekar number: 867208672*867208672 = 752050880792003584, 75205088+0792003584 = 867208672
909090909 is a Kaprekar number: 909090909*909090909 = 826446280826446281, 82644628+0826446281 = 909090909
999999999 is a Kaprekar number: 999999999*999999999 = 999999998000000001, 999999998+000000001 = 999999999
1111111111 is a Kaprekar number: 1111111111*1111111111 = 1234567900987654321, 123456790+0987654321 = 1111111111
1776299581 is a Kaprekar number: 1776299581*1776299581 = 3155240201460775561, 315524020+1460775561 = 1776299581
2020202020 is a Kaprekar number: 2020202020*2020202020 = 4081216201612080400, 408121620+1612080400 = 2020202020
2646002646 is a Kaprekar number: 2646002646*2646002646 = 7001330002639001316, 7001330+002639001316 = 2646002646
3846956652 is a Kaprekar number: 3846956652*3846956652 = 14799075482367049104, 1479907548+2367049104 = 3846956652
3888938889 is a Kaprekar number: 3888938889*3888938889 = 15123845682376554321, 1512384568+2376554321 = 3888938889
4090859091 is a Kaprekar number: 4090859091*4090859091 = 16735128102417346281, 1673512810+2417346281 = 4090859091
4132841328 is a Kaprekar number: 4132841328*4132841328 = 17080377442424803584, 1708037744+2424803584 = 4132841328
110 Kaprekar numbers between 0 and 4294967295

所有10的幂减1都是以10为底的Kaprekar数,其他的Kaprekar数在log 10 (n)在10 n 和10 之间非常罕见n + 1