user-0 提问: 求问个算法问题

回复:
user: user-1
content: 仅供参考：  #include <stdio.h> #include <string.h> int day_credit(int day_sum) { 	if (day_sum <= 3) 		return 1; 	else if (day_sum <= 7) 		return 2; 	else if (day_sum <= 15) 		return 3; 	else 		return 4; } int credits(char *signs, int days) { 	//积分 	int credit = 0; 	//连续签到天数 	int day_sum = 0; 	int i; 	for (i = 0; i < days; i++) 	{ 		if (signs[i] == '1')  //第i天签到 		{ 			day_sum++;    //累计签到天数加一 			credit += day_credit(day_sum); 		} 		else  //第i天未签到 		{ 			day_sum = 0;    //累计签到天数置0 		} 	} 	return credit; } int main(void) { 	char *signs = malloc(10000001); 	memset(signs, 0, 10000001); 	scanf("%s", signs); 	//总天数 	int days = strlen(signs);  	int credit = credits(signs, days); 	printf("credits=%d\n", credit);  	//如果全部签到 	memset(signs, '1', days); 	credit = credits(signs, days); 	printf("全部签到credits=%d\n", credit); 	free(signs);  	return 0; }  //10001110001 //credits=5 //11111111111 //全部签到credits=23

user: user-2
content: 仅供参考：#include <stdio.h> #include <string.h> #define MAXLEN 1000 char ln[MAXLEN]; FILE *f; int i,z; int b,n,L; int main(int argc,char **argv) {     if (argc<2) {         printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);         return 1;     }     f=fopen(argv[1],"r");     if (NULL==f) {         printf("Can not open file [%s]!\n",argv[1]);         return 2;     }     z=0;     b=0;     n=0;     L=0;     while (1) {         if (NULL==fgets(ln,MAXLEN,f)) break;         L=strlen(ln);         if ('\n'==ln[L-1]) {             if (0==z) {                 for (i=0;i<L-1;i++) {                     if (!(' '==ln[i] || '\t'==ln[i])) break;                 }                 if (i<L-1) z=1;//当前行不是空行             }             if (0==z) b++; else n++;             z=0;         } else {             if (0==z) {                 for (i=0;i<L;i++) {                     if (!(' '==ln[i] || '\t'==ln[i])) break;                 }                 if (i<L) z=1;//当前行不是空行             }         }     }     fclose(f);     if (L>0 && '\n'!=ln[L-1]) {         if (0==z) b++; else n++;//最后一行末尾无'\n'也计算     }     printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);     return 0; }

user: user-2
content: 10000000天/365天/年≈27397年！ 出题者。

user: user-2
content: 读不倒→读不到（暴露了我之前常输入不倒这个词！）

user: user-2
content: 读不倒10MB的文件会超时？

user: user-1
content: 题目的限时多少？

user: user-0
content: 引用 2 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> #include <string.h> #define MAXLEN 1000 char ln[MAXLEN]; FILE *f; int i,z; int b,n,L; int main(int argc,char **argv) {     if (argc<2) {         printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);         return 1;     }     f=fopen(argv[1],"r");     if (NULL==f) {         printf("Can not open file [%s]!\n",argv[1]);         return 2;     }     z=0;     b=0;     n=0;     L=0;     while (1) {         if (NULL==fgets(ln,MAXLEN,f)) break;         L=strlen(ln);         if ('\n'==ln[L-1]) {             if (0==z) {                 for (i=0;i<L-1;i++) {                     if (!(' '==ln[i] || '\t'==ln[i])) break;                 }                 if (i<L-1) z=1;//当前行不是空行             }             if (0==z) b++; else n++;             z=0;         } else {             if (0==z) {                 for (i=0;i<L;i++) {                     if (!(' '==ln[i] || '\t'==ln[i])) break;                 }                 if (i<L) z=1;//当前行不是空行             }         }     }     fclose(f);     if (L>0 && '\n'!=ln[L-1]) {         if (0==z) b++; else n++;//最后一行末尾无'\n'也计算     }     printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);     return 0; }  我还是没理解这个的算法，1000w位每个都要读一遍的话很容易就会超时的！

