这个用C语言怎么编程?代码是什么?
发布网友
发布时间:2022-05-17 07:57
我来回答
共5个回答
热心网友
时间:2023-10-14 00:29
//1.
#include<stdio.h>
#define MAX 9999
int stove(int munber)
{
int n = 0;
for(; munber>0; munber/=10)
{
n += munber%10;
}
return n;
}
int main(void)
{
int i, j, k;
for(i=1000; i<=MAX; i++)
{
if(stove(i)==8)
printf("%d\t", i);
}
return 0;
}
//2.
#include<stdio.h>
int stove(int munber)
{
int i = 2;
if(munber==2)
return munber;
else
for(i=2; i<munber; i++)
{
if(munber%i==0)
return 0;
}
return munber==1 ? 0 : munber;
}
int main(void)
{
int n = 0;
while(scanf("%d", &n))
{
if(stove(n))
printf("%d是素数\n", n);
else
printf("%d不是素数\n", n);
}
return 0;
}
//3.
#include<stdio.h>
#define PMAX 30
#define MMAX 50
int main(void)
{
enum money{CHILD=1, WOMAN, MAN};
int man, woman, child, mmoney, wmoney, cmoney;
for(man=0,mmoney=0;man<=PMAX&&mmoney<=MMAX; man++,mmoney+=MAN)
for(woman=0,wmoney=0; woman<=PMAX&&wmoney<=MMAX; woman++,woman+=WOMAN)
for(child=0,cmoney=0; child<=PMAX&&cmoney<=MMAX; child++,cmoney+=CHILD)
{
if((man+woman+child)==PMAX&&(mmoney+wmoney+cmoney)==MMAX)
printf("男人%d\t女人%d\t小孩%d\n", man, woman, child);
}
return 0;
}
热心网友
时间:2023-10-14 00:30
第1题:
#include <stdio.h>
int main()
{
int a[4];
int n;
for(n=1000;n<10000;n++)
{
a[0]=n/1000;
a[1]=(n-a[0]*1000)/100;
a[2]=(n-a[0]*1000-a[1]*100)/10;
a[3]=n-a[0]*1000-a[1]*100-a[2]*10;
if(a[0]+a[1]+a[2]+a[3] == 8)
printf("%d\t",n);
}
return 0;
}
第2题:
/*判断一个数是否为素数*/
#include <stdio.h>
#include <math.h>
int main()
{
int n,m,flag=1;
printf("Input n:");
scanf("%d",&n); //输入一个正整数
for(m=2;m<=sqrt(n)&&flag;m++)
if(n%m==0) //如果不是素数,则标志位置0并退出for循环
{
flag=0;
break;
}
if(flag==1)
printf("%d is a prime number.\n",n);
else
printf("%d is not a prime number.\n",n);
return 0;
}
第3题:
#include <stdio.h>
int main()
{
int man,woman,child;
for(man=1;man<17;man++)
for(woman=1;woman<25;woman++)
for(child=1;child<30;child++)
if(man+woman+child == 30 && man*3+woman*2+child == 50)
{
printf("man is %d\n",man);
printf("woman is %d\n",woman);
printf("child is %d\n\n",child);
}
return 0;
}
热心网友
时间:2023-10-14 00:30
1.
#include<stdio.h>
void main(){
int numA,numB,numC,numD,num;
for (num = 1000; num <= 9999; num++) {
numA = i / 1000;
numB = i % 1000 / 100;
numC = i % 1000 % 100 / 10;
numD = i % 1000 % 100 % 10;
if (numA + numB + numC + numD == 8) {
printf("各位数之和等于8的数有:%d",num);
}
}
2.
#include<stdio.h>
void main(){
int count = 0;
printf("请输入一个数:");
int num = 0,i = 0;
scanf("%d",&num);
while (num < 2) {
if (num >= 2) {
break;
}
printf("请输入一个大于等于2的数字!");
printf("\n");
printf("请输入一个数:");
scanf("%d",&num);
}
for (i = 2; i <= num; i++) {
if (num % i == 0) {
count++;
}
}
if (count <= 1) {
printf("这个数是质数!");
}else{
printf("这个数不是质数!");
}
}
3.#include<stdio.h>
void main(){
int men,women,children,money,count=0;
for (men = 1; men <= 18; men++) {
for (women =1; women < 25; women++) {
children = 30 - men - women;
if (3 * men + 2 * women + children == 50) {
count++;
printf( "%d.男人有:%d个,女人有:%d个,小孩有:%d个。"count,men,women,children);
}
}
}
}
热心网友
时间:2023-10-14 00:31
1.求1000-9999各个数字之和等于8的数
#include<stdio.h>
int main()
{
int n;
int sum;
int val ;
for(n = 1000; n < 9999; n++)
{
val = n;
sum = 0;
while(val)
{
sum = sum + val%10;
val = val/10;
}
if(sum == 8)
printf("%d ",n);
}
printf("\n");
}
2.判断一个数判断是不是素数
#include<stdio.h>
int main()
{
int n;
int i;
printf("please input a number:");
scanf("%d",&n);
for(i = 2; i < n; i++)
{
if(n % i == 0)
break;
}
if(i == n)
printf("%d是素数\n",n);
else
printf("%d是不是素数\n",n);
printf("\n");
}
3.
#include <stdio.h>
main()
{
int x,y,z;
printf("男人\t 女人\t 小孩\t\n");
for(x=0; x<=30; x++)
for(y=0; y<=30; y++)
for(z=0; z<=30; z++)
if(x+y+z==30 && 3*x+2*y+z==50)
printf("%2d\t%5d\t%5d\n",x,y,z);
}追答为什么我先答的还不采纳我
桑心
热心网友
时间:2023-10-14 00:31
1.
int main(int argc, char *argv[]){
for(int i=1000;i<=9999;i++){
int a=i/1000;
int b=(i/100)%10;
int c=(i/10)%10;
int d=i%10;
s=a+b+c+d;
if(s==8){
printf("%d",s);
}
}
return 0;
}
2.int main(int argc, char *argv[]){
int a,b=0;
scanf("%d",a);
for(int i=1;i<=a/2;i++){
if(a%i!=0){
continue;
}else{
printf("这不是素数");
b=1;
break;
}
}
if(b!=1){
printf("这是素数");
}
return 0;
}
3
int main(int argc, char *argv[])
{
for(int i=0;i<=30;i++)
for(int j=0;j<30-i;j++){
if((3*i+2*j+(30-i-j))==50){
printf("%d\t%d\t%d",i,j,30-i-j);
printf("\n");
}
}
return 0;
}