|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前段时间看了算法珠玑这本书受益颇多,最近看c++视频有这么一道题,试着用边界扫描的思想编了一下。 以下是本人代码:
#include "stdio.h"#include "string.h"
#include "ctype.h"
#define MAXSIZE 100
//最大的输入字节数
int main(void){
char str[MAXSIZE];
int sum;
printf("请输入任意数量的空格和数字,对其进行求和\n");
while(gets(str) != NULL){
int i;
int starthere = 0;
int sum = 0;
for(i = 0;i <strlen(str)+1;i++){
if(isspace(str[i]) || (str[i] == '\0') ) { //对于空格进行边界扫描,重新定位
sum +=starthere;
starthere = 0;
continue;
}
else {
starthere = starthere *10 +(str[i] - '0');
//计算两个空格间的数字的大小
}
}
printf("%d.\n",sum);
}
}
|
|