Array and string (score statistic _stat, word length, product end 3 digits, calculator, rotate _rotate, convert 1_base1, convert 2_base2, mobile keyboard)

Source: Internet
Author: User
Tags stdin
about the C language stringThe difference between the scanf and gets of others ' home method of String inputscanf ("%s", In_buff); Gets (In_buff); Fgets (in_buff,buffer_size + 1,stdin);

1. When using the scanf function, if there are spaces within the range of the array, the characters after the first space are ignored, which is usually not what we want. Also, the SCANF function does not provide a check that the data being written to the array is out of bounds.
2. When using the gets function, it is not safe because gets does not provide a check that the data being written to the array is out of bounds.
3. When using the Fgets function, the fgets function does not write out the array bounds as long as the second argument exactly equals the number of bytes in the array to which the first argument is passed. So, the Fgets function is the best choice. 1 score Statistics _stat It seems that this method is stupid, do not know whether there is no good idea = = question: When the score is a nonnegative real number (keep two decimal digits). Well, multiply by 100. ==

int main () {
    int a[101]={0};
    int I=0,u,max;
    while (scanf ("%.2f", &u)!=eof) {
        a[u]++;
    }
    for (i=0;i<100;i++) {
        if (A[i]>max) max=a[i];
    }
Output scores appear most frequently, if there are multiple juxtaposition, from small to large output. For
    (i=0;i<100;i++)
    {
        if (A[i]==max) printf ("%d\n", I);
    }
    return 0;
}
Input:
12 34 56 78 89 12 12 12 34 34 34
Output:
34
2 The length of the word enter some words to count the average length of the word
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main () {
    char s[1000 ],BUF[1000];
    int sum=0,count=0,i;
    Fgets (buf,sizeof (s), stdin);
    From the back forward because you don't know when the last one is due for
    (I=strlen (BUF) -1;i>0;i--) {
        if (buf[i]== ') count++;
        else sum++;
    }
    printf ("%lf\n", (double) sum/count);
    return 0;
}
I am best one
input and output:

3 Product of the last 3-bit--product (for reference) Enter a number of integers, outputting their scores at the end of three bits (remainder), where there are interfering strings Break is the end of the whole loop body, continue is the end of a single cycle has not been able to understand the topic is how to input, carefully look "input a number of", certainly not to enter a string, so you can use while. =eof to cycle output

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN
Define MOD 1000
char a[maxn];
int main ()
{
  int i, n;
  Long d= 0, sum= 1;
  while (scanf ("%s", a)!=eof)
  {
     n = strlen (a);
     for (i = 0; i < n; i++)
     {
         if (A[i] >= ' A ' && a[i] <= ' Z ');
          Be sure to judge how to exit the
             continue;
             d= (d*10+a[i]-' 0 ')%mod;
         }
         if (A[i] >= ' A ' && a[i] <= ' Z ') 
             continue;
         If it is a capital letter, you do not need to multiply
         sum=sum*d%mod;
         D= 0;//This kind of counting, each time to clear 0
      }

      printf ("%3d\n", sum);
      System ("PAUSE");
      return 0;
    }
Suppose the input:
H
HU
12
Output:
728
4 Calculator Write a program that reads a line that contains exactly one plus, minus, or multiplication expression, outputting its value. This operator is guaranteed to be a two-dollar operator, and the two numbers are not more than 100 nonnegative integers. 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: Select the appropriate input method to simplify the problem. always thinking about the one-step, finally found no use. Divided into two parts to better deal with, the operation symbol for the center divided into two parts of the calculation.
 #include <stdio.h> #include <string.h> char buf[1010];
    int main () {int m=0,n=0,i,l;
    int middle;
        while (Fgets (buf,sizeof (BUF), stdin)) {L=strlen (BUF); for (i=0;i<l-1;i++) {if (buf[i]== ' + ') | | buf[i]== '-' | |
        buf[i]== ' * ') middle=i;
            for (i=0;i<middle;i++) {if (buf[i]== ') continue;
        m=m*10+buf[i]-' 0 ';
            for (i=middle+1;i<l-1;i++) {if (buf[i]== ') continue;
        n=n*10+buf[i]-' 0 ';
    }//if (buf[i]== ') continue;
   d=d*10+buf[i]-' 0 ';
   if (buf[i]== ' + ') {sign=1;m=d;d=0}
   else if (buf[i]== '-') {sign=-1;m=d;d=0}
        else if (buf[i]== ' * ') {sign=0;m=d;d=0}
            Switch (Buf[middle]) {case ' + ': printf ("%d\n", m+n);
            Case '-': printf ("%d\n", m-n);
        Case ' * ': printf ("%d\n", m*n);
        } m = 0; n = 0;
return 0;
 }
Input:
1+1
2-     5
0   *1982
Output:
2
-3
0
5 Rotary _rotate
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Char a[1010][1010],s[ 1001][1010];
int main () {
    int i,j,n;
    scanf ("%d", &n);
    GetChar ();
    char temp;
/* 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]);
        GetChar ();
    }
/*  for (i=0;i<n;i++) {
        for (j=0;j<n;j++)
            printf ("%c", A[i][j]);
        printf ("\ n");

    } * * for
    (i=0;i<n;i++) {for
        (j=0;j<n;j++) {
            s[i][j]=a[j][n-i-1];
            printf ("%c", S[i][j]);
        printf ("\ n");
    }
    return 0;
}
Input:
3
123
456
789
Output:
3
1 2 3
4 5 6 7 8 9

various rotation
int main () {
 int a[5][6]={1,3,4,2,5,2,
    2,3,6,3,8,3,
    2,9,7,9,4,8,
    6,3,8,1,9,3,
    9,3,5,2,8,5,};
    int s90[6][5],ni90[6][5],x180[5][6];
 int i,j;

 printf ("Rotate 90 degrees clockwise matrix: \ n");
 for (i=0;i<6;i++) {for
  (j=0;j<5;j++)
  {
   s90[i][j]=a[4-j][i];
   printf ("%2d%", S90[i][j]);
  printf ("\ n");
 }

 printf ("Rotate 90 degrees counterclockwise: \ n");
 for (i=0;i<6;i++) {for
  (j=0;j<5;j++)
  {
   ni90[i][j]=a[j][5-i];
   printf ("%2d%", Ni90[i][j]);
  printf ("\ n");
 }

 printf ("Rotate 180 degree matrix: \ n");
 for (i=0;i<5;i++)
 {for
  (j=0;j<6;j++)
  {
   x180[i][j]=a[4-i][5-j];
   printf ("%2d%", X180[i][j]);
  printf ("\ n");
 }

6 in-process conversion 1_base1
#include <stdio.h>
int a[100];
int main () {
    int i=0,b,n;
    scanf ("%d%d", &b,&n);
    while (n>0) {
        a[i]=n%b;
        n=n/b;
        printf ("%d", a[i++]);
    return 0;
}
Input:
2 15
Output:
1111
7 in-process conversion 2_base2
#include <stdio.h>
int main () {
    int n,b,k=1;
    int s=0;
    scanf ("%d%d", &b,&n);
    while (n>0) {
        s+=n%10*k;
        K=k*b;
        N=N/10;
    }
    printf ("%d", s);
    return 0;
}
Input:
2 1111
Output:
15
8 Mobile Keyboard--keyboard need to pay attention to limit the scope, when the input ' Z ' when the special processing, when the input ' a ' to ' y ' when the remainder, there is no limit will appear and output a 1. The reason may be that the last input exchange behavior of a character ' "", after the remainder is 0.
#include <stdio.h>
#include <string.h>
int main () {
    char a[100];
    int i=0,k;
    while (scanf ("%c", &a[i])!=eof) {
        if (a[i]== ' z ') {printf ("%c", A[i]);p rintf ("4");
        else if (a[i]>= ' a ' &&a[i]< ' Z ') {
            k= (a[i]-' a ')%3;
            Switch (k) {case
                0:
                {printf ("%c", A[i]);p rintf ("1");
                Case 1:
                {printf ("%c", A[i]);p rintf ("2");
                Case 2:
                {printf ("%c", A[i]);p rintf ("3");
/*          if (k==0) {printf ("%c", A[i]);p rintf ("1"); continue;}
            if (k==1) {printf ("%c", A[i]);p rintf ("2"); continue;}
            if (k==2) {printf ("%c", A[i]);p rintf ("3"); continue;} *
        /} i++;
    }
    return 0;
}
Input:
Pig
Output:
P1i3g1
Nine Sudoku Input method schematic diagram:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.