pat 1108. Finding Average (20)
来源:互联网 发布:软件教育 编辑:程序博客网 时间:2024/05/23 01:59
https://www.patest.cn/contests/pat-a-practise/1108
笨方法:
#include <cstdio>#include <iostream>#include <string>using namespace std;bool to2Doboule(string s, double &f) {double res = 0.0;int i = 0;if (s[i] == '-') i++;int len = s.length();while ('0' <= s[i] && s[i] <= '9'&&i < len) {res *= 10;res += s[i] - '0';i++;}if (s[i] == '.') {i++;if (len - i > 2) return false;double base = 10;while ('0' <= s[i] && s[i] <= '9' && i < len) {res += (s[i] - '0') / base;base *= 10;i++;}if (i != len) return false;f = s[0] == '-' ? -res : res;return true;}else if(i==len) {f = s[0] == '-' ? -res : res;return true;}else {return false;}}int main(){int n, count = 0;double sum = 0.0;string a;scanf("%d\n", &n);for (size_t i = 0; i < n; i++){cin >> a;double res;if (to2Doboule(a, res)&& res >= -1000 && res <= 1000 ) {//printf("%.2lf\n",res);count++;sum += res;}else {printf("ERROR: %s is not a legal number\n", a.c_str());}}if (count == 1) {printf("The average of 1 number is %.2lf\n", sum);}else if (count > 1) {printf("The average of %d numbers is %.2lf\n", count, sum / count);}else {printf("The average of 0 numbers is Undefined\n");}return 0;}
#include <iostream> #include <string> #include <cstdlib> using namespace std; int main(){ int n,cnt = 0; cin>>n; string num; double sum = 0.0; for(int i=0;i<n;i++){ cin>>num; char * pos; double value = strtod(num.c_str(),&pos); bool ok = true; if(*pos != '\0')ok = false; if(value<-1000 || value >1000)ok = false; int lpos = num.find_last_of("."); if(lpos != string::npos) if(num.size() - lpos > 3)ok = false; if(ok){ cnt++; sum += value; } else printf("ERROR: %s is not a legal number\n",num.c_str()); } if(cnt != 0){ if(cnt != 1)printf("The average of %d numbers is %.2lf\n",cnt,sum/cnt); else printf("The average of 1 number is %.2lf\n",sum); } else cout<<"The average of 0 numbers is Undefined\n"; return 0; }
利用sscanf,sprinf的方法http://www.liuchuo.net/archives/1924
#include <iostream>#include <cstdio>#include <string.h>using namespace std;int main() { int n, cnt = 0; char a[50], b[50]; double temp, sum = 0.0; cin >> n; for(int i = 0; i < n; i++) { scanf("%s", a); sscanf(a, "%lf", &temp); sprintf(b, "%.2lf",temp); int flag = 0; for(int j = 0; j < strlen(a); j++) { if(a[j] != b[j]) { flag = 1; } } if(flag || temp < -1000 || temp > 1000) { printf("ERROR: %s is not a legal number\n", a); continue; } else { sum += temp; cnt++; } } if(cnt == 1) { printf("The average of 1 number is %.2lf", sum); } else if(cnt > 1) { printf("The average of %d numbers is %.2lf", cnt, sum / cnt); } else { printf("The average of 0 numbers is Undefined"); } return 0;}
0 0
- [pat]1108. Finding Average (20)
- PAT 1108. Finding Average (20)
- pat 1108. Finding Average (20)
- PAT--1108. Finding Average
- 【PAT】1108. Finding Average
- pat 1108. Finding Average
- 【PAT】【Advanced Level】1108. Finding Average (20)
- Pat(A) 1108. Finding Average (20)
- PAT 甲级 1108. Finding Average (20)
- PAT 甲级:1108. Finding Average
- pat 甲级 1108. Finding Average
- 1108. Finding Average (20)
- 1108. Finding Average (20)
- 1108. Finding Average (20)
- 1108. Finding Average (20)
- 1108. Finding Average (20)
- 1108. Finding Average (20)
- 1108. Finding Average (20)
- 跪求大神指导错误点。
- PAT 1017. Queueing at Bank
- Java加载Class文件的原理机制
- 剑指offer经典编程(十一)
- 用户信息批量导出至EXCEL
- pat 1108. Finding Average (20)
- [NSBundle mainBundle] infoDictionary
- android studio新建项目后出现bug finished with non-zero exit value 1
- 面试中经常被问到的单例类
- dom4j解析xml
- __weak & __unsafe_unretained的用法以及区别
- hihoCoder 1142 三分求极值
- HTML之认识标签(二)
- 安卓绑定本地服务Service并与之通信