第十六次CCF计算机软件能力认证题解
我在场外,只做了第二题和第三题。就说说这两道题目吧。
这道题目,就是给你一个只包含七个字符的式子,判断是否等于24。按照四则混合运算进行计算。所以需要先做乘法和除法。再做加法和减法。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;
int a[4];
char b[3];
int getTag(string s);
int main() {
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
string s;
cin>>s;
int ans = getTag(s);
printf("%s\n",(ans==24)?"Yes":"No");
}
}
int getTag(string s){
int n = s.length();
a[0] = s[0] - '0';
int cnt = 1;
int cntb = 0;
for(int i=1;i<n;i++){
if(i%2==0){
a[cnt++] = s[i] - '0';
}else{
b[cntb++] = s[i];
}
}
int tmp;
for(int i=0;i<3;i++){
if(b[i]=='+' || b[i]=='-'){
continue;
}else{
if(b[i]=='x'){
int l = i;
while(a[l]==-1){
l = l - 1;
}
tmp = a[l] * a[i+1];
a[l] = tmp;
a[i+1] = -1;
}else if(b[i]=='/'){
int l = i;
while(a[l]==-1){
l = l - 1;
}
tmp = a[l] / a[i+1];
a[l] = tmp;
a[i+1] = -1;
}
}
}
int ans = a[0];
for(int i=0;i<3;i++){
if(b[i]=='+'){
ans = ans + (a[i+1]);
}else if(b[i]=='-'){
ans = ans - (a[i+1]);
}
}
return ans;
}
第三题
这道题是一个大模拟,题目可以仔细看看
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 1100;
int n,s,l;
struct node{
int number;
string value;
};
set<int> numbers;
node a[maxn];
int xx[1000000];
int yy[1000000];
int getPanHao(int tiao_dai_bian_hao){
return xx[tiao_dai_bian_hao];
}
int getPaiHao(int tiao_dai_bian_hao){
return yy[tiao_dai_bian_hao];
}
int getPaiNeiPaiHao(int tiaodaibianhao, int b){
int hah = b - tiaodaibianhao * s;
return hah;
}
int getBegin(int tiao_dai_bian_hao, int b){
int hah = b - tiao_dai_bian_hao * s;
int paihao = getPaiHao(tiao_dai_bian_hao);
int total = paihao * s + hah;
return total*8;
}
int getten(char cc){
int nnnn = cc - '0';
if (nnnn>=0 && nnnn<=9){
return nnnn;
} else{
return cc - 'A' + 10;
}
}
char get16(int dd){
if (dd<10){
return dd+'0';
} else{
int xxx = dd - 10;
xxx = xxx + 'A';
return xxx;
}
}
char yihuo(char aa,char bb){
int ans = getten(aa) ^ getten(bb);
return get16(ans);
}
int main(){
scanf("%d%d%d",&n,&s,&l);
int max_bian_hao = -1;
for(int i=0;i<l;i++){
cin>>a[i].number>>a[i].value;
numbers.insert(a[i].number);
if(i==0){
int tmp = a[i].value.length();
tmp = tmp / 8;
tmp = tmp / s;
max_bian_hao = tmp * (n-1);
}
}
// < n
int x = 0;
int y = 0;
for(int i=0;i<max_bian_hao;i++){
xx[i] = x;
yy[i] = y;
x = x + 1;
if (x >= n ){
x = 0;
if(((x+y)-(n-1))%n == 0){
y = y+1;
continue;
}
}
int kk = x+y;
if( (kk-(n-1))%n == 0 ){
x = x - 1;
x = x+1;
y = y+1;
}
}
int m;
scanf("%d",&m);
for(int i = 0;i<m;i++){
int b ;
scanf("%d",&b);
if((n-l)>1){
printf("-\n");
continue;
}
int tiao_dai_bian_hao = floor(b/s);
if(tiao_dai_bian_hao > max_bian_hao){
printf("-\n");
continue;
}
int panhao = getPanHao(tiao_dai_bian_hao);
if(numbers.count(panhao)==1){
int begin = getBegin(tiao_dai_bian_hao,b);
string ans = "";
for(int j=0;j<l;j++){
if(a[j].number == panhao){
int rr = begin + 8;
int ll = begin;
ans = a[j].value.substr(ll,8);
cout<<ans<<endl;
break;
}
}
}else{
// 需要找到其他的进行异或
int begin = getBegin(tiao_dai_bian_hao,b);
int ans[8];
string anstrue[8];
for(int k=0;k<8;k++){
ans[k] = 0;
for (int j = 0; j < l; ++j) {
ans[k] = ans[k] ^ getten(a[j].value[begin+k]);
// printf("%d\n",getten(a[j].value[begin+k]));
}
char hahah = get16(ans[k]);
printf("%c",hahah);
}
printf("\n");
}
}
return 0;
}