编程打卡:C语言趣味编程习题做
数制转换
问题描述
给定一个M进制的数x,实现对x向任意非M进制的数的转换。
设计思路
输入M进制的数x,将x转换为十进制数,再将十进制数转换为任意非M进制的数。
流程图
graph A["开始"] --> B["输入M进制的数x"] -->C["将x转换为十进制数"] --> D["将十进制数转换为任意非M进制的数"] --> E["输出"] --> F["结束"]代码实现
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int num2dec(int m,string num) {
int tmpm = 1;
int sum = 0;
for (int i = num.length()-1; i >= 0; i--) {
if (num[i] >= '0' && num[i] <= '9') {
sum += (num[i] - '0') * tmpm;
tmpm *= m;
}
else if (num[i] >= 'A' && num[i] <= 'F') {
sum += (num[i] - 'A' + 10) * tmpm;
tmpm *= m;
}
else if (num[i] >= 'a' && num[i] <= 'f') {
sum += (num[i] - 'a' + 10) * tmpm;
tmpm *= m;
}
}
return sum;
}
string dec2num(int m,int num) {
string res;
while (num) {
int tmp = num % m;
if (tmp >= 0 && tmp <= 9) {
res += tmp + '0';
}
else {
res += tmp - 10 + 'A';
}
num /= m;
}
reverse(res.begin(),res.end());
return res;
}
int main() {
int m,t;
cin >> m >> t;
string num;
cin >> num;
cout << dec2num(t,num2dec(m,num));
}
运行结果
输入
8 16
1234
输出
29C
输入
16 10
ff
输出
255
个人所得税问题
问题描述
编写程序,计算个人所得税。
设计思路
用数组存储不同工资阶段区间以及个人所得税率,根据输入的金额,循环判断来计算个人所得税
流程图
graph A[开始] --> B[输入工资] --> C{工资>3500}; C -- No --> D[输出个人所得税为0] --> E[结束]; C -- Yes --> F[i = 0] --> G{"工资>个税[i].min?"} -- Yes --> H{"工资<=个税[i].max?"} -- Yes --> I[计算输出个人所得税] --> E; H -- No --> J[i++] --> G;代码实现
#include <iostream>
#include <climits>
using namespace std;
struct zone {
int min;
int max;
double rate;
};
struct zone tax[] = {{0,1500,0.03},{1500,4500,0.10},{4500,9000,0.20},{9000,35000,0.25},{35000,55000,0.30},{55000,80000,0.35},{80000,INT_MAX,0.45}};
int main () {
double payTax = 0,salary;
cin >> salary;
if (salary <= 3500)
cout << 0 << endl;
else {
salary -= 3500;
for (struct zone i : tax) {
cout << salary << " " << i.min << " " << i.max << " " << i.rate << endl;
if (salary > i.max) {
payTax += (i.max - i.min) * i.rate;
}
else {
payTax += (salary - i.min) * i.rate;
break;
}
}
cout << payTax << endl;
}
}
标签:进制,--,编程,int,num,打卡,include,习题
From: https://www.cnblogs.com/sugar-refinery/p/17326674.html