acm社团题解
这篇是C++解法,需要纯c解法移步这个文章--https://www.cnblogs.com/Arbritry/p/19065887
(但是我还是建议学习c++,竞赛中纯c写题还是很麻烦的)
P1001 A+B Problem https://www.luogu.com.cn/problem/P1001
点击查看代码
#include<cstring>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b;
c=a+b;
cout<<c;
return 0;
}
P1046 [NOIP 2005 普及组] 陶陶摘苹果 https://www.luogu.com.cn/problem/P1046
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int a[10];
for(int i=0;i<10;i++)cin>>a[i];
int n;
cin>>n;
n+=30;
int ans=0;
for(int i=0;i<10;i++){
if(a[i]<=n)ans++;
}
cout<<ans;
return 0;
}
/*有人可能想ios::sync_with_stdio(false);cin.tie(0);这个是干嘛的,
这个就是关缓冲流,不关的话cin这个输入是比scanf这个慢的,
功能性的代码,删了也不影响,只影响一点速度
*/
P3954 [NOIP 2017 普及组] 成绩 https://www.luogu.com.cn/problem/P3954
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main(){
long a,b,c,d;
cin>>b>>c>>d;
b=b*0.2;
c=c*0.3;
d=d*0.5;
a=b+c+d;
cout<<a;
return 0;
}
P5711 【深基3.例3】闰年判断 https://www.luogu.com.cn/problem/P5711
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
if ( (n % 4 == 0 && n % 100 != 0) || n % 400 == 0) cout << 1;
else cout << 0;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
//主要代码在void solve里面,就看这个就可以了
P1089 津津的储蓄计划 https://www.luogu.com.cn/problem/P1089
点击查看代码
#include<iostream>
#include<cstring>
#include<cstdio>
int c,s=0,h=0;
int main(){
for(int i=1;i<=12;i++){
scanf("%d",&c);
s-=c-300;
if(s<0){
printf("-%d",i);
return 0;
}
else h+=s/100,s%=100;
}
printf("%d",120*h+s);
return 0;
}
P5718 【深基4.例2】找最小值 https://www.luogu.com.cn/problem/P5718
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ref(i,n,x) for(int i=x;i>=n;i--)
#define fo(i, n, x) for (int i = x; i <= n; i++)
int mod = 1e6 + 7;
void solve() {
int n;
cin>>n;
int minn=(1<<30);
fo(i,n,1){
int x;
cin>>x;
minn=min(x,minn);
}
cout<<minn;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
P5726 【深基4.习9】打分https://www.luogu.com.cn/problem/P5726
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ref(i,n,x) for(int i=x;i>=n;i--)
#define fo(i, n, x) for (int i = x; i <= n; i++)
void solve() {
int n;
cin>>n;
vector<int>a(n+1);
fo(i,n,1)cin>>a[i];
long double ans=0;
sort(a.begin()+1,a.end());
fo(i,n-1,2)ans+=a[i];
cout << fixed << setprecision(2) << (double)ans/(n-2) << endl;
}
//fixed << setprecision(2)固定格式,输出2位小数
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
P1308 [NOIP 2011 普及组] 统计单词数https://www.luogu.com.cn/problem/P1308
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ref(i,n,x) for(int i=x;i>=n;i--)
#define fo(i, n, x) for (int i = x; i <= n; i++)
void solve() {
string word, s;
cin >> word;
cin.ignore();
getline(cin, s);
//tolower是把大写字母换成小写字母的函数,可以记也可以不记,直接用ascii码转换也可以
for(int i=0; i<word.length(); i++) word[i] = tolower(word[i]);
for(int i=0; i<s.length(); i++) s[i] = tolower(s[i]);
word = " " + word + " ";//防溢出
s = " " + s + " ";
int cnt = 0;
int ans = -1;
size_t pos = s.find(word);
while(pos != string::npos){
/*s 是一个字符串(std::string),word 是要查找的子字符串。
string::find(word) 会在 s 中查找 word 第一次出现的位置,返回值 pos 是该位置的索引(从 0 开始)。
如果 word 不存在于 s 中,find 会返回 string::npos(一个特殊值,表示 “未找到”
*/
cnt++;
if(ans == -1){
ans = pos;
}
pos = s.find(word, pos + 1);
}
if(ans == -1){
cout << -1 << endl;
} else {
cout << cnt << " " << ans << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
P5733 【深基6.例1】自动修正 https://www.luogu.com.cn/problem/P5733
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
string str,ans;
cin>>str;
for(char i:str){
if(i>='a'&&i<='z')
ans+=i-32;
else ans+=i;
}
cout<<ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
P3156 【深基15.例1】询问学号 https://www.luogu.com.cn/problem/P3156
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ref(i,n,x) for(int i=x;i>=n;i--)
#define fo(i, n, x) for (int i = x; i <= n; i++)
void solve() {
int n,m;
cin>>n>>m;
vector<int>a(n+1);
fo(i,n,1)cin>>a[i];
fo(i,m,1){
int x;
cin>>x;
cout<<a[x]<<endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
P3613 【深基15.例2】寄包柜 https://www.luogu.com.cn/problem/P3613
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ref(i,n,x) for(int i=x;i>=n;i--)
#define fo(i, n, x) for (int i = x; i <= n; i++)
void solve() {
map<int,map<int,int>>arr;
/*外层 map 的键是第一个维度坐标 i,值是一个内层 map。
内层 map 的键是第二个维度坐标 j,值是存储的实际数据 k
但是因为这题的原因,所以我们可以当a[i][j]来使用
*/
int n,q;
cin>>n>>q;
fo(xx,q,1){
int pd;
int i,j,k;
cin>>pd;
if(pd==1){
cin>>i>>j>>k;
arr[i][j]=k;
}
else {
cin>>i>>j;
cout<<arr[i][j]<<endl;
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}
P2670 [NOIP 2015 普及组] 扫雷游戏 https://www.luogu.com.cn/problem/P2670
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ref(i,n,x) for(int i=x;i>=n;i--)
#define fo(i, n, x) for (int i = x; i <= n; i++)
// 修正方向数组:8个方向为上、下、左、右、左上、右上、左下、右下
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; // 行方向偏移
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; // 列方向偏移
void solve() {
int n,m;
cin>>n>>m;
vector<vector<char>>a(n+1,vector<char>(m+1));
fo(i,n,1)
fo(j,m,1)
cin>>a[i][j];
vector<vector<char>>ans(n+1,vector<char>(m+1));
auto inmap=[&](int x,int y){ // 判断坐标是否在网格内
return x>=1 && x<=n && y>=1 && y<=m;
};
fo(i,n,1){
fo(j,m,1){
if(a[i][j]=='*'){ // 地雷格直接输出*
ans[i][j]='*';
} else { // 非地雷格计算周围地雷数
int cnt = 0;
for(int k=0;k<8;k++){ // 检查8个相邻方向
int xx = dx[k] + i;
int yy = dy[k] + j;
if(inmap(xx,yy) && a[xx][yy] == '*'){
cnt++;
}
}
ans[i][j] = '0' + cnt; // 转换为数字字符
}
}
}
fo(i,n,1){
fo(j,m,1){
cout<<ans[i][j];
}
cout<<endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin >> t;
while (t--) solve();
return 0;
}

浙公网安备 33010602011771号