Algorithms change the world. The importance of algorithms is self-evident to a programmer, so the practice of some algorithms is indispensable. Next, I will share some algorithmic problems.
Question
Analysis Arrays or stacks can be used to store data, multiply and divide first, convert all operations into additions, and then add.
Solution Here is the solution to this problem.The following are two solutions, the result is 100 points.
Program Method One 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include <iostream> #include <string> #include <cstdio> using namespace std ;const int BLOCK_SIZE=4 ;string disk[1000 ];int main () { int disk_num, band_size, disk_remain, output_num, output_block[1000 ], disk_band=0 ; cin >> disk_num >> band_size >> disk_remain; for (int i=0 ; i<disk_remain; i++){ int j; cin >> j >> disk[j]; if (disk_band==0 ) disk_band = (int )disk[j].size()/(band_size*BLOCK_SIZE*2 ); } cin >> output_num; for (int i=0 ; i<output_num; i++){ cin >> output_block[i]; int block_longitude, block_latitude, band_longitude, band_latitude, band_num; band_num = output_block[i]/band_size; band_latitude = band_num/(disk_num-1 ); int check_disk; check_disk = (disk_num-1 )-band_latitude%disk_num; band_longitude = check_disk+band_num%(disk_num-1 )+1 ; while (band_longitude>=disk_num) band_longitude -= disk_num; block_longitude = band_longitude; block_latitude = band_latitude*band_size+output_block[i]%band_size; if ((disk[block_longitude].empty() && (disk_num-disk_remain)>1 ) || output_block[i]>(disk_band*band_size*(disk_num-1 )-1 ) || disk_band==0 ) cout << "-" << endl ; else { int block_start = block_latitude*BLOCK_SIZE*2 ; if (!disk[block_longitude].empty()) cout << disk[block_longitude].substr(block_start, BLOCK_SIZE*2 ) << endl ; else { for (int j=0 ; j<BLOCK_SIZE*2 ; j++){ int temp = 0 ; for (int k=0 ; k<disk_num-1 ; k++){ if (k!=block_longitude && disk[k][block_start]>='A' && disk[k][block_start]<='Z' ){ temp=temp^((int )disk[k][block_start]-55 ); }else if (k!=block_longitude && disk[k][block_start]>='0' && disk[k][block_start]<='9' ){ temp=temp^((int )disk[k][block_start]-48 ); } } cout << uppercase << hex << temp; block_start ++; } cout << endl ; } } } }
Method Two 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 #include <iostream> #include <cstdio> #include <string> using namespace std ;int len = 0 ;char getc (char c) { if (c<10 ) return c+'0' ; else if (c<=16 ) return c+'A' -10 ; else if (c<='9' ) return c-'0' ; else if (c<='F' ) return c-'A' +10 ; else return c; } void cxor (string &a,string b) { for (int i=0 ;i<8 ;i++){ a[i]=getc(getc(a[i])^getc(b[i])); } } void getdata (string dskd[], int n, int s, int l, int r) { int level=r/((n-1 )*s); int curp=(n-level%n)-1 ; int blk=level*s+r%s; int disk=r%((n-1 )*s)/s+curp+1 ; disk=disk%n; if (len<blk*8 +8 ||len==0 ){ cout <<"-" <<endl ; return ; } if (dskd[disk].length()!=0 ) cout <<dskd[disk].substr(blk*8 ,8 )<<endl ; else if (dskd[disk].length()==0 &&n-l==1 ){ string str="00000000" ; for (int i=0 ;i<n;i++){ if (i!=disk) cxor(str,dskd[i].substr(blk*8 ,8 )); } cout <<str<<endl ; }else cout <<"-" <<endl ; } int main () { int n,s,l; cin >>n>>s>>l; string dskd[1010 ]; int dpos; for (int i=0 ;i<l;i++){ cin >>dpos; cin >>dskd[dpos]; } int m; cin >>m; int a[1000 ]; for (int i=0 ;i<m;i++){ cin >>a[i]; } for (int i=0 ;i<n;i++){ if (dskd[i].length()!=0 ){ len = dskd[i].length(); break ; } } for (int i=0 ;i<m;i++){ getdata(dskd,n,s,l,a[i]); } return 0 ; }
This program is written in C++ and uses standard input and output functions.
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !