[英]Extracting numbers from 3 digit number in C++ is it possible w/o using arrays?
问题是:编写一个 function 作为输入参数,它接收一个三位正数,因此必须得到相同的 3 位数字除以中位数获得的最大和最小数字之和。 示例:function 的输入参数 438 同位最大的是 843,最小的是 348,所以应该计算 (843 + 348) / 4。
我已经尝试过了,结果还可以,但是我的代码似乎很复杂,所以我问有没有更好的方法来做到这一点?
提前致谢
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int check(int x) {
int a, b, c, biggestNum, smallestNum, medianNum;
a = x / 100;
b = (x / 10) % 10;
c = x % 10;
if (a > b && a > c && b > c) {
biggestNum= a * 100 + b * 10 + c;
smallestNum= c * 100 + b * 10 + a;
medianNum= b;
}
else if (a > b && a > c && b < c) {
biggestNum= a * 100 + c * 10 + b;
smallestNum= b * 100 + c * 10 + a;
medianNum= c;
}
else if (b > a && b > c && a < c) {
biggestNum= b * 100 + c * 10 + a;
smallestNum= a * 100 + c * 10 + b;
medianNum= c;
}
else if (b > a && b > c && a > c) {
biggestNum= b * 100 + a * 10 + c;
smallestNum= c * 100 + a * 10 + b;
medianNum= a;
}
else if (c > a && c > b && a > b) {
biggestNum= c * 100 + a * 10 + b;
smallestNum= b * 100 + a * 10 + c;
medianNum= a;
}
else if (c > a && c > b && a < b) {
biggestNum= c * 100 + b * 10 + a;
smallestNum= a * 100 + b * 10 + c;
medianNum= b;
}
cout << "Smallest number is: " << smallestNum<< " ,biggest is: " << biggestNum << " and median is: " << medianNum<< "." << endl;
return (biggestNum + smallestNum) / medianNum;
}
int main() {
cout << "Enter one 3 digit positive number: ";
int x;
cin >> x;
float result = check(x);
cout << "The result is: " << result << "." << endl;
system("pause");
return 0;
}
考虑到结果是用 integer 算术计算的,发布的代码不能真正产生正确的答案:
int check(int x) // <- note the type of the returned value
{
int biggestNum, smallestNum, medianNum;
// ...
return (biggestNum + smallestNum) / medianNum; // <- This is an integer division
}
int main()
{
int x;
// ...
float result = check(x); // Now it's too late to get the right result
}
该逻辑也没有考虑所有可能的情况,事实上它忽略了重复的数字和大的if else if
构造,缺少默认分支(最终无条件else
),使那些未初始化的变量未确定,因此以下操作给出一个毫无意义的结果。
鉴于分配限制,我会写如下内容
#include <utility>
// The assignment is about 3-digit numbers, you should check that x is actually in
// the range [100, 999]. Note that one of the extremes is a special case.
// Well, both, actually.
double I_ve_no_idea_how_to_name_this(int x)
{
constexpr int base = 10;
int smallest = x % base;
x /= base;
int median = x % base;
x /= base;
// Note that this "works" (extracting the third digit) even if
// x isn't a 3-digit number. If you can assure the input is well
// defined, you can simplify this.
int biggest = x % base;
// Now we can sort the previous variables.
using std::swap;
if ( median < smallest ) {
swap(median, smallest);
}
// Now I know that smallest <= median
if ( biggest < median ) {
swap(biggest, median);
}
// Now I know that median <= biggest
// ...
// Is that enough or am I missing something here?
// Please think about it before running the code and test it.
// Once the variables are sorted, the result is easily calculated
return (biggest + smallest + base * (2 * median + base * (biggest + smallest)))
/ static_cast<double>(median);
}
尝试这个...
int check(int x) {
int a,b,c,temp;
a = x/100;
b = (x/10)%10;
c = x%10;
if(b>a){
temp=a;
a=b;
b=temp;
}
if(c>b){
temp=b;
b=c;
c=temp;
}
if(b>a){
temp=a;
a=b;
b=temp;
}
cout << "smallest: " << a+(b*10)+(c*100) << "\n";
cout << "biggest: " << (a*100)+(b*10)+c << "\n";
cout << "median: " << b << "\n";
return (((a+c)*100)+(2*b*10)+(a+c))/b;
}
首先,您应该使用更具描述性的变量名称,并且应该在定义时初始化每个变量。 这两个步骤极大地有助于消除复杂程序中的错误。 我知道这并不复杂,但这是一个好习惯。 其次,标准库可以帮助找到最大和最小的数字,从而使 rest 变得简单。 所以这是一个没有任何if
语句的例子。
double check(int x)
{
int a = x / 100;
int b = (x / 10) % 10;
int c = x % 10;
int bigdigit = std::max({ a, b, c }); // find largest
int smalldigit = std::min({ a, b, c }); //find smallest
int middledigit = a + b + c - bigdigit - smalldigit; // sum of all digits minus largest and smallest gives the remaining one
int biggest = smalldigit + middledigit * 10 + bigdigit * 100;
int smallest = smalldigit * 100 + middledigit * 10 + bigdigit;
std::cout << "biggest: " << biggest << '\n';
std::cout << "smallest: " << smallest << '\n';
std::cout << "median: " << middledigit << '\n';
return (1.0 * biggest + 1.0 * smallest) / (1.0 * middledigit); --using double instead of int, as result could be fractional
}
检查此检查 function。
int check(int x) {
if(x >= 1000) x %= 1000; //or return -1;
//get digits
int M = x/100;
int C = (x/10)%10;
int m = x%10;
//unrolled bubble sort.
if(M < C) swap(M,C);
if(C < m) swap(C,m);
if(M < C) swap(M,C);
//simplified formula
return ((m+M)*(101))/C + 20;
}
//derivation of formula
B = M*100 + C*10 + m;
s = m*100 + C*10 + M;
B+s = (m+M)*100 + C*20 + (m+M)
= (m+M)*(100 + 1) + C*20
(B+s)/C = ((m+M)*(100 + 1) + C*20)/C
= ((m+M)*(101))/C + 20
最简单的解决方案是将数字转换为std::string
。 如果我们对字符串中的数字进行排序,我们将得到最小的数字。
如果我们再次对字符串进行排序,但从最高位到最低位,我们将得到最大的数字。
这使得整个 function 变得非常简单。
请参见:
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
double check(unsigned int test) {
// Convert number to string
std::string testString{ std::to_string(test) };
// Sort the characters (the digits) of the number --> Get smallest value
std::sort(testString.begin(), testString.end());
const unsigned int smallest{static_cast<unsigned int>(stoul(testString))};
// Sort the characters (the digits) of the number reverse --> Get biggest value
std::sort(testString.begin(), testString.end(), std::greater<char>());
const unsigned int greatest{ static_cast<unsigned int>(stoul(testString)) };
// Get result
return (smallest + greatest) / 2.0;
}
int main() {
// Give instrunction to the user
std::cout << "Enter one 3 digit positive number: ";
// Get test value from user
unsigned int test;
std::cin >> test;
// Show the result
std::cout << "\nThe result is: " << check(test) << "\n";
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.