Đề bài: Viết chương trình C cho phép nhập vào n và thực hiện liệt kê các số arstrong nhỏ hơn n. (Mỗi kết quả thỏa mãn cách nhau một khoảng trắng)
INPUT
1000
OUTPUT
1 2 3 4 5 6 7 8 9 153 370 371 407
e chạy thử thấy kết quả ra đúng mà nộp lên tool của thầy giáo toàn chấm sai.
#include <stdio.h>
#include <math.h>
#define MAX 10000
int kTraArmstrong(int n){
int k=0,a[MAX]={},m;
m=n;
while (m!=0){
a[k]=m%10;
k++;
m/=10;
}
for (int i=0; i<=k; i++){
m=m+pow(a[i],k);
}
if (n==m)
return 1;
else return 0;
}
void lietKeArmstrong(int x){
int a[MAX];
x++;
for (int j=1;j<x; j++){
if (kTraArmstrong(j))
a[j]=1;
else a[j]=0;
}
for (int j=1; j<x;j++){
if (a[j])
printf ("%d ", j);
}
}
int main(){
int x;
scanf("%d", &x);
lietKeArmstrong(x);
return 0;
}