C语言:程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat(0<repeat<10),做repeat次下列运算:输入两个正整数m和n(1<=m,n<=1000),输出m到n之间的所有完数,并输出其因子。...
输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入两个正整数 m 和 n(1<=m, n<=1000),输出 m 到 n之间的所有完数,并输出其因子。一个数如恰好等于它的因子之和,这个数称为完数,例如,6=1+2+3,其中1、2、3为因子,6为因子和。输出使用以下语句:printf("%d = 1", number);printf(" + %d", factor);printf("\n");输入输出示例:括号内为说明输入:2 (repeat=2)1 30 (m=1, n=30)400 500 (m=400, n=500)输出result:1 = 16 = 1 + 2 + 328 = 1 + 2 + 4 + 7 + 14result:496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
不知道哪里错了
我是这么做的:
------------------------------------------------------------------------------------------------
#include<stdio.h>int main(){ int factor, m, n, number, sum; int repeat, ri; scanf("%d", &repeat); for(ri=1;ri<=repeat;ri++){ scanf("%d%d", &m, &n); printf("result:\n"); for(number=m;number<=n;number++){ sum=0; for(factor=2;factor<=(number-1);factor++){ if(number%factor==0){ sum=sum+factor; } } if(sum==number){ printf("%d = 1", number); for(factor=2;factor<=(nummber-1);factor++){ if(number%factor==0){ printf(" + %d", factor); } } } } }} 展开
不知道哪里错了
我是这么做的:
------------------------------------------------------------------------------------------------
#include<stdio.h>int main(){ int factor, m, n, number, sum; int repeat, ri; scanf("%d", &repeat); for(ri=1;ri<=repeat;ri++){ scanf("%d%d", &m, &n); printf("result:\n"); for(number=m;number<=n;number++){ sum=0; for(factor=2;factor<=(number-1);factor++){ if(number%factor==0){ sum=sum+factor; } } if(sum==number){ printf("%d = 1", number); for(factor=2;factor<=(nummber-1);factor++){ if(number%factor==0){ printf(" + %d", factor); } } } } }} 展开
2个回答
展开全部
#include<stdio.h>
int main()
{
int factor, m, n, number, sum;
int repeat, ri;
scanf("%d", &repeat);
for(ri=1;ri<=repeat;ri++){
scanf("%d%d", &m, &n);
printf("result:\n");
for(number=m;number<=n;number++){
sum=1; /*这改了一下*/
for(factor=2;factor<=(number-1);factor++){
if(number%factor==0){
sum=sum+factor;
}
}
if(sum==number){
printf("%d = 1", number);
for(factor=2;factor<=(number-1);factor++){ /*这一行number的拼写*/
if(number%factor==0){
printf(" + %d", factor);
}
}
printf("\n"); /*输出下一个完全数前先换行 */
}
}
}
}
int main()
{
int factor, m, n, number, sum;
int repeat, ri;
scanf("%d", &repeat);
for(ri=1;ri<=repeat;ri++){
scanf("%d%d", &m, &n);
printf("result:\n");
for(number=m;number<=n;number++){
sum=1; /*这改了一下*/
for(factor=2;factor<=(number-1);factor++){
if(number%factor==0){
sum=sum+factor;
}
}
if(sum==number){
printf("%d = 1", number);
for(factor=2;factor<=(number-1);factor++){ /*这一行number的拼写*/
if(number%factor==0){
printf(" + %d", factor);
}
}
printf("\n"); /*输出下一个完全数前先换行 */
}
}
}
}
追问
结果跳不出来……
展开全部
完全数在 1~1000 里面只有三个 6 28 496
完全没必要去遍历,
话说很少见人用这么经典的编译器了,
它说你的 nummber 没有定义(声明),是你打错了吧
for(factor=2;factor<=(nummber-1);factor++){
完全没必要去遍历,
话说很少见人用这么经典的编译器了,
它说你的 nummber 没有定义(声明),是你打错了吧
for(factor=2;factor<=(nummber-1);factor++){
更多追问追答
追问
那怎么找啊
追答
判断范围就可以了啊
n<=6&&m>=6 就有 6 然后输出 6 = 1+2+3
同理判断输出其它
当然这是旁门左道,你要做算法,还是老老实实遍历出来
#include<stdio.h>
int main()
{
int factor, m, n, number, sum;
int repeat, ri;
scanf("%d", &repeat);
for(ri=0;ri<repeat;ri++){
scanf("%d,%d", &m, &n);///////////////////////////////
printf("result:\n");
for(number=m;number<=n;number++){
sum=1;//////////////////////////////////////////
for(factor=2;factor<(number);factor++){
if(number%factor==0){
sum=sum+factor;
}
}
if(number>1&&sum==number){
printf("%d = 1", number);
for(factor=2;factor<(number);factor++){////////////
if(number%factor==0){
printf(" + %d", factor);
}
}
printf("\n");
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询