1108 Finding Average (20 point(s))
The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N numbers are given in the next line, separated by one space.
Output Specification:
For each illegal input number, print in a line ERROR: X is not a legal number where X is the input. Then finally print in a line the result: The average of K numbers is Y where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined instead of Y. In case K is only 1, output The average of 1 number is Y instead.
Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
Sample Output 1:
ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38
Sample Input 2:
2
aaa -9999
Sample Output 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
题目大意:过滤符合要求的输入然后按规定输出
解题思路:用atof和字符串处理简单处理一下就好了
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
vector<string> cur;
vector<double> res;
map<int, bool> a;
int main()
{
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
string tmp;
cin >> tmp;
cur.push_back(tmp);
bool flag = true;
int cp=0;
int index;
for (int j = 0; j < tmp.size(); j++) {
if (j == 0) {
if (tmp[j] >= '0'&&tmp[j] <= '9'||tmp[j]=='-') {
continue;
}
else {
flag = false;
break;
}
}
else {
if (tmp[j] >= '0'&&tmp[j] <= '9') {
continue;
}
else if (tmp[j] == '.') {
cp++;
index = j;
}
else {
flag = false;
break;
}
}
}
if (flag&&cp >= 2) {
flag = false;
}
else if (flag&&cp == 1 && tmp.size() - index - 1 > 2) {
flag = false;
}
else if (flag&&cp == 1) {
if (atof(tmp.c_str()) >= -1000 && atof(tmp.c_str()) <= 1000)
res.push_back(atof(tmp.c_str()));
else
flag = false;
}
else if (flag&&cp == 0) {
if (atof(tmp.c_str()) >= -1000 && atof(tmp.c_str()) <= 1000)
res.push_back(atof(tmp.c_str()));
else
flag = false;
}
if (!flag)
a[i] = 0;
else
a[i] = 1;
}
for (int i = 0; i < cur.size(); i++) {
if (a[i] == 0) {
cout << "ERROR: " << cur[i] << " is not a legal number" << endl;;
}
}
double sum = 0;
for (int i = 0; i < res.size(); i++) {
sum += res[i];
}
if (res.size() == 0) {
printf("The average of 0 numbers is Undefined\n");
}
else if (res.size() == 1) {
printf("The average of 1 number is %.2lf", sum);
}
else {
printf("The average of %d numbers is %.2lf", res.size(),sum*1.0/res.size());
}
return 0;
}
1112 Stucked Keyboard (20 point(s))
On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for k times.
Now given a resulting string on screen, you are supposed to list all the possible stucked keys, and the original string.
Notice that there might be some characters that are typed repeatedly. The stucked key will always repeat output for a fixed k times whenever it is pressed. For example, when k=3, from the string thiiis iiisss a teeeeeest we know that the keys i and e might be stucked, but s is not even though it appears repeatedly sometimes. The original string could be this isss a teest.
Input Specification:
Each input file contains one test case. For each case, the 1st line gives a positive integer k (1<k≤100) which is the output repeating times of a stucked key. The 2nd line contains the resulting string on screen, which consists of no more than 1000 characters from {a-z}, {0-9} and _. It is guaranteed that the string is non-empty.
Output Specification:
For each test case, print in one line the possible stucked keys, in the order of being detected. Make sure that each key is printed once only. Then in the next line print the original string. It is guaranteed that there is at least one stucked key.
Sample Input:
3
caseee1__thiiis_iiisss_a_teeeeeest
Sample Output:
ei
case1__this_isss_a_teest
题目大意:有几个键是坏掉的,输出坏掉的键,输出正确的字符串
解题思路:怎么搞都感觉不好,抄了别人的代码找了个简洁的。
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
int main()
{
int k;
string s;
cin>>k>>s;
int n=s.size();
bool isprob[37];
memset(isprob,true,sizeof(isprob));
set<char>res;
for(int i=0;i<n;)
{
int j=i+1,count=1,idx;
if(isdigit(s[i])) idx=s[i]-'0';
else if(isalpha(s[i])) idx=s[i]-'a'+10;
else idx=36;
while(s[j]==s[i]&&j<n&&count<k)
{
count++;
j++;
}
if(count==k&&isprob[idx])
{
res.insert(s[i]);
}
else if(count!=k)
{
isprob[idx]=false;
res.erase(s[i]);
}
i=j;
}
set<char>res2=res;
for(int i=0;i<n;i++)
{
if(res2.find(s[i])!=res2.end())
{
cout<<s[i];
res2.erase(s[i]);
}
if(res2.empty()) break;
}
cout<<endl;
for(int i=0;i<n;)
{
if(res.find(s[i])!=res.end())
{
cout<<s[i];
i+=k;
}
else
{
cout<<s[i];
i++;
}
}
cout<<endl;
}

本文探讨两个算法挑战,一是过滤非法输入并计算合法数字平均值的程序设计,二是识别故障键盘重复字符及还原正确字符串的解决方案。通过代码实现,详细解释了如何处理复杂输入和识别模式。
218

被折叠的 条评论
为什么被折叠?



