网友年龄(简单枚举)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LoveJiaYu/article/details/79228314

1.问题描述:

网友年龄:

某君新认识一网友。
当问及年龄时,他的网友说:
“我的年龄是个2位数,我比儿子大27岁,
如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄”

请你计算:网友的年龄一共有多少种可能情况?
提示:30岁就是其中一种可能哦.
请填写表示可能情况的种数。

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

2.问题分析:

不妨设网友年龄在25~100岁之间,枚举每个年龄并且计算出儿子的年龄,看看差值是否为27即可。

3.程序解答:7

#include <stdio.h>
int main()
{
	int count = 0;
	for(int father = 25; father < 100; father++)
	{
		int son = (father%10)*10+father/10;
		if(father - son == 27)
		{
			count++;
		}
	}
	printf("%d\n", count);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/LoveJiaYu/article/details/79228314