Home > Net >  Do you have a can write
Do you have a can write

Time:09-24

Take the natural Numbers below 10000 eligible, condition is: one thousand number and one hundred ten digits with bits digital must equal the sum of the Numbers, and the one thousand number and one hundred number is equal to the sum of the difference between the unit digit with one thousand - bit digital 10 times, calculate and output the number of the four natural number cn and the number and the sum,

CodePudding user response:

Assuming four digits is the abcd, by conditions have a + b=c + d, a + b=10 * (d - a), c and d can be obtained on a, b expression, 10 * 11 * d=a + b, c=a + b - d, notice the 0 & lt;=d<=9, 0 & lt;=c<=9 and c and d are integers, so two loops can solve the problem,
Int a, b, c, d;
List Result=new List (a);
For (a=1; A<=9; +)
{
For (b=0; B<=9; B++)
{
D=(11 * a + b)/10;
If ((10 * d)! * a + b)=(11) continue;
If (d> 9) continue;
C=a + b - d;
If (c<0 | | c> 9) continue;
Result. The Add (1000 * 100 * a + b + 10 * c + d);
}
}
Cn=result. Count;
Sum=((int []) (result) ToArray ())). The sum ();

CodePudding user response:

 using System; 
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;

The namespace ConsoleApplication1
{
Class Program
{
The 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 (" meet the condition of four the number of natural number is: {0} ", count);
Console. WriteLine (" meet the conditions of the four natural number and is: {0} ", sum);
The Console. ReadKey (true);
}

The static bool Validate (int num)
{
Int a=num % 10;//single
Int b=num/10% 10;//ten digits
Int num=c/100% 10;//the digits
Int num=d/1000;//thousand digits
If (a + b==c + d & amp; & C + d==10 * (a - d))
{
return true;
}
The else
{
return false;
}
}
}
}
  •  Tags:  
  • C #
  • Related