C language mooc applet (1)

When learning mooc, I see a lot of interesting applets in C language. I have done some by myself.
First, read the integer.
Enter a number and output its pronunciation.
Pronunciation using a simple switch
And take out each digit with / percent.

int main(int argc, char *argv[])
{
    int n;
    scanf("%d",&n);
    if(n<0)
    {
        printf("fu ");
        n=-n;
    }
    int t=n;
    int a=1;
    while(t>=10)
    {
        t/=10;
        a*=10;
    }
    while(a>0)
    {
        int i;
        i=n/a;
        switch(i)
        {
            case 0:
            printf("ling");
            break;
            case 1:
            printf("yi");
            break;
            case 2:
            printf("er");
            break;
            case 3:
            printf("san");
            break;
            case 4:
            printf("si");
            break;
            case 5:
            printf("wu");
            break;
            case 6:
            printf("liu");
            break;
            case 7:
            printf("qi");
            break;
            case 8:
            printf("ba");
            break;
            case 9:
            printf("jiu");
            break;  
        }
        n%=a;
        a/=10;
        if(a>0)
        {
            printf(" ");
        }
    }
    return 0;
}

Second applet, output digital eigenvalues

Evaluating the eigenvalue of a number is a common encoding algorithm, and even-odd is a simple eigenvalue.For an integer, number each digit from one bit, digit 1, digit 10 is 2, and so on.The number of this integer in the nth digit is counted as x. If x and n have the same parity, write down the next 1; otherwise, write down the next 0.A binary number is formed by recording both 0 and 1 representing the parity of the corresponding bits in the order of integers.For example, for 342315, the binary number is 001101.
Similarly, each number of digits is output from right to left and converted to binary; this can be done by multiplying by 2.

int main(int argc, char *argv[]) 
{
int n,a,c=1,b=0,flag=1;
scanf("%d",&n);
while(n>0)
{
    a=n%10;
if(a%2==flag%2)
{
        b=b+c;
    }
    c=c*2;
    flag++;
    n/=10;
}
printf("%d",b);
return 0;
}

The sum of the third prime number

We think that 2 is the first prime, 3 is the second, 5 is the third, and so on.Now, given two integers n and m, 0<n<=m<=200, your program calculates the sum of all prime numbers from the nth prime number to the mth prime number, including the nth and mth prime numbers.

Putting all of the main functions into the main function is too cumbersome. Define another function to determine the prime number, then use the main to put in the array.
Note: The for loop termination condition in prime numbers can be changed to sqrt(n), which reduces the program burden.

int fun(int n)
{
    int i,flag=1;
    for(i=2;i<=sqrt(n);i++)
{
        if(n%i==0)
    {
    flag=0; break;
    }
}
    return flag;
}


int main(int argc, char *argv[]) 
{
    int n,m,i,a[200],sum=0;
    int count=0;
    for(i=2;;i++)
    if(fun(i)==1)
    {
        a[count]=i;
        count++;
        if(count==200)
        break;
    }
    scanf("%d%d",&n,&m);
    for(i=n;i<=m;i++)
    sum+=a[i-1];
    printf("%d",sum);
    return 0;
}

I've been watching mooc lately and will keep updating.

Tags: encoding C

Posted on Tue, 12 May 2020 09:21:31 -0700 by fireineyes