user-0 提问: 奇怪的数组题

回复:
user: user-0
content: 引用 6 楼 bo_self_effacing的回复:#include <iostream> #include <math.h>  double cal_fun(double); double make_double_int(double);  static const int RATE_NUMS = 3; static const double amounts[RATE_NUMS] = {0,5000,10000}; static const float rate[RATE_NUMS] = {0.08,0.10,0.12}; static const double limit[RATE_NUMS] = {0,5000*0.08,5000*0.08+(10000-5000)*0.10}; static const double SLARY = 5000;  using std::cout; using std::endl;  int main() {  	unsigned int nums = 0; 	while(!nums) 		scanf("%d",&nums); 	double results[nums]; 	double param = 0; 	int i; 	for(i = 0;i < nums;i++) 	{ 		scanf("%lf",¶m); 		results[i] = cal_fun(param); 	} 	for(i = 0;i < nums;i++) 	{ 		cout<<results[i]<<endl; 	} 	return 0; }  double cal_fun(double n) { 	if(n <= SLARY) 		return 0; 	double real_n = n - SLARY; 	double ret(0); 	for(int i = RATE_NUMS - 1; i >= 0; i--) 	{ 		if(real_n > limit[i]) 		{ 			ret += (real_n - limit[i])/rate[i]; 			/*printf("real_n: %f, i: %d, amounts[i]: %f, rate[i]: %f, limit[i]: %f, ret: %f\n", 					real_n,i,amounts[i],rate[i],limit[i],ret);*/ 			real_n = limit[i]; 		} 	} 	return make_double_int(ret + 0.5); }  double make_double_int(double src) { 	double bitnum = fmod(src,10); 	//int int_bitnum = bitnum + 0.5; //四舍五入 	int int_bitnum = bitnum; //去尾 	return src - bitnum + int_bitnum; }  不错c++很漂亮但我其实没怎么看懂。。

user: user-0
content: 引用 5 楼 前行者001的回复:/*多谢楼主提醒      程序已更正*/  #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 /* run this program using the console pauser or add your own getch, system("pause") or input loop */  float Fun(float price) { 	float sum = 0.0;  	price = price - 5000;  	if(price < 5000*0.08) 		sum += price/0.08; 	else if(price >= 5000*0.08 && price <= 5000*0.08 + 5000*0.10) 	{ 		sum += 5000; 		sum += (price-5000*0.08)/0.10; 	} 	else 	{ 		sum = sum + 5000; 		sum = sum + 5000; 		sum = sum + (price - 5000*0.08 - 5000*0.10)/0.12;  	} 	return sum; }    int main() {  	printf("请输入想要获得的佣金金额\n"); 	int n = 0; 	float price[MAXSIZE] = {0.0};   	scanf("%d",&n);  	for(int i = 0;i < n;i++)  	scanf("%f",&price[i]);  	for(int i = 0;i < n;i++)  	price[i] = Fun(price[i]);  	for(int i = 0;i < n;i++)  	printf("%.d ",(int)price[i]); 	return 0; }  运行结果  嗯对了float改为double更妙而且最后不用进行类型转换吧改成%.0lf就可以了

user: user-1
content: #include <iostream> #include <math.h>  double cal_fun(double); double make_double_int(double);  static const int RATE_NUMS = 3; static const double amounts[RATE_NUMS] = {0,5000,10000}; static const float rate[RATE_NUMS] = {0.08,0.10,0.12}; static const double limit[RATE_NUMS] = {0,5000*0.08,5000*0.08+(10000-5000)*0.10}; static const double SLARY = 5000;  using std::cout; using std::endl;  int main() {  	unsigned int nums = 0; 	while(!nums) 		scanf("%d",&nums); 	double results[nums]; 	double param = 0; 	int i; 	for(i = 0;i < nums;i++) 	{ 		scanf("%lf",¶m); 		results[i] = cal_fun(param); 	} 	for(i = 0;i < nums;i++) 	{ 		cout<<results[i]<<endl; 	} 	return 0; }  double cal_fun(double n) { 	if(n <= SLARY) 		return 0; 	double real_n = n - SLARY; 	double ret(0); 	for(int i = RATE_NUMS - 1; i >= 0; i--) 	{ 		if(real_n > limit[i]) 		{ 			ret += (real_n - limit[i])/rate[i]; 			/*printf("real_n: %f, i: %d, amounts[i]: %f, rate[i]: %f, limit[i]: %f, ret: %f\n", 					real_n,i,amounts[i],rate[i],limit[i],ret);*/ 			real_n = limit[i]; 		} 	} 	return make_double_int(ret + 0.5); }  double make_double_int(double src) { 	double bitnum = fmod(src,10); 	//int int_bitnum = bitnum + 0.5; //四舍五入 	int int_bitnum = bitnum; //去尾 	return src - bitnum + int_bitnum; }

