C# 获取一个范围内不带特定运算符的可除数(+;、-、/、*、%、+;=%=,等等)
查找输入范围内可被3整除的数字。只能使用=、++、--运算符 我尝试使用移位运算符和其他循环来获得余数,但我总是需要a-=或类似的东西C# 获取一个范围内不带特定运算符的可除数(+;、-、/、*、%、+;=%=,等等),c#,division,mod,C#,Division,Mod,查找输入范围内可被3整除的数字。只能使用=、++、--运算符 我尝试使用移位运算符和其他循环来获得余数,但我总是需要a-=或类似的东西 Console.Clear(); int n, d, count = 1; // get the ending number n = getNumber(); // get the divisor d = 3;//
Console.Clear();
int n,
d,
count = 1;
// get the ending number
n = getNumber();
// get the divisor
d = 3;// getDivisor();
Console.WriteLine();
Console.WriteLine(String.Format("Below are all the numbers that are evenly divisible by {0} from 1 up to {1}", d, n));
Console.WriteLine();
// loop through
while (count <= n)
{
// if no remainder then write number
if(count % d == 0)
Console.Write(string.Format("{0} ", count));
count++;
}
Console.WriteLine();
Console.WriteLine();
Console.Write("Press any key to try again. Press escape to cancel");
Console.Clear();
int n,
D
计数=1;
//获取结束号码
n=getNumber();
//求除数
d=3;//getDivisor();
Console.WriteLine();
WriteLine(String.Format(“下面是从1到{1}可被{0}整除的所有数字,d,n));
Console.WriteLine();
//循环通过
而(count如果赋值允许使用==运算符,则可以使用
int remainder = 0; // assumes we always count up from 1 to n, we will increment before test
在循环内部,将现有的if替换为
remainder++;
if (remainder == 3) {
Console.Write(string.Format("{0} ", count));
remainder = 0;
}
[编辑:纠正了代码中的输入错误]想想下面的数学:
2 x 3 = 3 + 3
3 x 3 = 3 + 3 + 3
4 * 3 = 3 + 3 + 3 + 3
……等等
同样,被3整除意味着乘以3的数必须是偶数..所以
public bool EvenlyDivisibleBy3(int aNumber)
{
int even = 2;
int currentMultiple = 0;
while (currentMultiple < aNumber)
{
int xTimes = 0;
for (int x = 1; x <= even; x++)
{
((xTimes++)++)++; // add three to xTimes
}
currentMultiple = xTimes;
(even++)++: // next even number
}
return currentMultiple == aNumber;
}
public bool均匀可分by3(整数)
{
int偶数=2;
int currentMultiple=0;
while(currentMultiple 对于(int x=1;x),我现在使用%运算符,无法找到其他解决方案。C lang\lang不可知类似问题