个人认为这个题目出的有点歧义。因为不知道是否连带输入的两个数。所以目前网上基本上有几种解释:
不包含输入的两个数、包含其中一个数、包含输入的两个数。
网上很多代码都是包含其中一个数,要么带上上限 要么带上下限 ,主要是看用的是自加还是自减。
这里说一下包含和不包含上下限的情况。
当为包含输入的两个数时:
#include <iostream>
using namespace std;
int main()
{
int one,one1,one2;
cout << "请输入两个数 " << endl;
cin >> one1 >> one2;
one = one1 - one2;
if (one == 0)
{
cout << "相等整数间无整数\n" << endl;
}
else
{
while (one1 > one2) {
cout << "两整数之间的中间数有: " << one1 << endl;
one1--;
}
while (one1 < one2) {
cout << "两整数之间的中间数有: " << one1 << endl;
one1++;
}
//打印最后一次--的结果
cout << "两整数之间的中间数有: " << one1 << endl;
}
return 0;
}
上述代码测试:
当理解为不包含两个数时:(掐头去尾)
#include <iostream>
using namespace std;
int main()
{
int one,one1,one2;
cout << "请输入两个数 " << endl;
cin >> one1 >> one2;
one = one1 - one2;
if (abs(one) <= 1)
{
cout << "相邻或相等整数间无整数\n" << endl;
}
while (one1 > (one2+1)) {
one1--;
cout << "上述两个整数之间的中间数有: " << one1 << endl;
}
while ((one1+1) < one2) {
one1++;
cout << "上述两个整数之间的中间数有: " << one1 << endl;
}
return 0;
}
上述代码测试:
当one1>one2 和one1<one2 的情况时:
当 |one1-one2 |<=1的情况时: