这是问题中的第一个“数字”为“n”,“其他数字”为 10 的程序。
void divideme()
static int count=0; //initalised a variable which I'll be returning the value of.
int n;
cin>>n;//taken input of variable which I want to divide by another number (say 10 in this case)
int &rem=n;//created a reference variable which stores the value of n.
while (rem>=10) {
rem=rem%10; //this is to be corrected as rem = rem - 10;
count++;
}
return count;
Run Code Online (Sandbox Code Playgroud)
你的代码太过分了。只需进行一次除法即可。结果是 10 进入该数字的次数。根本不需要循环。该%运算符为您提供除法的模(余数),这不是您在这种情况下需要的。
int divideme()
{
int n;
cin>>n; //get input which I want to divide by another number (say 10 in this case)
return (n / 10);//return how many times it divides by 10
}
Run Code Online (Sandbox Code Playgroud)
例如:
9 / 10 = 0
9 % 10 = 9
Run Code Online (Sandbox Code Playgroud)
10 进 9 0 次,余数为 9。
12345 / 10 = 1234
12345 % 10 = 5
Run Code Online (Sandbox Code Playgroud)
10 进 12345 1234 次,余数 5。