C - 找出一个数字的a ^ 2 + b ^ 2

时间:2014-08-12 08:40:20

标签: c numbers

我手头有一个问题,我无法弄明白,因为我对C编程很陌生。问题是找出100和999之间的所有整数,可以用(a ^ 2 + b ^ 2)的形式表示。

以下是我尝试的内容:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int n,i,j,soln;

  for (n=100;n<1000;++n) {
    soln=0;

    for (i=0;i<100;++i) {
      for (j=i;j<100;++j) {
        if ((i^2 + j^2)==n) {
          soln=1;
        } else {
          soln=0;
        }
      }
    }

    if (soln==1) printf("%d is a solution.\n",n);
  }

  return EXIT_SUCCESS;
}

谢谢你们!

4 个答案:

答案 0 :(得分:2)

您的代码有两个问题:

<强> 1。使用*对数字求平方,而不是^

要在C中对数字进行平方,请不要使用^运算符(这是一个按位XOR运算符)。请改用*运算符:

if ((i*i + j*j)==n) {
   soln=1;
} else {
   soln=0;
}

<强> 2。将if条件移动到内循环

您的代码的另一个问题是您覆盖soln值。您应该将条件移动到内循环中:

for (n=100;n<1000;++n) {
  soln=0;

  for (i=0;i<100;++i) {
    for (j=i;j<100;++j) {
      if ((i*i + j*j)==n) {
        soln=1;
      } else {
        soln=0;
      }
      // Condition here. When it was in the outer loop level, 
      // the soln=1 would be overwritten to the 0 in the next iteration
      // and printf() wouldn't be called.
      if (soln==1) {
        printf("%d is a solution.\n",n);
        // To avoid printing multiple times for the same n,
        // break from the loop (loop for 'j').
        // If you would want to print for every i,j pair that meets
        // the criteria, remove this 'if' block, get rid of 'soln'
        // and print in the if above (where you square the numbers).
        break;
      }
    }
    // We need to break the two loops, for i and j. This one is for 
    // the outer loop ('i').
    if (soln == 1) break;
  }
}

正如您所看到的,您可以通过两种略有不同的变体来实现它:

  • 您只关心n - 如果您不需要知道解决方案号码的组成部分,您可以加快速度通过不打印每个可能的解决方案,只需满足它的n,算法有点。在这种情况下,一旦找到ij,当得到平方时,给你n,只需打破两个循环。你可以这样做,因为对于那个n,我们知道存在满足条件的i / j对。

  • 您关心n以及解决方程的确切i / j - 在这种情况下,请移除{{1} }变量,只需打印解决方案,而不是将soln设置为1。

答案 1 :(得分:1)

运营商^在C,C ++,C#中意味着 XOR eXclusive OR ),而不是进入权力。 使用乘法*代替:

 if ((i * i + j * j) == n) {
   ... 

答案 2 :(得分:1)

如果您找到解决方案,我认为您需要退出循环:

for (i=0;i<100;++i) {
  for (j=i;j<100;++j) {
    if ((i*i + j*j)==n) {
      soln=1;
      break;
    } else {
      soln=0;
    }
  }

  if (soln=1) {
    break;
  }
}

soln = 1是您找到解决方案的标志。但是在下一次迭代中,您将覆盖该值并再次设置soln = 0.

答案 3 :(得分:1)

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int n,i,j,soln;

  for (n=100;n<1000;++n) {
    soln=0;

    for (i=0;i<100;++i) {
    for (j=i;j<100;++j) {
        if ((i*i + j*j)==n) {
                printf("%d is a solution for i: %d and j: %d \n",n,i,j);
          soln=1;
        } else {
          soln=0;
        }
      }
    }
//    if (soln==1) printf("%d is a solution.\n",n);
  }

  return EXIT_SUCCESS;
}

你的printf()位置错误。我修改了你的代码。 输出

