写在前面
- 思路分析
- 字符数组存储读入数据,
char a[50], b[50];
- 字符串格式化读取、格式化写入函数
- sscanf() – 从1个字符串中读进与指定格式相符的数据
- sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串
- 问题点
- 合法区间、合法实数校验,
[−1000,1000]
- 输出坑,
numbers / numbers
-
The average of K numbers is Y,其中 K 是合法输入的个数,Y 是它们的平均值,精确到小数点后 2 位
-
如果平均值无法计算,则用 Undefined 替换 Y
-
如果 K 为 1,则输出 The average of 1 number is Y
- 题目简单,10分钟a题
测试用例
input:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
output:
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
input:
2
aaa -9999
output:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0
ac代码
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main()
{
int n, cnt = 0;
cin >> n;
char a[50],b[50];
double tmp, sum = 0.0;
for(int i=0; i<n; i++)
{
scanf("%s", a);
sscanf(a, "%lf", &tmp);
sprintf(b, "%.2f", tmp);
int flag = 0;
for(int j=0; j<strlen(a); j++)
if(a[j]!=b[j]) flag = 1;
if(flag || tmp<-1000 || tmp>1000)
{
printf("ERROR: %s is not a legal number\n", a);
continue;
}
else
{
sum+=tmp;
cnt++;
}
}
if(cnt == 1)
printf("The average of 1 number is %.2f", sum);
else if(cnt>1)
printf("The average of %d numbers is %.2f", cnt, sum/cnt);
else
printf("The average of 0 numbers is Undefined");
return 0;
}
知识点小结
- sscanf
- 格式化字符串输入
-
int sscanf ( char * str, const char * format, ...);
/* sscanf example */
#include <stdio.h>
int main ()
{
char sentence []="Rudolph is 12 years old";
char str [20];
int i;
sscanf (sentence,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);
return 0;
}
Output:
Rudolph -> 12
- 代码示例
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[500];
char ss[200] = "123456 ";
sscanf(ss, "%s", str);
cout << "1: " << str << "-" << endl;
// 取指定长度的字符串。取最大长度为4的字符串
sscanf(ss, "%4s", str);
cout << "2: " << str << endl;
// 取到指定字符为止的字符串。取遇到空格为止字符串
strcpy(ss, "123456 abcdedf");
sscanf(ss, "%[^ ]", str);
cout << "3: " << str << endl;
strcpy(ss, "123456abcdedfBCDEF");
sscanf(ss, "%[1-9a-z]", str);
cout << "4: " << str << endl;
sscanf(ss, "%*[0-9]%[a-z]", str);
cout << "5: " << str << endl;
sscanf(ss, "%*[0-9]%*[a-z]%[A-Z]", str);
cout << "5`: " << str << endl;
int a, b, c;
sscanf("2006:03:18", "%d:%d:%d", &a, &b, &c);
cout << "6: a: " << a << " b: " << b << " c: " << c << endl;
const char* s = "iios/12DDWDFF@122";
char buf[20];
sscanf(s, "%*[^/]/%[^@]", buf );
cout << "7: s: " << s << " new s(buf): " << buf << endl;
strcpy(ss, "hello, world");
//%*s表示第一个匹配到的%s被过滤掉(hello)
sscanf(ss, "%*s%s", buf);
cout << "8: (buf): " << buf << endl;
// 0x 16进制
int i;
unsigned int j;
char input[]="10 0x1b aaaaaaaa bbbbbbbb";
char tss[5], tss2[5];
sscanf(input,"%d %x %5[a-z] %*s %f", &i, &j, tss, tss2);
printf("9: %d %d %s %s\n",i,j,tss,tss2);
return 0;
}
- sprintf
- 字串格式化命令,主要功能是把格式化的数据写入某个字符串
-
int sprintf ( char * str, const char * format, ... );
-
返回值:字符串长度(strlen)
/* sprintf example */
#include <stdio.h>
int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char long string\n",buffer,n);
return 0;
}
Output:
[5 plus 3 is 8] is a 13 char long
- 代码示例
#include <ctime>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s[200];
//把整数123 打印成一个字符串保存在s 中。
sprintf(s, "%d", 123);
cout << "1: " << s <<endl;
//产生"123"可以指定宽度,不足的左边补空格:
sprintf(s, "%4d-%4d-", 123, 4567);
cout << "2: " << s <<endl;
//产生:" 123 4567"当然也可以左对齐:
sprintf(s, "%04d-%4d-", 123, 4567);
cout << "3: " << s <<endl;
//小写16进制,宽度占8个位置,右对齐
sprintf(s, "%8x", 27);
cout << "4: " << s <<endl;
//大写16进制,宽度占8个位置,左对齐
sprintf(s, "%-8X", 27);
cout << "5: " << s <<endl;
sprintf(s, "%f", 3.1415926);
cout << "6: " << s <<endl;
sprintf(s, "%.2f", 3.1415926);
cout << "7: " << s <<endl;
// "%m.nf"格式,m表示打印宽度,n表示小数点后位数
sprintf(s, "%10.3f", 3.1415626);
cout << "8: " << s << "-" <<endl;
sprintf(s, "%-10.3f", 3.1415626);
cout << "9: " << s << "-" <<endl;
double tmp = 0;
sscanf("aaa", "%lf", &tmp);
cout << "10(aaa): -" << tmp << "-" <<endl;
sscanf("-3.2123123", "%lf", &tmp);
cout << "11(-3.2): " << tmp << "-" <<endl;
sprintf(s, "%.3f", tmp);
cout << "11`: " << s << "-" <<endl;
// 基于当前系统的当前日期/时间
time_t t = time(0);
// 产生"YYYY-MM-DD hh:mm:ss"格式的字符串。
char snew[32];
strftime(snew, sizeof(snew), "%Y-%m-%d %H:%M:%S", localtime(&t));
cout << "12: -" << snew << " : " << asctime(localtime(&t));
return 0;
}
参考文章
- C++字符串格式化 sprintf、printf