Contest3897 - 计科23级算法设计与分析上机作业-01
题目链接
A.判断一个数能否被3,5,7整除
题面

思路
直接模拟即可
示例代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'
const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n;
cin >> n;
if(n % 3 == 0 && n % 5 == 0 && n % 7 == 0) cout << "3 5 7" << '\n';
else if(n % 3 == 0 && n % 5 == 0) cout << "3 5" << '\n';
else if(n % 3 == 0 && n % 7 == 0) cout << "3 7" << '\n';
else if(n % 5 == 0 && n % 7 == 0) cout << "5 7" << '\n';
else if(n % 3 == 0) cout << "3" << '\n';
else if(n % 5 == 0) cout << "5" << '\n';
else if(n % 7 == 0) cout << "7" << '\n';
else cout << 'n' << '\n';
return 0;
}
B.判断是否构成回文
题面

思路
双指针直接模拟即可
示例代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'
const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
string s;
cin >> s;
int i = 0, j = s.size() - 2;
while(i < j){
if(s[i] != s[j]){cout << "No\n"; return 0;}
i++;
j--;
}
cout << "Yes\n";
return 0;
}
C.求最大公约数
题面

思路
辗转相除法直接模拟即可
示例代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'
const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
void solve() {
int a, b;
cin >> a >> b;
cout << gcd(a, b) << '\n';
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int T = 1;
cin >> T;
while(T--) solve();
return 0;
}
D.HTML新手 - 图片收集者
题面

思路
用getline读取每一行字符串并转化成stringstream流,检测line的开头是否为<img,是的话后面就有一个图片地址,在后面再检测word前几位字符是否为src=,再提取图片地址,最后合并输出即可。但是不知道为啥WA了。
示例代码
#include<bits/stdc++.h>
#include<sstream>
using namespace std;
#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'
const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int cnt = 0;
vector<string> res;
string line;
while(getline(cin, line)){
stringstream ss(line);
string word;
bool ok = false;
while(ss >> word){
if(word == "<img"){
cnt++;
ok = true;
}
else if(ok == false) break;
else if(word.size() >= 5 && word.substr(0, 4) == "src=")
{res.push_back(word.substr(5, word.size() - 6));break;}
}
ss.clear();
}
cout << cnt << '\n';
for(auto x : res) cout << x << '\n';
return 0;
}
E.平方和
题面

思路
直接模拟即可
示例代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)
#define dbg(x) cout << #x << ' ' << char(61) << ' ' << x << '\n'
const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
const int inf = 1e9;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
ll ans = 0;
fer(i, 1, 2020){
int j = i;
bool f = false;
while(i){
if(i % 10 == 2 || i % 10 == 0 || i % 10 == 1 || i % 10 == 9)
{f = true; break;}
i /= 10;
}
if(f) ans += j * j;
i = j;
}
cout << ans << '\n';
return 0;
}

浙公网安备 33010602011771号