I haven't written a blog since the Blue Bridge Cup. Today, the third chapter of the previous part of the problem of the answer to fill.
3-4 adding integers
This topic has the hint, said chooses the appropriate input way, can simplify the question. I didn't think of Cin at first, and the result was a string, and I remembered Cin, thanks to someone reminding me. Shame on you.
#include <iostream>using namespace Std;int main () { int a A, B; char op; while (cin>>a>>op>>b) { Switch (OP) {case ' + ': cout<<a+b<<endl; break; Case '-': cout<<a-b<<endl; break; Case ' * ': cout<<a*b<<endl; break; } } return 0;}
3-5 rotate the character matrix counterclockwise by 90 degrees
#include <iostream> #define MAXN 10using namespace Std;int main () { char m[maxn][maxn]; int i,j,n; while (Cin>>n) {for (i = 0, i < n; i++) {for (j = 0;j<n;j++) { cin>>m[i][j]; } } for (i = n-1;i>=0;i--) {for (j = 0;j<n;j++) { cout<<m[j][i]; } cout<<endl; } } return 0;}
3-6 binary conversion (converts decimal to other binary)
This problem I will expand the topic into a hdu2031 conversion, the following I also in the way of hdu this problem (but it seems to be no different)
#include <iostream> #include <string.h> #define MAXN 10using namespace Std;int main () {int n,b;int p,cnt; Char NUM[MAXN]; while (cin>>n>>b) { memset (num,0,sizeof (num)); CNT = 0; if (n<0) { cout<< "-"; n =-n; } if (n<2) cout<<n<<endl; else{ while (n>0) { p = n%b; n/= B; if (p>9) num[cnt++] = p + ' A '-ten; else num[cnt++] = p + ' 0 '; } NUM[CNT] = ' + '; } for (int i = strlen (num) -1;i>=0;i--) cout<<num[i]; cout<<endl; } return 0;}
3-7 binary conversion (converting other binary to decimal)
This topic originally wanted to expand it into a very large number of arbitrary conversions into decimal numbers, but did not think of a more concise and powerful method, so I can only question the topic.
#include <iostream> #include <string.h> #include <cmath> #define MAXN + 10;using namespace std ; int change (int n,int b) { int ans = 0; int p; for (int i = 0; n>0;i++,n/=10) { p = (n%10) * POW (b,i); ans + = p; } return ans;} int main () {int n,b; while (cin>>n>>b) { cout<<change (n,b) <<endl; } return 0;}
3-8 Phone Keypad
This one has seen a decisive table, violence resolved.
#include <stdio.h> #include <string.h> #define MAXN 10typedef struct{ char A; Char str[10];} Ch;int Main () { char word[maxn]; CH p[26]={{' A ', ' A1 '}, {' B ', ' b2b1 '}, {' C ', ' c3c1 '}, {' d ', ' D1 '}, {' E ', ' e2e1 '},{' f ', ' f3f1 '},{' g ', ' G1 '} , {' H ', ' H2h1 "},{' I '," I3i1 "},{' J '," J1 "}, {' K '," k2k1 "},{' l '," L3L1 "}, {' m '," M1 " }, {' n '," n2n1 "},{' O '," o3o1 "},{' P '," P1 "}, {' Q ', ' q2q1 '}, {' R ', ' R3r1 '},{' s ', ' s4s1 '},{' t ', ' T1 '}, {' U ', ' u2u1 '},{' V ', ' v3v1 '}, {' w ', ' W1 '}, {' X ', ' x2x1 '}, {' Y ', ' y3y1 '},{' z ', ' Z4z1 '}}; while (1) {scanf ("%s", word); for (int i = 0; i < strlen (word); i++) { for (int j = 0; j<26;j++) { if (word[i]==p[j].a) { printf ("%s", p[ J].STR);}}} printf ("\ n"); } return 0;}
In this case, we will be the beginning of the classic algorithm competition is an over-the-ground.
If there is a better solution or error, please give more advice.
(If reproduced, please specify the source)
"Algorithmic Competition Primer Classic" "chapter three" after-school exercises (Part II)