繁体   English   中英

如何仅使用循环来划分两个用户输入并且不允许使用任何操作?

[英]How can I divide two user inputs using only loops and using any operations is not allowed?

Console.Write("Input Value for A: ");

int a = int.Parse(Console.ReadLine());

Console.Write("Input Value for B: ");
int b = int.Parse(Console.ReadLine());

int acc = 0;
for (int i = a; a >= b; a--)
{
    acc += b;
}

Console.Write("The quotient is {0}", acc);
Console.ReadKey(true);

好吧,这是一个棘手的问题,因此我立即不喜欢它。 解决方案如下:

var quotient = 0;   
for (var acc = b; acc <= a; acc += b, quotient += 1);
Console.Write($"The quotient of {a} divided by {b} is {quotient}");

诀窍是您仍在使用操作,但作为循环本身的一个步骤,而不是作为循环体内的显式操作,这可能是练习的重点。

更新:此代码仅在ab具有相同符号时才给出准确的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM