法1 区间代换
namespace ConsoleApplication32 { class Program { static void Main(string[] args) { while (true) { int x, y,a; Random r = new Random(); x = r.Next(1, 10);//千位数随机1-9 a = r.Next(2, 10);//a为4位数的百位数 y = r.Next(1, a);//随机数取不到a,故三位数的百位数永远小于a Console.WriteLine((x*1000+a*100) + "-" + (y*100) + "=" +((x*1000+a*100)-(y*100))); Console.ReadLine(); } } } }
法2
namespace ConsoleApplication32 { class Program { static void Main(string[] args) { while (true) { int x, y, n, m, p; Random r = new Random(); x = r.Next(1000, 10000);//可以看成x为两位数,y为个位数,两位数x对10取余为a,y = r.Next(0, a); y = r.Next(100, 1000); m = y / 100; p = x / 1000; n = (x - p * 1000) / 100;//保证4为数的百位数大于3位数的百位数 if (n > m) { Console.WriteLine(x + "-" + y + "=" + (x - y)); } Console.ReadLine(); } } }