100 is a solution for i: 0 and j: 10 
100 is a solution for i: 6 and j: 8 
101 is a solution for i: 1 and j: 10 
104 is a solution for i: 2 and j: 10 
106 is a solution for i: 5 and j: 9 
109 is a solution for i: 3 and j: 10 
113 is a solution for i: 7 and j: 8 
116 is a solution for i: 4 and j: 10 
117 is a solution for i: 6 and j: 9 
121 is a solution for i: 0 and j: 11 
122 is a solution for i: 1 and j: 11 
125 is a solution for i: 2 and j: 11 
125 is a solution for i: 5 and j: 10 
128 is a solution for i: 8 and j: 8 
130 is a solution for i: 3 and j: 11 
130 is a solution for i: 7 and j: 9 
136 is a solution for i: 6 and j: 10 
137 is a solution for i: 4 and j: 11 
144 is a solution for i: 0 and j: 12 
145 is a solution for i: 1 and j: 12 
145 is a solution for i: 8 and j: 9 
146 is a solution for i: 5 and j: 11 
148 is a solution for i: 2 and j: 12 
149 is a solution for i: 7 and j: 10 
153 is a solution for i: 3 and j: 12 
157 is a solution for i: 6 and j: 11 
160 is a solution for i: 4 and j: 12 
162 is a solution for i: 9 and j: 9 
164 is a solution for i: 8 and j: 10 
169 is a solution for i: 0 and j: 13 
169 is a solution for i: 5 and j: 12 
170 is a solution for i: 1 and j: 13 
170 is a solution for i: 7 and j: 11 
173 is a solution for i: 2 and j: 13 
178 is a solution for i: 3 and j: 13 
180 is a solution for i: 6 and j: 12 
181 is a solution for i: 9 and j: 10 
185 is a solution for i: 4 and j: 13 
185 is a solution for i: 8 and j: 11 
193 is a solution for i: 7 and j: 12 
194 is a solution for i: 5 and j: 13 
196 is a solution for i: 0 and j: 14 
197 is a solution for i: 1 and j: 14 
200 is a solution for i: 2 and j: 14 
200 is a solution for i: 10 and j: 10 
202 is a solution for i: 9 and j: 11 
205 is a solution for i: 3 and j: 14 
205 is a solution for i: 6 and j: 13 
208 is a solution for i: 8 and j: 12 
212 is a solution for i: 4 and j: 14 
218 is a solution for i: 7 and j: 13 
221 is a solution for i: 5 and j: 14 
221 is a solution for i: 10 and j: 11 
225 is a solution for i: 0 and j: 15 
225 is a solution for i: 9 and j: 12 
226 is a solution for i: 1 and j: 15 
229 is a solution for i: 2 and j: 15 
232 is a solution for i: 6 and j: 14 
233 is a solution for i: 8 and j: 13 
234 is a solution for i: 3 and j: 15 
241 is a solution for i: 4 and j: 15 
242 is a solution for i: 11 and j: 11 
244 is a solution for i: 10 and j: 12 
245 is a solution for i: 7 and j: 14 
250 is a solution for i: 5 and j: 15 
250 is a solution for i: 9 and j: 13 
256 is a solution for i: 0 and j: 16 
257 is a solution for i: 1 and j: 16 
260 is a solution for i: 2 and j: 16 
260 is a solution for i: 8 and j: 14 
261 is a solution for i: 6 and j: 15 
265 is a solution for i: 3 and j: 16 
265 is a solution for i: 11 and j: 12 
269 is a solution for i: 10 and j: 13 
272 is a solution for i: 4 and j: 16 
274 is a solution for i: 7 and j: 15 
277 is a solution for i: 9 and j: 14 
281 is a solution for i: 5 and j: 16 
288 is a solution for i: 12 and j: 12 
289 is a solution for i: 0 and j: 17 
289 is a solution for i: 8 and j: 15 
290 is a solution for i: 1 and j: 17 
290 is a solution for i: 11 and j: 13 
292 is a solution for i: 6 and j: 16 
293 is a solution for i: 2 and j: 17 
296 is a solution for i: 10 and j: 14 
298 is a solution for i: 3 and j: 17 
305 is a solution for i: 4 and j: 17 
305 is a solution for i: 7 and j: 16 
306 is a solution for i: 9 and j: 15 
313 is a solution for i: 12 and j: 13 
314 is a solution for i: 5 and j: 17 
317 is a solution for i: 11 and j: 14 
320 is a solution for i: 8 and j: 16 
324 is a solution for i: 0 and j: 18 
325 is a solution for i: 1 and j: 18 
325 is a solution for i: 6 and j: 17 
325 is a solution for i: 10 and j: 15 
328 is a solution for i: 2 and j: 18 
333 is a solution for i: 3 and j: 18 
337 is a solution for i: 9 and j: 16 
338 is a solution for i: 7 and j: 17 
338 is a solution for i: 13 and j: 13 
340 is a solution for i: 4 and j: 18 
340 is a solution for i: 12 and j: 14 
346 is a solution for i: 11 and j: 15 
349 is a solution for i: 5 and j: 18 
353 is a solution for i: 8 and j: 17 
356 is a solution for i: 10 and j: 16 
360 is a solution for i: 6 and j: 18 
361 is a solution for i: 0 and j: 19 
362 is a solution for i: 1 and j: 19 
365 is a solution for i: 2 and j: 19 
365 is a solution for i: 13 and j: 14 
369 is a solution for i: 12 and j: 15 
370 is a solution for i: 3 and j: 19 
370 is a solution for i: 9 and j: 17 
373 is a solution for i: 7 and j: 18 
377 is a solution for i: 4 and j: 19 
377 is a solution for i: 11 and j: 16 
386 is a solution for i: 5 and j: 19 
388 is a solution for i: 8 and j: 18 
389 is a solution for i: 10 and j: 17 
392 is a solution for i: 14 and j: 14 
394 is a solution for i: 13 and j: 15 
397 is a solution for i: 6 and j: 19 
400 is a solution for i: 0 and j: 20 
400 is a solution for i: 12 and j: 16 
401 is a solution for i: 1 and j: 20 
404 is a solution for i: 2 and j: 20 
405 is a solution for i: 9 and j: 18 
409 is a solution for i: 3 and j: 20 
410 is a solution for i: 7 and j: 19 
410 is a solution for i: 11 and j: 17 
416 is a solution for i: 4 and j: 20 
421 is a solution for i: 14 and j: 15 
424 is a solution for i: 10 and j: 18 
425 is a solution for i: 5 and j: 20 
425 is a solution for i: 8 and j: 19 
425 is a solution for i: 13 and j: 16 
433 is a solution for i: 12 and j: 17 
436 is a solution for i: 6 and j: 20 
441 is a solution for i: 0 and j: 21 
442 is a solution for i: 1 and j: 21 
442 is a solution for i: 9 and j: 19 
445 is a solution for i: 2 and j: 21 
445 is a solution for i: 11 and j: 18 
449 is a solution for i: 7 and j: 20 
450 is a solution for i: 3 and j: 21 
450 is a solution for i: 15 and j: 15 
452 is a solution for i: 14 and j: 16 
457 is a solution for i: 4 and j: 21 
458 is a solution for i: 13 and j: 17 
461 is a solution for i: 10 and j: 19 
464 is a solution for i: 8 and j: 20 
466 is a solution for i: 5 and j: 21 
468 is a solution for i: 12 and j: 18 
477 is a solution for i: 6 and j: 21 
481 is a solution for i: 9 and j: 20 
481 is a solution for i: 15 and j: 16 
482 is a solution for i: 11 and j: 19 
484 is a solution for i: 0 and j: 22 
485 is a solution for i: 1 and j: 22 
485 is a solution for i: 14 and j: 17 
488 is a solution for i: 2 and j: 22 
490 is a solution for i: 7 and j: 21 
493 is a solution for i: 3 and j: 22 
493 is a solution for i: 13 and j: 18 
500 is a solution for i: 4 and j: 22 
500 is a solution for i: 10 and j: 20 
505 is a solution for i: 8 and j: 21 
505 is a solution for i: 12 and j: 19 
509 is a solution for i: 5 and j: 22 
512 is a solution for i: 16 and j: 16 
514 is a solution for i: 15 and j: 17 
520 is a solution for i: 6 and j: 22 
520 is a solution for i: 14 and j: 18 
521 is a solution for i: 11 and j: 20 
522 is a solution for i: 9 and j: 21 
529 is a solution for i: 0 and j: 23 
530 is a solution for i: 1 and j: 23 
530 is a solution for i: 13 and j: 19 
533 is a solution for i: 2 and j: 23 
533 is a solution for i: 7 and j: 22 
538 is a solution for i: 3 and j: 23 
541 is a solution for i: 10 and j: 21 
544 is a solution for i: 12 and j: 20 
545 is a solution for i: 4 and j: 23 
545 is a solution for i: 16 and j: 17 
548 is a solution for i: 8 and j: 22 
549 is a solution for i: 15 and j: 18 
554 is a solution for i: 5 and j: 23 
557 is a solution for i: 14 and j: 19 
562 is a solution for i: 11 and j: 21 
565 is a solution for i: 6 and j: 23 
565 is a solution for i: 9 and j: 22 
569 is a solution for i: 13 and j: 20 
576 is a solution for i: 0 and j: 24 
577 is a solution for i: 1 and j: 24 
578 is a solution for i: 7 and j: 23 
578 is a solution for i: 17 and j: 17 
580 is a solution for i: 2 and j: 24 
580 is a solution for i: 16 and j: 18 
584 is a solution for i: 10 and j: 22 
585 is a solution for i: 3 and j: 24 
585 is a solution for i: 12 and j: 21 
586 is a solution for i: 15 and j: 19 
592 is a solution for i: 4 and j: 24 
593 is a solution for i: 8 and j: 23 
596 is a solution for i: 14 and j: 20 
601 is a solution for i: 5 and j: 24 
605 is a solution for i: 11 and j: 22 
610 is a solution for i: 9 and j: 23 
610 is a solution for i: 13 and j: 21 
612 is a solution for i: 6 and j: 24 
613 is a solution for i: 17 and j: 18 
617 is a solution for i: 16 and j: 19 
625 is a solution for i: 0 and j: 25 
625 is a solution for i: 7 and j: 24 
625 is a solution for i: 15 and j: 20 
626 is a solution for i: 1 and j: 25 
628 is a solution for i: 12 and j: 22 
629 is a solution for i: 2 and j: 25 
629 is a solution for i: 10 and j: 23 
634 is a solution for i: 3 and j: 25 
637 is a solution for i: 14 and j: 21 
640 is a solution for i: 8 and j: 24 
641 is a solution for i: 4 and j: 25 
648 is a solution for i: 18 and j: 18 
650 is a solution for i: 5 and j: 25 
650 is a solution for i: 11 and j: 23 
650 is a solution for i: 17 and j: 19 
653 is a solution for i: 13 and j: 22 
656 is a solution for i: 16 and j: 20 
657 is a solution for i: 9 and j: 24 
661 is a solution for i: 6 and j: 25 
666 is a solution for i: 15 and j: 21 
673 is a solution for i: 12 and j: 23 
674 is a solution for i: 7 and j: 25 
676 is a solution for i: 0 and j: 26 
676 is a solution for i: 10 and j: 24 
677 is a solution for i: 1 and j: 26 
680 is a solution for i: 2 and j: 26 
680 is a solution for i: 14 and j: 22 
685 is a solution for i: 3 and j: 26 
685 is a solution for i: 18 and j: 19 
689 is a solution for i: 8 and j: 25 
689 is a solution for i: 17 and j: 20 
692 is a solution for i: 4 and j: 26 
697 is a solution for i: 11 and j: 24 
697 is a solution for i: 16 and j: 21 
698 is a solution for i: 13 and j: 23 
701 is a solution for i: 5 and j: 26 
706 is a solution for i: 9 and j: 25 
709 is a solution for i: 15 and j: 22 
712 is a solution for i: 6 and j: 26 
720 is a solution for i: 12 and j: 24 
722 is a solution for i: 19 and j: 19 
724 is a solution for i: 18 and j: 20 
725 is a solution for i: 7 and j: 26 
725 is a solution for i: 10 and j: 25 
725 is a solution for i: 14 and j: 23 
729 is a solution for i: 0 and j: 27 
730 is a solution for i: 1 and j: 27 
730 is a solution for i: 17 and j: 21 
733 is a solution for i: 2 and j: 27 
738 is a solution for i: 3 and j: 27 
740 is a solution for i: 8 and j: 26 
740 is a solution for i: 16 and j: 22 
745 is a solution for i: 4 and j: 27 
745 is a solution for i: 13 and j: 24 
746 is a solution for i: 11 and j: 25 
754 is a solution for i: 5 and j: 27 
754 is a solution for i: 15 and j: 23 
757 is a solution for i: 9 and j: 26 
761 is a solution for i: 19 and j: 20 
765 is a solution for i: 6 and j: 27 
765 is a solution for i: 18 and j: 21 
769 is a solution for i: 12 and j: 25 
772 is a solution for i: 14 and j: 24 
773 is a solution for i: 17 and j: 22 
776 is a solution for i: 10 and j: 26 
778 is a solution for i: 7 and j: 27 
784 is a solution for i: 0 and j: 28 
785 is a solution for i: 1 and j: 28 
785 is a solution for i: 16 and j: 23 
788 is a solution for i: 2 and j: 28 
793 is a solution for i: 3 and j: 28 
793 is a solution for i: 8 and j: 27 
794 is a solution for i: 13 and j: 25 
797 is a solution for i: 11 and j: 26 
800 is a solution for i: 4 and j: 28 
800 is a solution for i: 20 and j: 20 
801 is a solution for i: 15 and j: 24 
802 is a solution for i: 19 and j: 21 
808 is a solution for i: 18 and j: 22 
809 is a solution for i: 5 and j: 28 
810 is a solution for i: 9 and j: 27 
818 is a solution for i: 17 and j: 23 
820 is a solution for i: 6 and j: 28 
820 is a solution for i: 12 and j: 26 
821 is a solution for i: 14 and j: 25 
829 is a solution for i: 10 and j: 27 
832 is a solution for i: 16 and j: 24 
833 is a solution for i: 7 and j: 28 
841 is a solution for i: 0 and j: 29 
841 is a solution for i: 20 and j: 21 
842 is a solution for i: 1 and j: 29 
845 is a solution for i: 2 and j: 29 
845 is a solution for i: 13 and j: 26 
845 is a solution for i: 19 and j: 22 
848 is a solution for i: 8 and j: 28 
850 is a solution for i: 3 and j: 29 
850 is a solution for i: 11 and j: 27 
850 is a solution for i: 15 and j: 25 
853 is a solution for i: 18 and j: 23 
857 is a solution for i: 4 and j: 29 
865 is a solution for i: 9 and j: 28 
865 is a solution for i: 17 and j: 24 
866 is a solution for i: 5 and j: 29 
872 is a solution for i: 14 and j: 26 
873 is a solution for i: 12 and j: 27 
877 is a solution for i: 6 and j: 29 
881 is a solution for i: 16 and j: 25 
882 is a solution for i: 21 and j: 21 
884 is a solution for i: 10 and j: 28 
884 is a solution for i: 20 and j: 22 
890 is a solution for i: 7 and j: 29 
890 is a solution for i: 19 and j: 23 
898 is a solution for i: 13 and j: 27 
900 is a solution for i: 0 and j: 30 
900 is a solution for i: 18 and j: 24 
901 is a solution for i: 1 and j: 30 
901 is a solution for i: 15 and j: 26 
904 is a solution for i: 2 and j: 30 
905 is a solution for i: 8 and j: 29 
905 is a solution for i: 11 and j: 28 
909 is a solution for i: 3 and j: 30 
914 is a solution for i: 17 and j: 25 
916 is a solution for i: 4 and j: 30 
922 is a solution for i: 9 and j: 29 
925 is a solution for i: 5 and j: 30 
925 is a solution for i: 14 and j: 27 
925 is a solution for i: 21 and j: 22 
928 is a solution for i: 12 and j: 28 
929 is a solution for i: 20 and j: 23 
932 is a solution for i: 16 and j: 26 
936 is a solution for i: 6 and j: 30 
937 is a solution for i: 19 and j: 24 
941 is a solution for i: 10 and j: 29 
949 is a solution for i: 7 and j: 30 
949 is a solution for i: 18 and j: 25 
953 is a solution for i: 13 and j: 28 
954 is a solution for i: 15 and j: 27 
961 is a solution for i: 0 and j: 31 
962 is a solution for i: 1 and j: 31 
962 is a solution for i: 11 and j: 29 
964 is a solution for i: 8 and j: 30 
965 is a solution for i: 2 and j: 31 
965 is a solution for i: 17 and j: 26 
968 is a solution for i: 22 and j: 22 
970 is a solution for i: 3 and j: 31 
970 is a solution for i: 21 and j: 23 
976 is a solution for i: 20 and j: 24 
977 is a solution for i: 4 and j: 31 
980 is a solution for i: 14 and j: 28 
981 is a solution for i: 9 and j: 30 
985 is a solution for i: 12 and j: 29 
985 is a solution for i: 16 and j: 27 
986 is a solution for i: 5 and j: 31 
986 is a solution for i: 19 and j: 25 
997 is a solution for i: 6 and j: 31