2014江西理工大学C语言程序竞赛高级组

时间:2022-04-09 01:37:51

Beautiful Palindrome Number

题意:求N里面有多少个符合要求的数字(数字要求:回文数,且前一半部分是不严格递增)

解法:打表

#include<bits/stdc++.h>
using namespace std;
int find1(int index)
{
int count1 = ;
int number = ; //记录数位上的回文数,如个位回文数为9
int w = ; //记录数位 long half; //保存回文数的左半边的结果
long h = ; //回文数的左半边的起始基数
long res; //结果 while(true)
{
if(w > && w% == ) //每进两个数位,回文数乘以10
{
number *= ;
}
w++; //数位加一
if(count1 + number > index) //回文数大于查找的回数,跳出
break; count1+= number; //回文数加上当前数位上的回文数
} index -= count1; //在当前数位上的位置。如w=5,index=50,则万位上的第50个回文数是我们所求 for(int i = ; i < (w-) / ; i++) //求回文数的左半边的基数,如回文数在万位上,则为100
{
h *= ;
} half = h + index; //回文数的左半边,如100 + 50 = 150 res = half; if(w% != ) //如果为奇数,则中间那个数不必算入右半边了!
half /=; while(half != ) //拼接回文数
{
res = res * + half % ;
half /= ;
} return res;
}
int cmd(int x)
{
int cot=;
int a[];
while(x)
{
a[cot++]=x%;
x/=;
}
int pos=a[];
if(cot%==)
{
for(int i=; i<cot/; i++)
{
if(pos<=a[i])
{
pos=a[i];
}
else
{
return ;
}
}
}
else
{
for(int i=; i<=cot/; i++)
{
if(pos<=a[i])
{
pos=a[i];
}
else
{
return ;
}
}
} return ;
}
int a[]={,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
,};
int main()
{
int t;
int n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=;;i++)
{
if(a[i]==) break;
else if(a[i]>n)
{
cout<<i<<endl;
break;
}
}
}
return ;
}

五子棋

解法:其实不难,判断却非常复杂,首先当然只要判断是否成为5子。然后判断黑白棋的个数是否满足要求,接下来判断是白子后下还是黑子后下。再根据这两种情况判断,具体看代码判断

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ans[][];
//int n,m,a,b;
int Min=(1e9);
string a[];
int x1,x2,x3,x4,x5,x6,x7,x8;
int cmd(char s)
{
int M=-;
for(int i=; i<; i++)
{ for(int j=; j<; j++)
{
x1=,x2=,x3=,x4=,x5=,x6=,x7=,x8=;
if(a[i][j]==s)
{
//cot1++;
// cout<<"/"<<endl;
int ans1=i;
int ans2=j;
while(ans1>=)
{
if(a[ans1][j]==s)
{
// a[i][ans2]='.';
ans1--;
x1++;
}
else
{
break;
}
}
// cout<<x1<<"A"<<endl;
ans1=i;
ans2=j;
//cout<<ans1<<" "<<ans2<<endl;
while(ans1<)
{
if(a[ans1][j]==s)
{
// a[ans1][j]='.';
ans1++;
x2++;
}
else
{
break;
}
}
// cout<<x2<<"B"<<endl;
ans1=i;
ans2=j;
while(ans2>=)
{
if(a[i][ans2]==s)
{
// a[i][ans2]='.';
ans2--;
x3++;
}
else
{
break;
}
}
// cout<<x3<<"C"<<endl;
ans1=i;
ans2=j;
while(ans2<)
{
// cout<<i<<" "<<ans2<<endl;
// cout<<a[i][ans2]<<endl;
if(a[i][ans2]==s)
{
// cout<<"A"<<endl;
// a[i][ans2]='.';
ans2++;
x4++;
}
else
{
break;
}
}
//cout<<x4<<"D"<<endl;
ans1=i;
ans2=j;
while(ans1>=&&ans2>=)
{
if(a[ans1][ans2]==s)
{
// a[ans1][ans2]='.';
ans1--;
ans2--;
x5++;
}
else
{
break;
}
}
//cout<<x5<<endl;
ans1=i;
ans2=j;
while(ans1<&&ans2<)
{
if(a[ans1][ans2]==s)
{
// a[ans1][ans2]='.';
ans1++;
ans2++;
x6++;
}
else
{
break;
}
}
//cout<<x6<<endl;
ans1=i;
ans2=j;
while(ans1>=&&ans2<)
{
if(a[ans1][ans2]==s)
{
// a[ans1][ans2]='.';
ans1--;
ans2++;
x7++;
}
else
{
break;
}
}
// cout<<x7<<endl;
ans1=i;
ans2=j;
while(ans1<&&ans2>=)
{
if(a[ans1][ans2]==s)
{
//a[ans1][ans2]='.';
ans1++;
ans2--;
x8++;
}
else
{
break;
}
}
// cout<<x1<<" "<<x2<<endl;
// cout<<x3<<" "<<x4<<endl;
// cout<<x5<<" "<<x6<<endl;
// cout<<x7<<" "<<x8<<endl;
//cout<<x8<<endl;
M=max({x1+x2-,x3+x4-,x5+x6-,x7+x8-,M});
} }
// M=-1;
}
if(M>=)
{
return ;
}
else
{
return ;
}
}
int cmp(char s)
{
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(a[i][j]==s)
{
a[i][j]='.';
for(int k=; k<; k++)
{
//cout<<a[k]<<endl;
}
// cout<<cmd(s)<<endl;
if(!cmd(s))
{
return ;
}
// cout<<endl;
a[i][j]=s;
}
}
}
return ;
}
int main()
{ int t;
cin>>t;
while(t--)
{
x1=,x2=,x3=,x4=,x5=,x6=,x7=,x8=;
int M=-;
int cot1=;
int cot2=;
int N=-;
for(int i=; i<; i++)
{
cin>>a[i];
for(int j=; j<; j++)
{
if(a[i][j]=='@')
{
cot1++;
}
else if(a[i][j]=='X')
{
cot2++;
}
}
}
if(cot2==cot1+ || cot1 == cot2)
{
int wb = cmd('X');
int ww = cmd('@');
if(cot2==cot1+ && wb)
{
if(ww)
cout << "wrong state" << endl;
else
{
if(cmp('X'))cout << "black" << endl;
else cout << "wrong state" << endl;
}
}
else if(ww && cot1 == cot2)
{
if(wb)
cout << "wrong state" << endl;
else
{
if(cmp('@'))cout << "white" << endl;
else cout << "wrong state" << endl;
}
}
else if(!wb && !ww)cout << "continue" << endl;
else cout << "wrong state" << endl;
}
else cout << "wrong state" << endl;
}
return ;
}

