Exercise 3-3 the last three bits of the product
Enter several words, enter a number of integers (which can be positive, negative, or 0), and output the last three bits of their product. These integers are mixed with a string of uppercase letters, and your program should ignore them. Tip: Try to enter a string when executing scanf ("%d").
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 100#define MOD 1000char A[MAXN] ; int main (int argc, char *argv[]) { int i, n; Long sum = 0, Product = 1; int sign; while (scanf ("%s", a) = = 1) { n = strlen (a); for (i = 0; i < n; i++) { if (A[i] >= ' A ' && a[i] <= ' Z ') break;//must determine how to exit if (a[i] = = '-' | | A[i] = = ' + ') continue; sum = (sum*10 + a[i]-' 0 ')%mod; } if (A[i] >= ' A ' && a[i] <= ' Z ') continue; Product = Product*sum%mod; sum = 0;//This count is used, each time to clear 0 } printf ("%3d\n", product); System ("PAUSE"); return 0;}
Summary: 1 Note overflow
2 for a break in the bad, the next code is to determine where the for-through-bad exits from.
3 sum = 0 Remember to clear the zero because you are reading a new string
Exercise 3-4 Calculator
Write a program that reads a line of expressions that exactly contain a plus, minus, or multiplication sign, and outputs its value. This operator is guaranteed to be a two-tuple operator, and two numbers are non-negative integers that do not exceed 100. The operands and operators can be next to each other, or they can be separated by one or more spaces and tabs. There can be spaces at the end of the line. Tip: Choose the right input method to simplify the problem.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 100char A[MAXN]; int main (int argc, char *argv[]) { int i, n, x = 0, y = 0, middle; while (Fgets (A, sizeof (a), stdin)) { n = strlen (a); for (i = 0; i < n-1; i++) { if (a[i] = = ' + ' | | a[i] = = '-' | | a[i] = = ' * ') middle = i; } for (i = 0; i < middle; i++) { if (a[i] = = ") Continue; x = x*10 + a[i]-' 0 '; } for (i = middle+1; i < n-1; i++) { if (a[i] = = ") Continue; y = y*10 + a[i]-' 0 '; } Switch (A[middle]) {case ' + ': printf ("%d\n", x+y); Case '-': printf ("%d\n", X-y); Case ' * ': printf ("%d\n", x*y); x = 0; y = 0; } System ("PAUSE"); return 0;}
Summary: 1 input with fgets finally there's a \ n and a, but Strlen doesn't fall in.
2 x y to clear Zero
A simpler way to go
#include <stdio.h> #define MAXN int main () { int a,b,c; char F; scanf ("%d", &a); scanf ("%c", &f); while (f!= ' + ') && (f!= '-') && (f!= ' * ')) scanf ("%c", &f); scanf ("%d", &b); if (f== ' + ') c=a+b; else if (f== '-') c=a-b; else c=a*b; printf ("%d", c); return 0;
}
Exercise 3-5 Rotation
Enter a n*n character matrix and turn it left 90 degrees after the output.
#include <stdlib.h> #define MAXN 100char a[maxn][maxn];int Main (int argc, char *argv[]) { int n, I, J; scanf ("%d\n", &n); for (i = 0; i < n; i++) scanf ("%s", A[i]); /* for (i = 0; i < n; i++) { for (j = 0; J < N; j + +) scanf ("%c", &a[i][j]); } * /for (j = n-1; J >= 0; j--) {for (i = 0; i < n; i++) { printf ("%c", A[i][j]); } printf ("\ n"); } System ("PAUSE"); return 0;}
Summary: The scanf ("%c") will also read the space after the character or line break in it????
Algorithm competition Getting Started classic 3-3 product of the last three-bit exercise 3-4 Calculator Exercise 3-5 rotation