晚上做杭电OJ,第二题是一个比较直观的题,我也是太直观了。贴出代码,供自己反省,从7点15分一直做到结束,wa9次。。。。orz。。。。看来自己的算法功底还差的很远很远。
接下的几天,学习大白上的基础
算法部分。
#include<iostream> #include<stdio.h> #include<cstring> using namespace std; char a[20],b[20]; char c[20]; void shuchu(int num) { if(num==0) return ; shuchu(num/2); printf("%d",num%2); } void translate(int num) { if(num<16) printf("0"); if(num<8) printf("0"); if(num<4) printf("0"); if(num<2) printf("0"); /* while(num) { printf("%d",num%2); num=num/2; }*/ shuchu(num); } void fun1() { if(!strcmp(a,"ADD")) printf("000001"); if(!strcmp(a,"SUB")) printf("000010"); if(!strcmp(a,"DIV")) printf("000011"); if(!strcmp(a,"MUL")) printf("000100"); if(!strcmp(a,"MOVE")) printf("000101"); if(!strcmp(a,"SET")) printf("000110"); } void fun2() { int num1=0; int i=0; while(b[i]!=‘,‘&&b[i]!=‘\0‘) { i++; } //cout<<i<<endl; if(i==3) { num1+=b[i-1]-‘0‘; num1+=(b[i-2]-‘0‘)*10; translate(num1); } else if(i==2) { num1+=b[i-1]-‘0‘; translate(num1); } if(!strcmp(a,"SET")) return; int num2=0; int j=0; i+=2; while(b[i]!=‘\0‘) { j++; i++; } if(j==1) { num2+=b[i-1]-‘0‘; translate(num2); } else if(j==2) { num2+=b[i-1]-‘0‘; num2+=(b[i-2]-‘0‘)*10; translate(num2); } } void fun3() { char d[20]; for(int i=0;i<6;i++) { d[i]=c[i]; } d[6]=‘\0‘; if(strlen(c)==16||strlen(c)==11) { } else { printf("Error!"); return; } if(strlen(c)==11) { if(strcmp(d,"000110")) { printf("Error!"); return; } } if(strlen(c)==16) { if(!strcmp(d,"000110")) { printf("Error!"); return; } } if(!strcmp(d,"000001")) printf("ADD R"); else if(!strcmp(d,"000010")) printf("SUB R"); else if(!strcmp(d,"000011")) printf("DIV R"); else if(!strcmp(d,"000100")) printf("MUL R"); else if(!strcmp(d,"000101")) printf("MOVE R"); else if(!strcmp(d,"000110")) printf("SET R"); else { printf("Error!"); return; } int num1=0; int k=16; for(int i=6;i<11;i++) { num1+=(c[i]-‘0‘)*k; k/=2; } printf("%d",num1); if(!strcmp(d,"000110")) return; printf(",R"); int num2=0; k=16; for(int i=11;i<16;i++) { num2+=(c[i]-‘0‘)*k; k/=2; } printf("%d",num2); } int main() { int n; while(cin>>n) { if(n==1) { cin>>a>>b; fun1(); fun2(); cout<<endl; } if(n==0) { cin>>c; fun3(); cout<<endl; } } return 0; }
时间: 2024-10-04 02:48:32