110,825
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int count = 0;
for (int i = 1000; i < 10000; i++)
{
if (Validate(i))
{
sum += i;
count++;
}
}
Console.WriteLine("满足条件的四位自然数的个数为:{0}", count);
Console.WriteLine("满足条件的四位自然数的和为:{0}", sum);
Console.ReadKey(true);
}
static bool Validate(int num)
{
int a = num % 10; // 个位数
int b = num / 10 % 10; // 十位数
int c = num / 100 % 10; // 百位数
int d = num / 1000; // 千位数
if (a + b == c + d && c + d == 10 * (a - d))
{
return true;
}
else
{
return false;
}
}
}
}