丁丁历险记

解法:数据不大,跑最短路,再求1-A到A+B哪一个更短

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ans[][];
int n,m,a,b;
int Min=(1e9);
int main()
{
int t;
cin>>t;
while(t--)
{
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
ans[i][j]=Min;
}
}
cin>>a>>b>>m;
for(int i=;i<=m;i++)
{
int s,e;
cin>>s>>e;
if(s!=e)
{
ans[s][e]=;
ans[e][s]=;
}
ans[s][s]=;
ans[e][e]=;
}
for(int k=; k<=a+b; k++)
{
for(int i=; i<=a+b; i++)
{
for(int j=; j<=a+b; j++)
{
ans[i][j]=min(ans[i][j],ans[i][k]+ans[k][j]);
}
}
}
int M=(1e9);
for(int i=;i<=a;i++)
{
M=min(ans[i][a+b],M);
}
if(M==(1e9))
{
puts("No");
}
else
cout<<M<<endl;
}
return ;
}

平行四边形

解法:数学,重点是知道a,b,c,d是干什么的

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int n;
int t;
cin>>t;
while(t--)
{
int a,b,c,d;
int sum=;
cin>>a>>b>>c>>d;
for(int i=;i<d;i++)
{
if(i*c%d==)
{
sum+=b-;
}
else
{
sum+=b;
}
}
cout<<sum<<endl;
}
return ;
}

木木换班

解法:结构体排序

#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct P
{
int a,b;
}He[];
bool cmd(P x,P y)
{
if(x.b==y.b)
{
return x.a<y.a;
}
return x.b>y.b;
}
int main()
{
int t;
int n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>He[i].a>>He[i].b;
}
sort(He+,He+n+,cmd);
for(int i=;i<=n;i++)
{
// cout<<He[i].a<<" "<<He[i].b<<endl;
}
cout<<He[].a+He[].b<<endl;
}
return ;
}

0.333333333......

解法:a*100000....%b然后xjb求

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
double a,b;
cin>>a>>b;
double s=a/b*1.0;
s*=;
// cout<<s<<endl;
ll s1=(ll)s;
// cout<<s1<<endl;
if(abs(s1-s)<=1e-)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
// printf("[%d,%d]\n",a-1,b-1);
}
return ;
}

定义域

解法:没啥好说的

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
printf("[%d,%d]\n",a-,b-);
}
return ;
}