C++编程。已知祖父年龄70岁,长孙20岁,次孙15岁,幼孙5岁,问过多少年,三个孙子的年龄之和同祖父的年龄相等
发布网友
发布时间:2022-05-16 00:14
我来回答
共5个回答
热心网友
时间:2023-10-02 16:47
/*C++编程。已知祖父年龄70岁,长孙20岁,次孙15岁,幼孙5岁,问过多少年,三个孙子的年龄之和同祖父的年龄相等...........by Mr.Kong */
#include<iostream>
using namespace std;
int main()
{
int grandpa=70,grandson1=20,grandson2=15,grandson3=5,i;
for(i=1;i<100;i++)
{
grandpa++;grandson1++;grandson2++;grandson3++;
if(grandpa==grandson1+grandson2+grandson3)
cout<<i;
}
return 0;
}
热心网友
时间:2023-10-02 16:48
int zufu,a,b,c;
zufu=70;a=20;b=15;c=5;
int how=0;
while(1)
{
how = how + 1;
a = a + 1;
b = b + 1;
c = c + 1;
if(a+b+c==zufu)
{
printf("%d\n",how);
break;
}
else if(a+b+c>zufu)
{
printf("不会相等\n");
break;
}
}
热心网友
时间:2023-10-02 16:48
#include <iostream>
#include "cstdio"
#include "typeinfo"
using namespace std;
int main()
{
int a[4] = {70,20,15,5};
int i,year = 0;
while(1){
if(a[0] == (a[1] + a[2] + a[3]))
break;
for(i = 0;i < 4;i ++){
a[i]++;
}
year++;
}
printf("%d年后\n",year);
return 0;
}
热心网友
时间:2023-10-02 16:49
#include<stdio.h>
int main()
{
int a = 70;
int b = 20;
int c = 15;
int d = 5;
int n = 0;
while((a+n) != (b+c+d+3*n))
++n;
printf("需要再过%d年!\n",n);
return 0;
}
答案是15年:(70-20-15-5)/(3-1)
热心网友
时间:2023-10-02 16:50
#include<stdio.h>
#include<stdlib.h>
void main()
{
int ageA=70,ageB=20,ageC=15,ageD=5,count=0;
do
{
ageA+=1;
ageB+=1;
ageC+=1;
ageD+=1;
count+=1;
}while(ageA!=(ageB+ageC+ageD));
printf("要过%d年之后三个孙子的年龄之和同祖父年龄相等,祖父还在就是神仙了!!\n",count);
}