解题思路:
使用两层循环,外循环寻找完数,内循环寻找因子
细节上为了避免输出因子时仍要循环寻找因子,引入数组来储存因子,并用指针的移动来控制数组的读写
但是非完数也会有储存因子的过程,所以代码写得少了,但运行速度并不算快
主要是不希望做重复的过程和定义太多变量,容易搞乱
参考代码:
#include <stdio.h> int main() { int n; scanf("%d", &n); //输入n for (int i = 6; i <= n; i++) //for循环从第一个完数6开始 { // if (i % 10 != 6 && i % 10 != 8) continue; //减少运行时间可加入此行,跳过个位非6与8的数 int s[100] = {0}; //初始化数组s全为0,循环内的临时数组每次循环都会初始化 int *p = s; //通过指针p的移动来控制数组的读写 for (int j = 1; j <= i / 2; j++) { if (i % j == 0) { *++p = j; //此处p先后移,再赋值储存因子j s[0] += j; //s[0]储存因子的和 } } if (s[0] == i) //s[0]==i 则 i 为完数 { printf("%d its factors are", i); for (p = &s[1]; *p != 0; p++) //for循环p指向s[1],值为0时停止,每次循环后移一次 { printf(" %d", *p); } putchar('\n'); } } return 0; }
0.0分
34 人评分
int main() { int N; scanf("%d", &N); for (int i = 6; i <= N; i++) { int sum = 0; //此步至关重要,每次内层循环结束后将sum清零 for (int j = 1; j <= i-1; j++) { if (i % j == 0) sum += j; } if (sum == i) { printf("%d its factors are ", i); for (int j = 1; j <= i-1; j++) { if (i % j == 0) printf("%d ", j); } printf("\n"); } } return 0; }
#include <stdio.h> int main() { int n,m=0; scanf("%d", &n); for (int i = 6; i <= n; i++) { int s[100] = { 0 }; for (int j = 1; j <= i / 2; j++) { if (i % j == 0) { s[++m] = j; s[0] += j; } } if (s[0] == i) { printf("%d its factors are", i); for (m=1;s[m] != 0; m++) { printf(" %d", s[m]); } putchar('\n'); }
起岸希 2022-09-18 20:23:44 |
看我这个,其实不用指针的
#include<stdio.h> #include<math.h> int main() { int n; scanf("%d", &n); for (int i = 6; i < n; i++) { int sum = 0, count = 0,a[1001],t=1; for (int j = 1; j < i; j++) { if (i % j == 0) { a[t] = j; sum += j; t++; count++;//数组个数 } if (sum == i&&i==j+1) { printf("%d its factors are ", i); for (int z = 1; z <=count ; z++) { printf("%d ", a[z]); } printf("\n"); break; } } } }
#include<stdio.h> int main() { int N; scanf("%d",&N); int m; for(m=6;m<=N;m++) { if (m % 10 != 6 && m% 10 != 8) continue; int n[100]={0}; int *h=n,p; for(p=1;p<=m/2;p++) { if(m%p==0) n[0]=p+n[0]; *++h=p; } if(n[0]==m) {printf("%d its factors are ",m); for(h=&n[1];*h!=0;h++) { printf("%d ",*h); } putchar('\n'); } } return 0; } 运行错误,咋回事啊
#include<stdio.h> int main() { int n,a,i,r; scanf("%d",&n); for(a=1;a<=n;a++) { int sum=0,b[1000]={0},j=0; for(i=1;i<a;i++) { r=a%i; if(r==0) { b[j]=i; j++; } } for(i=0;i<j;i++) { sum+=b[i]; } if(a==sum) { printf("%d ",a); printf("its factors are "); for(i=0;i<j;i++) { printf("%d ",b[i]); } printf("\n"); } } return 0; }
int main() { int n,i, j,a = 0; scanf("%d", &n); for ( i = 1; i < n; i++) { for ( j = 1; j < i; j++) { if (i%j == 0)a = a + j; } if (a==i) { printf("%d its factors are ", i); for ( j = 1; j < i; j++) { if (i%j == 0)printf("%d ", j); } printf("\n"); } a = 0; } return 0; }
Carbon14 2021-08-10 02:21:15 |
我写的与你类似,但是我在<处用的是<=,为什么就不成功了……
#include<stdio.h> int main() { int a,b,d,c,e; int number[50]; int cnt=0; scanf("%d",&a); for(b=1;b<a;b++){ c=a%b; while(c==0){ number[cnt]=b; c++; } cnt++; } e=cnt; for(cnt;cnt>=0;cnt--){ d+=number[cnt]; } while(a==d){ printf("%d its factors are ",a); for(cnt;cnt<=e;cnt++){ printf(" %d",number[cnt]); a++; } return 0; } }
bobo 2021-03-15 23:56:26 |
为什么运行没有结果,求解
CodeRookie 2021-03-16 12:54:54 |
你这错的有点多啊...... 我帮你改改你看一下吧
CodeRookie 2021-03-16 12:55:32 |
#include<stdio.h> int main() { int a, b, d, c, e; int cnt = 0; scanf("%d", &e); for (a = 6; a <= e; a++) { int number[100] = {0}; cnt = 0; for (b = 1; b < a; b++) { c = a % b; while (c == 0) { number[cnt] = b; c++; cnt++; } } d = 0; for (int i = 0; i < cnt; i++) { d += number[i]; } if (a == d) { printf("%d its factors are ", a); for (int i = 0; i < cnt; i++) { printf("%d ", number[i]); a++; } printf(" "); } } return 0; }
CodeRookie 2021-03-16 12:58:21 |
发现评论不能换行,我私信发你吧
破解简单密码 (C语言代码)浏览:1864 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:607 |
多输入输出练习1 (C语言代码)浏览:1219 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:1071 |
【偶数求和】 (C语言代码)浏览:588 |
wu-理财计划 (C++代码)浏览:907 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:751 |
Quadratic Equation (C语言代码)浏览:1034 |
A+B for Input-Output Practice (I) (C语言代码)浏览:451 |
小O的数字 (C语言代码)浏览:1490 |