thoughtworksd一道编程题

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

商品税的计算:

国内商品除食物、书籍、药品外均收取10%的基本税收

进口商品要收取除基本税以外的关税

计算的税收以0.05为单位,进行入整

输入1:

一本书 12.49

一本音乐CD 14.99

一条巧克力棒0.85

输出1:

一本书:12.49

一本音乐CD  14.99+14.99*0.1 = 14.99 +1.499 = 14.99+1.5=16.49

一条巧克力棒 0.85

税收:1.5

总价钱:12.49+16.49+0.85=29.83

输入2:

一盒进口巧克力:10

一瓶进口香水:47.50

输出2:

一盒进口巧克力:10+10*0.05=10.5

一瓶进口香水:47.5+47.5*0.15=47.5+7.125=47.5+7.15=54.65

税收:0.5+7.15=7.65

总价:10.5+54.65=65.15

输入3:

一瓶进口香水:27.99

一瓶香水:18.99

一盒感冒药:9.75

一盒进口巧克力:11.25

输出3:

一瓶进口香水:27.99+27.99*0.15=27.99+4.1985=27.99+4.20=32.19

一瓶香水:18.99+18.99*0.1=18.99+1.899=18.99+1.90=20.89

一盒感冒药:9.75

一盒进口巧克力:11.25+11.25*0.05=11.25+0.5625=11.25+0.60=11.85

税收:4.20+1.90+0.60=6.70

总价:74.68

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

实现方法1:

/**********************************************

*文件名:sales_taxes.cpp

*作者:

*联系方式:

*功能说明:

*上次修改:

**********************************************/

#include<iostream>

#define  N 10            //暂定结构体数组的长度为10,可根据实际情况设置

#define  JM 0.05         //进口单不收税

#define  JB 0.15         //进口且收税

#define  BB 0.10         //非井口不免费

typedef struct goods{    //存储商品属性的结构体

double price;

bool   is_import;

bool   is_free;

}goods;

double two_point(double num);

void   sales_taxes_func(goods shoping[N],double& taxes,double& total,int n );

void   output_info(double taxes,double total);

void   input_info();

int main(void)

{

using namespace std;

goods shoping[N];

input_info();

int n=0;    //实现数据的输入,因为价格不能为0,以0为结束标志

{

int cycle=1;

double buffer;

while(cycle){

cin>>buffer;

if(buffer!=0){

shoping[n].price=buffer;

cin>>shoping[n].is_import

>>shoping[n].is_free;

n++;

}

else cycle=0;

}

}

double taxes=0;

double total=0;

sales_taxes_func(shoping,taxes,total,n);

output_info(taxes,total);

system("pause");

return 0;

}

//precisiong two points

double two_point(double num){

double result=0.0;

int temp=static_cast<int>(num*100);     //强制把double转换为int,实现向下取整

int flag=temp%10;

if(0==flag){

result=temp/100.0;

}else if(flag>=5){

temp+=(10-flag);

result=temp/100.0;

}else{

temp+=(5-flag);

result=temp/100.0;

}

return result;

}

//sales taxes count

void  sales_taxes_func(goods shoping[N],double& taxes,double& total,int n ){

for(int i=0;i<n;i++){

if(shoping[i].is_free){

if(!shoping[i].is_import){

total+=shoping[i].price;

}else{

double temp=0.0;

temp=two_point(shoping[i].price*JM);

taxes+=temp;

total+=shoping[i].price+temp;

}

}else{

if(!shoping[i].is_import){

double temp=0.0;

temp=two_point(shoping[i].price*BB);

taxes+=temp;

total+=shoping[i].price+temp;

}else{

double temp=0.0;

temp=two_point(shoping[i].price*JB);

taxes+=temp;

total+=shoping[i].price+temp;

}

}

}

return;

}

//result output

void output_info(double taxes,double total){

using namespace std;

cout<<endl;

cout<<"---------------------------result------------------------------\n";

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

cout<<"taxes: "<<taxes<<endl

<<"total: "<<total<<endl<<endl<<endl;

return;

}

//input infomation