user: user-0
content: 引用 5 楼 zhangxiangDavaid 的回复:[quote=引用 4 楼 my_bkb 的回复:] [quote=引用 3 楼 zhangxiangDavaid 的回复:] 仅供参考：  #include <stdio.h> #include <string.h> int day_credit(int day_sum) { 	if (day_sum <= 3) 		return 1; 	else if (day_sum <= 7) 		return 2; 	else if (day_sum <= 15) 		return 3; 	else 		return 4; } int credits(char *signs, int days) { 	//积分 	int credit = 0; 	//连续签到天数 	int day_sum = 0; 	int i; 	for (i = 0; i < days; i++) 	{ 		if (signs[i] == '1')  //第i天签到 		{ 			day_sum++;    //累计签到天数加一 			credit += day_credit(day_sum); 		} 		else  //第i天未签到 		{ 			day_sum = 0;    //累计签到天数置0 		} 	} 	return credit; } int main(void) { 	char *signs = malloc(10000001); 	memset(signs, 0, 10000001); 	scanf("%s", signs); 	//总天数 	int days = strlen(signs);  	int credit = credits(signs, days); 	printf("credits=%d\n", credit);  	//如果全部签到 	memset(signs, '1', days); 	credit = credits(signs, days); 	printf("全部签到credits=%d\n", credit); 	free(signs);  	return 0; }  //10001110001 //credits=5 //11111111111 //全部签到credits=23 请问下 这句是什么意思char *signs = malloc(10000001);[/quote]  字符串的最大位数是1000W位，再加上最后的结束符'\0'，共10000001位，故分配这么多字节数[/quote] 我还是没理解这个的算法，1000w位每个都要读一遍的话很容易就会超时的！

user: user-3
content: 为啥非要分配这么多数据？一个char足以使用。  //积分     int credit = 0;     //连续签到天数     int day_sum = 0; char c; int count = 0; while(scanf("%c", &c) != EOF) {     if (c == '1') { //处理逻辑          day_sum++;    //累计签到天数加一             credit += day_credit(day_sum);     } else if (c == '0') {       day_sum = 0;    //累计签到天数置0     } else {         break;     }     count++; } printf("credits=%d\n", credit);  if (count > 15) { credit=3*1 +4*2+8*3+(count-15)*4; } else if (count>7) { credit=3*1 +4*2+(count-7)*3; } else if (count > 3){ credit=3*1 +(count-3)*2; } else { credit=count; }  printf("全部签到credits=%d\n", credit);    代码抄点楼上的。

user: user-1
content: 答得好，记得给分哦

user: user-1
content: 引用 4 楼 my_bkb 的回复:[quote=引用 3 楼 zhangxiangDavaid 的回复:] 仅供参考：  #include <stdio.h> #include <string.h> int day_credit(int day_sum) { 	if (day_sum <= 3) 		return 1; 	else if (day_sum <= 7) 		return 2; 	else if (day_sum <= 15) 		return 3; 	else 		return 4; } int credits(char *signs, int days) { 	//积分 	int credit = 0; 	//连续签到天数 	int day_sum = 0; 	int i; 	for (i = 0; i < days; i++) 	{ 		if (signs[i] == '1')  //第i天签到 		{ 			day_sum++;    //累计签到天数加一 			credit += day_credit(day_sum); 		} 		else  //第i天未签到 		{ 			day_sum = 0;    //累计签到天数置0 		} 	} 	return credit; } int main(void) { 	char *signs = malloc(10000001); 	memset(signs, 0, 10000001); 	scanf("%s", signs); 	//总天数 	int days = strlen(signs);  	int credit = credits(signs, days); 	printf("credits=%d\n", credit);  	//如果全部签到 	memset(signs, '1', days); 	credit = credits(signs, days); 	printf("全部签到credits=%d\n", credit); 	free(signs);  	return 0; }  //10001110001 //credits=5 //11111111111 //全部签到credits=23 请问下 这句是什么意思char *signs = malloc(10000001);[/quote]  字符串的最大位数是1000W位，再加上最后的结束符'\0'，共10000001位，故分配这么多字节数

user: user-0
content: 引用 3 楼 zhangxiangDavaid 的回复:仅供参考：  #include <stdio.h> #include <string.h> int day_credit(int day_sum) { 	if (day_sum <= 3) 		return 1; 	else if (day_sum <= 7) 		return 2; 	else if (day_sum <= 15) 		return 3; 	else 		return 4; } int credits(char *signs, int days) { 	//积分 	int credit = 0; 	//连续签到天数 	int day_sum = 0; 	int i; 	for (i = 0; i < days; i++) 	{ 		if (signs[i] == '1')  //第i天签到 		{ 			day_sum++;    //累计签到天数加一 			credit += day_credit(day_sum); 		} 		else  //第i天未签到 		{ 			day_sum = 0;    //累计签到天数置0 		} 	} 	return credit; } int main(void) { 	char *signs = malloc(10000001); 	memset(signs, 0, 10000001); 	scanf("%s", signs); 	//总天数 	int days = strlen(signs);  	int credit = credits(signs, days); 	printf("credits=%d\n", credit);  	//如果全部签到 	memset(signs, '1', days); 	credit = credits(signs, days); 	printf("全部签到credits=%d\n", credit); 	free(signs);  	return 0; }  //10001110001 //credits=5 //11111111111 //全部签到credits=23 请问下 这句是什么意思char *signs = malloc(10000001);