线性分类器:
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1010,M=30;
int n,m;
int x[N],y[N],s[N];
//在线上方是1,下方是-1.方向*标志。如果是1*1
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++){
char temp;
scanf("%d %d %c",&x[i],&y[i],&temp);
if(temp=='A')s[i]=-1;
else s[i]=1;
}
for(int i=0;i<m;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
bool result = true;
int flag=((a+b*x[0]+c*y[0])>0) ? 1 : (-1);
flag = flag*s[0];
for(int j=1;j<n;j++){
int temp = ((a+b*x[j]+c*y[j])>0) ? 1 : (-1);
temp = temp*s[j];
if(temp!=flag){
result = false;
break;
}
}
if(result)printf("Yes\n");
else printf("No\n");
}
return 0;
}
教训:一定要将测试的printf清理干净
稀疏向量
用c11标准,使用 #include <bits/stdc++.h> using gg = long long; ios::sync_with_stdio(false); cin.tie(0);
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
gg n,a,b,x,y;
gg ans=0;
cin>>n>>a>>b;
unordered_map<gg,gg> um;
while(a--) {
cin>>x>>y;
um[x] = y;
}
while(b--){
cin>>x>>y;
ans+=um[x]*y;
}
cout<<ans;
return 0;
}
报数:
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
int m[5];
for(int i=0;i<5;i++){
m[i] = 0;
}
cin>>n;
int i=0;
int c=1;
while(i!=n){
if(c%7==0){
m[c%4]++;
}else if(c%10==7 || (c/10)%10==7 || (c/100)%10==7){
m[c%4]++;
}else{
i++;
}
c++;
}
for(int i=1;i<4;i++){
cout<<m[i]<<endl;
}
cout<<m[0];
return 0;
}
回收站选址:
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
typedef pair<int,int> PII;
int n;
vector<PII> a;
map<PII,int> b;
map<PII,int> c;
int s[5];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
while(n--){
int x,y;
cin>>x>>y;
a.push_back({x,y});
b[{x+1,y}]++;
b[{x,y+1}]++;
b[{x-1,y}]++;
b[{x,y-1}]++;
c[{x+1,y+1}]++;
c[{x-1,y-1}]++;
c[{x-1,y+1}]++;
c[{x+1,y-1}]++;
}
for(int i=0;i<a.size();i++){
PII t = a[i];
if(b[t]==4){
s[c[t]]++;
}
}
cout<<s[0]<<endl<<s[1]<<endl<<s[2]<<endl<<s[3]<<endl<<s[4];
return 0;
}
化学方程式:
这题非常难,需要重做,下面是抄的代码
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int n;
string formula;
unordered_map<string,int> ans;
int computeDigit(int& first,int last){
int i=0;
for(;first<=last and isdigit(formula[first]);++first){
i = i*10 + formula[first]-'0';
}
return i == 0 ? 1:i;
}
void f(int first,int last,int e){
if(first == last or last-first==1 and islower(formula[last])){
ans[formula.substr(first,last-first+1)] += e;
return;
}
e *= computeDigit(first,last);
for(int i= first,j = i+1;i<=last;i=j,++j){
if(isupper(formula[i])){
if(j<=last and islower(formula[j]))
j++;
int k = j; //这里非常关键,因为computeDigit函数会改变第一个参数的值,所以j的值需要先保存起来
f(i,k-1,e*computeDigit(j,last));
}else if(formula[i] == '('){
for (int num = 1;num!=0;j++){
if(formula[j] == '(')num++;
else if (formula[j] == ')')num--;
}
int k=j;
f(i+1,k-1,e*computeDigit(j,last)) ;
}
}
}
void expression(int first,int last,int e){
for(int i=first,j=first;i<=last;i = j+1){
j = formula.find('+',i);
if(j==string::npos or j>last){//这里可以改成end的吧
j = last + 1;
}
f(i,j-1,e);
}
}
int main(){
cin>>n;
while(n--){
cin>>formula;
ans.clear();
int k = formula.find('=');
expression(0,k-1,1);
expression(k+1,formula.size()-1,-1);
auto i = find_if(ans.begin(),ans.end(),[](pair<string,int> p){return p.second!=0;});
cout << ((i == ans.end()) ? "Y" : "N")<<endl;
}
return 0;
}
测试数据:
8
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
Cu+As=Cs+Au
收获:
find返回数字,表示位置
find_if返回特定对象
ans.clear();
str.find('=');//单纯查字符
int i=0,j=0;//连续声明
24点:
自己写的感觉不错的代码:
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int num[5];
char op[4];
bool f(char a){
if(a=='+'||a=='-')return true;
return false;
}
int cal(int a,int b,char oper){
if(oper=='+'){
return a+b;
}else if(oper=='-'){
return a-b;
}else if(oper=='x'){
return a*b;
}else if(oper=='/'){
return a/b;
}
return 1;
}
int main(){
cin>>n;
while(n--){
cin>>s;
int a = 0,b = 0;//a表示+-的个数
num[1] = s[0];
num[2] = s[2];
num[3] = s[4];
num[4] = s[6];
int res;
if(f(s[1]) and !f(s[3]) and !f(s[5])){
res = cal(cal(cal(num[2]-'0',num[3]-'0',s[3]),num[4]-'0',s[5]),num[1]-'0',s[1]);
cout<<res<<endl;
cout<<(( res ==24) ?"Yes":"No")<<endl;
}else if(!f(s[1]) and f(s[3]) and !f(s[5])){//
res = cal(cal(num[1]-'0',num[2]-'0',s[1]),cal(num[3]-'0',num[4]-'0',s[5]),s[3]);
cout<<res<<endl;
cout<<(( res ==24) ?"Yes":"No")<<endl;
}else if(f(s[1]) and !f(s[3]) and f(s[5])){
res = cal(cal(cal(num[2]-'0',num[3]-'0',s[3]),num[1]-'0',s[1]),num[4]-'0',s[5]);
cout<<res<<endl;
cout<<(( res ==24) ?"Yes":"No")<<endl;
}else if(f(s[1]) and f(s[3]) and !f(s[5])){//
res = cal(cal(num[1]-'0',num[2]-'0',s[1]),cal(num[3]-'0',num[4]-'0',s[5]),s[3]);
cout<<res<<endl;
cout<<(( res ==24) ?"Yes":"No")<<endl;
}else{
res = cal(cal(cal(num[1]-'0',num[2]-'0',s[1]),num[3]-'0',s[3]),num[4]-'0',s[5]);
cout<<res<<endl;
cout<<(( res ==24) ?"Yes":"No")<<endl;
}
}
return 0;
}