序列判断
算法描述
- 给出一个列表values,返回该列表的排序状态
- “ASCENDING mean”————列表中的数字按照升序排列,且没有重复值
- “DESCENDING mean”————列表中的数字按照降序排列,且没有重复值
- “NONASCENDING freq”————列表中的数字按照降序排列,且有重复值
- “NONDECENDING freq”————列表中的数字按照升序排列,且有重复值
- “NOTHING”————非以上任何一种情况
- 上面的mean代表着列表中数字的平均值,以分数的形式给出
- 上面的freq代表着重复数字出现的次数
- mean中的分子分母都不能以0开头
- values = {1,2,4,11} 返回 “ASCENDING 9/2” 平均数是: 18/4 = 9/2
- values = {1,2,2,2,3,4} 返回 “NONDESCENDING 3” 2出现了3次
- values = {6,5,1} 返回 “DESCENDING 4/1” 平均数是: 12/3 = 4/1
- values = {5,5,4,4,1} 返回 “NONASCENDING 2” 5出现了2次
- values = {1,2,3,4,1} 返回 “NOTHING”
参数定义
- 类名
Ordered
- 方法
getType
- 输入参数
vector <int>
- 输出
tring
- 方法声明
string getType(vector <int> values)
限制条件
- values包含[2,50]个字符
- values中至少2个不同元素
- values中的每个元素在[1,1000]之间
例子
- 输入
- values: {1,2,4,11}
- 输出
- ”ASCENDING 9/2”
测试实例
测试实例
实例一
- 输入
- values: {1,2,2,2,3,4}
- 输出
- ”NONDESCENDING 3”
- 输入
实例二
- 输入
- values: {6,5,1}
- 输出
- ”DESCENDING 4/1”
- 输入
实例三
- 输入
- values: {5,5,4,4,1}
- 输出
- ”NONASCENDING 2”
- 输入
代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// -------------- start of solution ------------
class Ordered {
public:
string getType(vector<int> values) {
// calcuate the times of decending, acending, and equaling
int decendingOper = 0, acendingOper = 0, equalOper = 0;
int totalSize = (int)values.size();
for(int i = 0; i < totalSize - 1; i++) {
if(values[i + 1] > values[i])
acendingOper++;
else if(values[i + 1] < values[i])
decendingOper++;
else
equalOper++;
}
// according the result calcuted before, determine the return condition
if(acendingOper + 1== totalSize) {
string mean = calcMean(values);
return "ASCENDING " + mean;
}
else if(decendingOper + 1 == totalSize) {
string mean = calcMean(values);
return "DECENDING " + mean;
}
else if(acendingOper + equalOper + 1 == totalSize)
return "NONDECENDING " + calFreq(values);
else if(decendingOper + equalOper + 1 == totalSize)
return "NONACENDING " + calFreq(values);
else
return "NOTHING";
}
private:
// calculate the number of times that appears most frequently in the int list
string calFreq(vector<int> values) {
int freq = 0;
int tempFreq = 0;
int totalSize = values.size();
for(int i = 0; i < totalSize - 1; i++) {
if(values[i + 1] == values[i])
tempFreq++;
else {
if(freq < tempFreq)
freq = tempFreq;
tempFreq = 0;
}
}
if(freq < tempFreq)
freq = tempFreq;
return to_string(freq + 1);
}
// calcute the Largest Common Divisor
int largestCommonDivisor(int a, int b) {
int c;
while(b != 0) {
c = a % b;
a = b;
b = c;
}
return a;
}
// calculate the mean value that the problem had mentioned
string calcMean(vector<int> values) {
int sum = 0;
int totalSize = values.size();
for(vector<int>::const_iterator iter = values.cbegin(); iter < values.cend(); iter++)
sum += *iter;
int lcd = largestCommonDivisor(sum, totalSize);
string mean = to_string(sum/lcd) + "/" + to_string(totalSize/lcd);
return mean;
}
};
// -------------- end of solution --------------
int main() {
Ordered od;
vector<int> values {1, 2, 3, 4, 1};
string ret = od.getType(values);
cout << ret << endl;
cout.flush();
}