【发布时间】:2015-02-25 04:31:53
【问题描述】:
我的 C 编程课程介绍有这个作业,我的部分代码必须找到一个数字的平方和的序列,以便确定给定的数字是否是一个快乐的数字(平方和数字 = 1)
这是我的部分代码:
#include <stdio.h>
#include <math.h>
//平方和函数
int sqd (int x) {
int sum = 0;
while (x > 0) {
sum = sum + pow(x%10, 2);
x = x/10;
}
return sum;
}
// 搜索功能
int search (int a[], int val, int size) {
int i;
for (i = 0; i < size; i++) {
if (a[i] == val) {
return 1;
}
}
return 0;
}
// 主程序
void main () {
int a [1000] = {0};
int N;
int count = 1;
int j;
printf("Please enter the potential happy number:\n", N);
scanf ("%d", &N);
a[0] = N;
a[count] = sqd (N);
do {
a[count] = sqd (a[count-1]);
count++;
} while (search (a, a[count], count));
for ( j = 0; j <= count; j++) {
printf("%d\n", a[j]);
}
}
它只打印序列中的前三个总和。我真的不知道如何使它工作。
提前谢谢你
【问题讨论】:
-
对不起,while 前面多了一个“}”
-
您可以随时编辑您的问题。