P2670 [NOIP2015 普及组] 扫雷游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P2670
通过答案
//看的很乱---蒟蒻蒟蒻蒟蒻蒟蒻
#include <bits/stdc++.h>
using namespace std;
const int x[8] {1,1,1,0,0,-1,-1,-1};//int ;
const int y[8] {-1,0,1,-1,1,-1,0,1};
char a[105][105];
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]=='*'){
cout<<'*';
}
else{
int cnt=0;
for(int k=0;k<8;k++)
if(a[i+x[k]][j+y[k]]=='*')
cnt++;
cout<<cnt;
}
}
cout<<"\n";
}
return 0;
}
- 错误原因:
- 未初始化 字符数组a[][]; 在全局定义 自动初始化
- const int ...;
- 其实if()放"大部分代码" else放"小部分"更美观
P1303 A*B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
int a[2023],b[2023],c[3000];//数2000,乘积大于
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string A,B;
cin>>A>>B;
int lena=A.length(),lenb=B.length();
for(int i=lena-1;i>=0;i--) a[lena-i]=A[i]-'0';
for(int i=lenb-1;i>=0;i--) b[lenb-i]=B[i]-'0';//倒 从1开始
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++)
c[i+j-1] +=a[i]*b[j];// 从1开始
int len =lena+lenb;
for(int i=1;i<=len;i++){
c[i+1]+=c[i]/10;
c[i]%=10;
}
for(;!c[len];){//for(int i=len;!c[i];i--) 可去掉i;
len--;
}
for(int i=max(1,len);i>=1;i--)//i--....
cout<<c[i];
return 0;
}
错误原因:
- 高精乘范围:乘积范围大
- 字符串转化整形数组 倒过来 防止尾部0情况,0被混入空
P4924 [1007] 魔法少女小Scarlet - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
难点:旋转
算法:模拟
旋转前---> 旋转后
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 | 8 | 9 |
7 | 4 | 1 |
---|---|---|
8 | 5 | 2 |
9 | 6 | 3 |
A行与A列有关系
void spin(int x,int y,int r)
{
for(int i=x-r;i<=x+r;i++)
{
for(int k=y-r;k<=y+r;k++)
temp[i][k]=square[i][k];
}//将以(x,y)为中心的(2r+1)*(2r+1)的矩阵赋给temp
int x1=x+r,y1=y-r;
for(int i=x-r;i<=x+r;i++)
{
for(int k=y-r;k<=y+r;k++)
{
square[i][k]=temp[x1][y1];//将某一列的数字赋值给某一列
x1--;
}
x1=x+r,y1++;
}
}
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
模拟,没啥难的,脑子抽抽了 循环过界用if,还用2个if 没排除全部清况.
就是没想到 : 用%永远过不了界..
#include <bits/stdc++.h>
using namespace std;
#define int long long
bool who_win(int a, int b)
{
if(a==0){
if(b==2||b==3) return true;
else return false;
}else if(a==1){
if(b==0||b==3) return true;
else return false;
}else if(a==2){
if(b==1||b==4) return true;
else return false;
}else if(a==3){
if(b==2||b==4) return true;
else return false;
}else if(a==4){
if(b==0||b==1) return true;
else return false; //暴力,按着表打
}
}
signed main()
{
int A=0,B=0;
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N,alen,blen;
cin>> N>>alen>>blen;
int a[alen],b[blen];
for(int i=0;i<alen;i++) cin>>a[i];
for(int i=0;i<blen;i++) cin>>b[i];//input
for(int i=0,a_now=0,b_now=0;i<N;i++,a_now++,b_now++){
a_now%=alen; //懵了..用a_now=0
b_now%=blen;
/*if(a_now==alen)
a_now=0; // 脑子打折片段,不要学
if(b_now==blen)
b_now=0;
*/
if(a[a_now]==b[b_now]){ //平局 均直接continue,bool非负即胜,不能进
continue;
}
if(who_win(a[a_now],b[b_now])) //true甲++ fasle乙++; bool非负即胜
A++;
else
B++;
}
cout<<A<<" "<<B; //output
return 0;
}