user: user-2
content: /*多谢楼主提醒      程序已更正*/  #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 /* run this program using the console pauser or add your own getch, system("pause") or input loop */  float Fun(float price) { 	float sum = 0.0;  	price = price - 5000;  	if(price < 5000*0.08) 		sum += price/0.08; 	else if(price >= 5000*0.08 && price <= 5000*0.08 + 5000*0.10) 	{ 		sum += 5000; 		sum += (price-5000*0.08)/0.10; 	} 	else 	{ 		sum = sum + 5000; 		sum = sum + 5000; 		sum = sum + (price - 5000*0.08 - 5000*0.10)/0.12;  	} 	return sum; }    int main() {  	printf("请输入想要获得的佣金金额\n"); 	int n = 0; 	float price[MAXSIZE] = {0.0};   	scanf("%d",&n);  	for(int i = 0;i < n;i++)  	scanf("%f",&price[i]);  	for(int i = 0;i < n;i++)  	price[i] = Fun(price[i]);  	for(int i = 0;i < n;i++)  	printf("%.d ",(int)price[i]); 	return 0; }  运行结果

user: user-0
content: 引用 3 楼 前行者001的回复:你好，题中给出的测试数据好像有点小问题。  (460833-10000)*0.12 +5000*0.10 + 5000*0.08 = 55000 != 60000; （210833 - 10000）*0.12 + 5000*0.10+5000*0.08 = 25000 ！=30000 不知道是不是我的理解有问题  #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 /* run this program using the console pauser or add your own getch, system("pause") or input loop */  float Fun(float price) { 	float sum = 0.0;  	if(price < 5000*0.08) 		sum += price/0.08; 	else if(price >= 5000*0.08 && price <= 5000*0.08 + 5000*0.10) 	{ 		sum += 5000; 		sum += (price-5000*0.08)/0.10; 	} 	else 	{ 		sum = sum + 5000; 		sum = sum + 5000; 		sum = sum + (price - 400 - 500)/0.12;  	} 	return sum; }    int main() {  	printf("请输入想要获得的佣金金额\n"); 	int n = 0; 	float price[MAXSIZE] = {0.0};   	scanf("%d",&n);  	for(int i = 0;i < n;i++)  	scanf("%f",&price[i]);  	for(int i = 0;i < n;i++)  	price[i] = Fun(price[i]);  	for(int i = 0;i < n;i++)  	printf("%.d ",(int)price[i]); 	return 0; }   还有个基本工资5000啊

user: user-0
content: 引用 1 楼 user-3的回复:一种是用静态数组 #define N 300 double res[N]; int main () {     int n;     scanf(“%d”, &n); //这里只要输入n不大于300即可     for (int k=0; k<n; n++) {         //res[k]=xxx     }     return 0; }  一种是用动态数组 int main () {     int n;     scanf(“%d”, &n); //这里只要输入n不大于300即可     double *res = (double *)malloc(sizeof(double)*n); //动态申请内存     for (int k=0; k<n; n++) {         //res[k]=xxx     }     free res; //释放内存     return 0; } 大佬你看题目了么。

user: user-3
content: 一种是用静态数组 #define N 300 double res[N]; int main () {     int n;     scanf(“%d”, &n); //这里只要输入n不大于300即可     for (int k=0; k<n; n++) {         //res[k]=xxx     }     return 0; }  一种是用动态数组 int main () {     int n;     scanf(“%d”, &n); //这里只要输入n不大于300即可     double *res = (double *)malloc(sizeof(double)*n); //动态申请内存     for (int k=0; k<n; n++) {         //res[k]=xxx     }     free res; //释放内存     return 0; }