蓝桥杯/上机题目(第二周) 打卡Day1
题目1 AcWing3389. N 的阶乘
题目2 AcWing3448. 基本算术
题目3 AcWing3453. 整数查询
1.AcWing3389. N 的阶乘
思路: 高精度乘法复习题,注意一下预处理打表即可
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int N = 1010;
vector<int> f[N];
vector<int> mul(vector<int>& temp, int b)
{
vector<int>res;
int t = 0;
for(int i = 0; i < temp.size() || t; i ++){
if(i < temp.size()) t += temp[i] * b;
res.push_back(t % 10);
t /= 10;
}
while(res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
int main()
{
int n;
f[0] = {1}, f[1] = {1};
for(int i = 2; i <= N; i ++){
f[i] = mul(f[i -1] , i);
}
while(cin >> n){
auto temp = f[n];
for(int i = temp.size() - 1; i >= 0; i --) cout << temp[i];
cout << endl;
}
return 0;
}
2.AcWing3448. 基本算术
思路 :高精度加法复习题
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int add(vector<int> temp1, vector<int>temp2){
int t = 0;
int s = 0;
vector<int> res;
for(int i = 0; i < temp1.size() || i < temp2.size(); i ++){
if(i < temp1.size()) t += temp1[i];
if(i < temp2.size()) t += temp2[i];
if(t >= 10) s ++;
res.push_back(t % 10);
t /= 10;
}
if(t){
res.push_back(1);
}
return s;
}
int main()
{
while(true){
vector<int>a ,b;
string s1, s2;
cin >> s1 >> s2;
if(s1 == "0" && s2 == "0") break;
for(int i = s1.size() - 1; i >= 0; i --) a.push_back(s1[i] - '0');
for(int i = s2.size() - 1; i >= 0; i --) b.push_back(s2[i] - '0');
int res = add(a,b);
if(res > 1){
printf("%d carry operations.\n",res);
}else if(res == 1){
printf("%d carry operation.\n",res);
}else{
puts("No carry operation.");
}
}
return 0;
}
3.AcWing3453. 整数查询
思路:高精度加法复习题
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
vector<int>add(vector<int>temp1, vector<int>temp2){
int t = 0;
vector<int>res;
for(int i = 0; i < temp1.size()|| i < temp2.size(); i ++){
if(i < temp1.size()) t += temp1[i];
if(i < temp2.size()) t += temp2[i];
res.push_back(t % 10);
t /= 10;
}
if(t) res.push_back(1);
return res;
}
int main()
{
vector<int>res = {0};
queue<vector<int>>q;
string temp;
while(cin >> temp, temp !="0"){
vector<int> b;
for(int i = temp.size() - 1; i >= 0; i -- ) b.push_back(temp[i] - '0');
q.push(b);
}
while(q.size()){
auto t = q.front();
q.pop();
res = add(t,res);
}
while(res.size() > 1 && res.back() == 0) res.pop_back();
for(int i = res.size() - 1; i >= 0; i --) cout << res[i];
cout << endl;
return 0;
}