2017-08-29 57 views
0
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
int main() 
{ 
char a[100]; 
long int n,i,j,k,l,sum,p,q; 
while(scanf("%ld",&l)) 
{ 

    for(i=0; i<pow(10,l); i++) 
    { 
     if(l==2) 
      sum=((i/10)+(i%10))*((i/10)+(i%10)); 
     else if(l==4) 
      sum=((i/100)+(i%100))*((i/100)+(i%100)); 
     else if(l==8) 
      sum=((i/10000)+(i%10000))*((i/10000)+(i%10000)); 
     if(sum==i) 
     { 
      itoa(i,a,100); 
      j=strlen(a); 
      for(k=0; k<l-j; k++) 
       printf("0"); 
      for(k=0; k<j; k++) 
       a[k]=a[k]+48; 
      puts(a); 
     } 
    } 
} 
return 0; 
} 

我不明白這個輸出是什麼。我試圖找出quirksome數字。我是一個初學者,請幫助。 關於quirksome號: 數3025具有顯着的怪癖:如果在等於 長度(30和25)的兩個字符串分割其十進制表示和方如此獲得的數目的總和,則獲得原始數:c中字符串中的隨機符號

(30 + 25) = 3025

+5

這部分:'用於(K = 0; k <Ĵ; k ++) a [k] = a [k] +48;'似乎沒什麼意義。如果'a'已經是一個十進制整數,爲什麼你會覆蓋這些字符?很混亂。 – unwind

+0

無關,但你忘了'#include '。不要使用「48」等魔法數字,而是使用「0」來代替;這是相當的,但清楚地表明你的意圖。 –

+0

...以及您期望的輸出是什麼? –

回答

0

也許你已經從別的地方的代碼,你想了解它做什麼。

它要求l,這將是數字的長度。 l必須是2,4或8(代碼不檢查此)。

對於從1到10的每個值il的冪,它參與其兩部分的數字,然後執行「quirksome」計算到sum

如果總數等於當前的i,則它是一個奇怪的數字並打印出來。印刷有點奇怪,並忘記字符串a。 (puts(a);之前必須有a[k]=0;)。整個打印本來可以用printf("%0*d\n",l,i);

0

看看這個代碼 - brutforce finder。很容易讓它變得更快,但我會爲你保留它。我已經使用微軟的特定功能,但很容易地改變它的C庫實現那些

int isQuirksome(unsigned long long number) 
{ 
    int result = 0, len; 
    char strnumber[50], first[50], second[50]; 


    if (number < 10ULL) result = -1; 
    if (result != -1) 
    { 
     _ui64toa(number, strnumber, 10); // microsoft specific use the one from your library 
     len = strlen(strnumber); 
     result = 0; 

     for (int i = 1; i < len; i++) 
     { 
      unsigned long long FirstValue, SecondValue, sum; 

      strncpy(first, strnumber, i); 
      strncpy(second, strnumber + i, len - i); 
      first[i] = 0; 
      second[len - i] = 0; 

      FirstValue = _strtoui64(first, NULL, 10); 
      SecondValue = _strtoui64(second, NULL, 10); 
      sum = FirstValue + SecondValue; 
      if ((sum * sum) == number) 
      { 
       result = i; 
       break; 
      } 
     } 
    } 
    return result; 
} 

int main(int argc, char **argv) 
{ 

    char result[50]; 

    for (unsigned long long i = 10; i < ULLONG_MAX; i++) 
    { 
     int pos; 
     if ((pos = isQuirksome(i)) > 0) 
     { 
      printf("Found !!!! %llu is the QuikSome Number position of the split is %d\n", i, pos); 
     } 
    } 
} 

和一些結果:

Found !!!! 81   is the QuikSome Number position of the split is 1 
Found !!!! 100   is the QuikSome Number position of the split is 2 
Found !!!! 2025     is the QuikSome Number position of the split is 2 
Found !!!! 3025     is the QuikSome Number position of the split is 2 
Found !!!! 9801     is the QuikSome Number position of the split is 2 
Found !!!! 10000    is the QuikSome Number position of the split is 3 
Found !!!! 88209    is the QuikSome Number position of the split is 2 
Found !!!! 494209    is the QuikSome Number position of the split is 3 
Found !!!! 998001    is the QuikSome Number position of the split is 3 
Found !!!! 1000000    is the QuikSome Number position of the split is 4 
Found !!!! 4941729    is the QuikSome Number position of the split is 3 
Found !!!! 7441984    is the QuikSome Number position of the split is 3 
Found !!!! 23804641    is the QuikSome Number position of the split is 3 
Found !!!! 24502500    is the QuikSome Number position of the split is 4 
Found !!!! 25502500    is the QuikSome Number position of the split is 4 
Found !!!! 28005264    is the QuikSome Number position of the split is 2 
Found !!!! 52881984    is the QuikSome Number position of the split is 4 
Found !!!! 60481729    is the QuikSome Number position of the split is 4 
Found !!!! 99980001    is the QuikSome Number position of the split is 4 
Found !!!! 100000000   is the QuikSome Number position of the split is 5