void input_info(){

using namespace std;

cout<<"--------------------------help info-----------------------------\n"

<<"price is two points.\n"

<<"inport is true meaning inport;false is meaning export.\n"

<<"free is true meaing free;false is meaing not free.\n"

<<"-------------------------input info-----------------------------\n"

<<"please input goods(interupt table key,0 is end):\n"

<<"price\t"

<<"import\t"

<<"free\t"

<<endl;

return;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

实现方法2:

/**********************************************

*document name:sales_taxes.cpp

*author:wuyongxian

*contact information:the e-mail is [email protected] and the telephone number is 15191433756

*the function specification:it is able to print out the details which are the total prices and the sales taxes of the shopping goods.

**********************************************/

#include<iostream>

#include<string>

#include<conio.h>

using namespace std;

#define  JM 0.05         // rate of the imported taxes

#define  JB 0.15         // rate of the imported taxes and  the basic taxes

#define  BB 0.10         // rate of the basic taxes

typedef struct goods{    //the structure stores the attribute of the goods

int count;

char *name;

double price;

bool   is_import;

bool   is_free;

struct goods *next;

}goods;

double rounding(double num);

void   sales_taxes_func(goods *head,double& taxes,double& total );

void   print(goods *head);

goods  *input_goodsinformation();

int main()

{

goods *q;

double taxes=0.0;

double total=0.0;

q=input_goodsinformation();

sales_taxes_func(q,taxes,total);

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

print(q);

cout<<"Sales Taxes:"<<taxes<<endl;

cout<<"Total:"<<total<<endl;

system("pause");

return 0;

}

/***************input goods information**********/

goods *input_goodsinformation()

{

goods *p,*s,*head;

int count;

char *name;

double price;

bool import;

bool free;

int cycle=1;

head=(goods*)malloc(sizeof(goods));

p=head;

while(cycle)

{

cout<<"please input count(0 end)\n"<<endl;

name=(char*)malloc(100);

cin >> count;

if(count)

{

cout<<"please input  name (string)\n"<<endl;

cin >> name;

cout<<"please input  price(double)\n"<<endl;

cin>>price;

cout<<"please input  name import(0-made in country,1-imported)\n"<<endl;

cin>>import;

cout<<"please input  free(1-exempt,0- nonexempt)\n"<<endl;

cin>>free;

s=(goods*)malloc(sizeof(goods));

s->count=count;

s->name=name;

s->price=price;

s->is_import=import;

s->is_free=free;

p->next=s;

p=s;

}

else

cycle=0;

}

head=head->next;

p->next=NULL;

return head;

}

/************output goods list**************************/

void print(goods *head)

{

goods *p;

cout<<"*********List of goods **********"<<endl;

p=head;

if(head!=NULL)

while(p!=NULL)

{

cout<<p->count<<" "<<p->name<<":"<<p->price<<endl;

p=p->next;

}

}

/*************precisiong two points*******************/

double rounding(double num){

double result=0.0;

int temp=static_cast<int>(num*100);

int flag=temp%10;

if(0==flag){

result=temp/100.0;

}else if(flag>=5){

temp+=(10-flag);

result=temp/100.0;

}else{

temp+=(5-flag);

result=temp/100.0;

}

return result;//make the price of taxes rounding up to the nearest 0.05

}

/****************Calculation of commodity price and the total tax revenues***********/

void sales_taxes_func(goods *head,double& taxes,double& total )

{

goods *p;

p=head;

double temp1=0.0;

double temp2=0.0;

double temp3=0.0;

if(head!=NULL)

{

while(p!=NULL)

{

if(p->is_free)

{

if(!p->is_import)

{

total+=p->price*p->count;

p=p->next;

}

else

{

temp1=rounding(p->price*JM);

taxes+=temp1*p->count;

total+=p->price*p->count+temp1*p->count;

p=p->next;

}

}

else

{

if(!p->is_import)

{

temp2=rounding(p->price*BB);

taxes+=temp2*p->count;

total+=p->price*p->count+temp2*p->count;

p=p->next;

}

else

{

temp3=rounding(p->price*JB);

taxes+=temp3*p->count;

total+=p->price*p->count+temp3*p->count;

p=p->next;

}

}

}

}

return;

}

时间: 2024-07-31 11:59:03

thoughtworksd一道编程题的相关文章

一道编程题实在是不知道哪个地方错了

求帮忙 一道觉得很简单的编程题,提交了很多次,但是,还是wrong,实在是崩溃啊,真的不知道哪里错了,希望大家可以帮忙解决一下,感激不尽. 就是这道题----Conversions Description Conversion between the metric and English measurement systems is relatively simple. Often, it involves either multiplying or dividing by a constant

Airbnb coding面的一道编程题

之前在直播的时候Airbnb负责人说他们的coding题难度不会到dp,我就知道肯定是一些字符串处理啥的编程题了. 果然,Airbnb的coding面是在codePad上手写代码,面试官希望能看到你书写的过程,所以少用本地ide(呵呵,不调试让写编程题是最XX的,所以有些东西你需要及时沟通) 自己给的三四个test case,要求输出对应的结果就行了(比ACM是相当宽松了). 题目是这样的,很简单: 有这样的文本(我转js的字符串了) let str1 = 'rwer321,dad,adas,y

对一道编程题的后续思考

原题来自<一道整数求值作业> 题目描述:给定一个整数 X,找到组成的数字和 X 完全相同的,且大于 X 的最小的那个数:若不存在这样的数,输出 0 一开始把它想复杂了,后来想想只需将该数组成的数列从后往前枚举,然后判断当前位置往后的数列是否是降序即可,时间复杂度O(n),详细思路跟上文一致可以参考原文. var a = 21544221; // 要求的数字 var s = a.toString(); var len = s.length; for(var i = len - 2; i >

赛码网的一道百度编程题

最近偶尔接触到这个赛码网,看了百度的一道编程题,于是尝试了一下,发现虽然天天写代码实现这个居然花了我好长时间,仍然没有通过全部案例.目前给的通过率是83% 题目如下: 小B最近对电子表格产生了浓厚的兴趣,她觉得电子表格很神奇,功能远比她想象的强大.她正在研究的是单元格的坐标编号,她发现表格单元一般是按列编号的,第1列编号为A,第2列为B,以此类推,第26列为Z.之后是两位字符编号的,第27列编号为AA,第28列为AB,第52列编号为AZ.之后则是三位.四位.五位--字母编号的,规则类似. 表格单

上海华为的一道关于指针方面的编程题(C/C++)

int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序,返回值为原数据中第一个元素为0的下标. 尽可能不使用辅助空间且考虑效率及异常问题,注释规范且给出设计思路 注:我的方法的复杂度为O(n),大家如果有其它方法希望可以交流一下. /* author: jiangxin Blog: http://blog.csdn.net/jiangxinnju */ #include <ios

一道模板元编程题源码解答(replace_type)

今天有一同学在群上聊到一个比较好玩的题目(本人看书不多,后面才知是<C++模板元编程>第二章里面的一道习题), 我也抱着试一试的态度去完成它, 这道题也体现了c++模板元编程的基础和精髓: 类型就是数据. 题目如下所述: Write a ternary metafunction replace_type<c,x,y> that takes an arbitrary compound type c as its first parameter, and replaces all oc

【求助】一道考验脑细胞的编程题

要求计算S的面积.注意:仅计算面积,不区分正负,如果围成的图形被x轴分割为上下两部分,那么就求上下两部分面积之和. 输入多项式fx,以字符串表示,格式为:4*x^5-x^2+5*x+12,多项式表达式不包含括号,可能包含空格.数字.字母x.^.*.+.-,保证多项式最高次幂为非负整数,且最高次幂不超过10. 表达式中4*x^5与4x^5等价,如下面的表达式是合法的: x^10-5x^1 -4*x^1 + 5x^0 2.3x^4 - 2.56*x + 1 输入不会出现下列类型的表达式: x(x+5

一道有意思的C语言编程题

最近在看经典的C语言入门书籍K&R,虽然是一本入门书籍,可是其中的精妙之处却需要慢慢体会.其中的经典题很多,仔细琢磨一定会收获良多. 今天看到这样一道题:编写一个删除C语言程序中所有的注释语句.感觉颇有意思,与大家一起分享一下: 我的思路: 找到注释的起始符号 \ 判断紧接着的输入字符,如果是*或者是\,则说明后面全是注释,跳过即可,否则照样输出 其他则直接输出 疑问: 所配套的答案书中提出要考虑引号后面的内容以做出响应,不是很明白这是为什么.个人认为无需考虑引号的影响也能将注释去除,希望有高手

算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。

只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/5882357.html.看了下,挺有意思,于是就想了想,又写了写,最终撸出来了.刚开始一看顿时感觉很熟悉,大学数据结构和算法课肯定讲过相关东西,什么深度搜索,广度搜索,最优路径,最优解...但是现在你让我说个一二三,我还就只记住几个名字,说不定名字都记错.我向来不喜欢死记东西,能查到的真的不想背下来,而