算价格01
Posted 大硕硕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算价格01相关的知识,希望对你有一定的参考价值。
题目:
输入购买图书的数量,根据购买图书的数量,有不同的优惠,计算出购买这些数量的图书需要花多少钱?
设计思想:
定义变量number为购买的图书数量,将number除以10取余数,用switch语句根据余数选择10以内的图书的价格price_part,再根据(number-yushu)/ 10 + price_part 计算总价格。
程序源代码如下:
#include<iostream> #include <windows.h> using namespace std; void main() { int number=0; double price=0; //总的图书最低价格 double price_part=0; //当图书数目除以10后,余数数量的图书所需要的价格 int yushu=0; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);
//这句话是设置字体颜色红色 cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
//这句话是设置字体颜色绿色 cout <<"Please input the number of book:"; cin>>number; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; yushu=number%10; switch(yushu) { case 0: price_part=0; break; case 1: price_part=8; break; case 2: price_part=15.2; break; case 3: price_part=21.6; break; case 4: price_part=25.6; break; case 5: price_part=30; break; case 6: price_part=43.2; break; case 7: price_part=47.2; break; case 8: price_part=51.2; break; case 9: price_part=55.6; break; } cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; price= ( number - yushu ) / 10 * 60 + price_part; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN); cout<<"买"<<number<<"本书的最低价格是 :"<<price<<" !"<<endl; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
//又把字体换成白色 }
运行结果:
个人总结:
今天的算法经过老师的提示实现起来比较简单,因此百度了一下怎么改变字体的颜色。做了最简单的加工。
以上是关于算价格01的主要内容,如果未能解决你的问题,请参考以下文章