用C语言编写:计算100到1000之间各位数字之和为5的个数

 我来答
脑筋风暴
2012-06-03
知道答主
回答量:22
采纳率:0%
帮助的人:22.4万
展开全部
#include<stdio.h>
void main()
{
int i=0,j=0,k=0;
for(i=1;i<6;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<5;k++)
{
if((i+k+j) == 5)
printf("%d\t",i*100+j*10+k);
}
}
}
}
veket的小号
2012-06-03 · TA获得超过3371个赞
知道大有可为答主
回答量:3762
采纳率:0%
帮助的人:3928万
展开全部
#include <stdio.h>
/*#define _DEBUG_INFO*/
int main()
{
int count;
int i;
for(i=100,count=0;i<1000;i++)
{
if(5==i/100+i/10%10+i%10)
{
count++;
#ifdef _DEBUG_INFO
printf("No.%d, %d = %d + %d + %d = 5\n", count, i, i/100, i/10%10, i%10);
#endif
}
}
printf("\ncount = %d\n", count);
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
caoyanchuang
2012-06-03 · TA获得超过486个赞
知道小有建树答主
回答量:115
采纳率:0%
帮助的人:105万
展开全部
#include <stdio.h>

int main()
{
int a, b, c; //定义三个整数,分别表示百位、十位、个位上的数字
int sum = 5; //三个数字的和
int bpc; //b+c
int num; //符合条件的数字
int count=0; //符合条件的数字的个数:

for (a=1; a<=sum; a++) {
bpc = sum-a;
for (b=0; b<=bpc; b++) {
count++;
}
}
printf("符合条件的数字有 %d 个。\n", count);

//输出符合条件的数字
for (a=1; a<=sum; a++) {
bpc = sum-a;
for (b=0; b<=bpc; b++) {
c = bpc - b;
num = a*100 + b*10 + c;
printf("%d\n", num);
}
}

return 0;
}

运行程序,输出如下:
符合条件的数字有 15 个。
104
113
122
131
140
203
212
221
230
302
311
320
401
410
500
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
董俊锟djk
2012-06-03 · TA获得超过2550个赞
知道小有建树答主
回答量:1231
采纳率:66%
帮助的人:1001万
展开全部
#include<stdio.h>

void main()
{
for(int i = 100; i <= 1000; i++)
{
int sum = 0;
int k = i;
while(k > 0)
{
int s = k % 10;
k /= 10;
sum += s;
}
if(sum == 5)
printf("%-4d",i);
printf("\n");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式