C language common face test (classic)

Source: Internet
Author: User
1. Enter a string of characters to calculate the number of spaces.
#include <stdio.h>
#include <string.h>
int function (char *str);
int main ()
{
Char str[30];
int num = 0;
scanf ("%[^\n]", str);
num = function (str);
printf ("num =%d\n", num);
return 0;
}
int function (char *str)
{
int i = 0, ret = 0;
for (i = 0; i < strlen (str); i++)
{
if (str[i] = = ")
RET + 1;
}
return ret
}
2. The young singer participates in the song Grand Prix, has 10 judges to score to her, the trial programming asks this contestant's average score (removes one highest score and one lowest score). Requirements: Encapsulates the sorted code in the function body.
#include <stdio.h>
int * sort_1 (int a[], int size);
int * sort_2 (int a[], int size);
int main ()
{
int i;
Float Ave = 0;
int a[10];
printf ("Plz input score like:\n");
for (i = 0; i < i++)
scanf ("%d", &a[i]);
Sort_2 (A, 10);
for (i = 0; i < i++)
printf ("%d", a[i]);
Puts ("");
for (i = 1; i < 9; i++)
{
printf ("Ave =%d\n", a[i]);
Ave = Ave+a[i];
}
printf ("Final score is:%6.1f", AVE/8);
Puts ("");
return 0;
}
int * sort_1 (int a[], int size)
{
int min = 0;
int I, J;
for (i = 0; i < size-1; i++)
{
min = i;
for (j = i + 1; j < size; J + +)
{
if (A[j] < a[min])
min = j;
}
if (min!= i)
{
A[i] ^= a[min];
A[min] ^= a[i];
A[i] ^= a[min];
}
}
return A;
}
int * sort_2 (int a[], int size)
{
int I, J;
for (i = 0; i < size; i++)
{
for (j = 0; J < size-1-I; j + +)
{
if (A[j] > a[j + 1])
{
A[J] ^= a[j + 1];
A[j + 1] ^= a[j];
A[J] ^= a[j + 1];
}
}
}
return A;
}
3. There is a 3x4 matrix that requires outputting the value of the element with the largest value, along with its row and column numbers.
int a[3][4] = {
{123, 94,-10, 218},
{3, 9, 10,-83},
{45, 16, 44,-99}
};
#include <stdio.h>
int main ()
{
int a[3][4] = {
{123, 94,-10, 218},
{3, 9, 10,-83},
{45, 16, 44,-99}
};
int I, J, Max, X, y;
max = a[0][0];
for (i = 0; i < 3; i++)
for (j = 0; J < 4; J + +)
if (A[i][j] > Max)
{
max = A[i][j];
x = i; y = j;
}
printf ("max =%d, x =%d, y =%d\n", max, x, y);
return 0;
}


4. Print the following pattern:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
#include <stdio.h>

int main (int argc,char **argv)
{
Char a[5]={' * ', ' * ', ' * ', ' * ', ' * '};
int i,j,k;
Char space= ';
for (i=0;i<5;i++)/* Output 5 Line * *
{
printf ("\ n"); /* The output should be changed before each line * *
printf (""); * * Leave 5 spaces before each line
for (j=1;j<=i;j++)
printf ("%c", space); * * Leave 1 more spaces per line
for (k=0;k<5;k++)
printf ("%c", A[k]); /* Enter 5 * * # per line * *
}
Puts ("");
return 0;
}
5. Find the sum of the lower-left triangular elements of the matrix
#include <stdio.h>
#define N 6
int main (int argc,char **argv) {
int i,j,sum=0;
int A[n][n] = {0};
printf ("Input 5x5 data:\n");
for (i=1;i<n;i++) {
printf ("Input The%d line data:\n", i);
for (j=1;j<n;j++)
scanf ("%d", &a[i][j]);
}
for (i=1;i<n;i++) {
for (j=1;j<n;j++)
printf ("%5d", A[i][j]);
printf ("\ n");
}
for (i=1;i<n;i++) {
for (j=1;j<=i;j++) {
printf ("%d\n", A[i][j]);
SUM=SUM+A[I][J];
}
}
printf ("sum=%d\n", sum);
}

6. Write a function that counts how many digits in a value of type int are 1
#include <stdio.h>
int fun (int data);
int main ()
{
int A;
int num;
scanf ("%d", &a);
num = Fun (a);
printf ("num =%d\n", num);
return 0;
}
int fun (int data)
{
int i = 0, count = 0;
int temp = data;
for (i = 0; i < i++)
{
if ((temp & 1) = = 1)
count++;
temp = temp >> 1;
}
return count;
}
7. Implement a similar atoi function to convert the string "123456" to a numeric 123456 int atoi (char *str);

#include <stdlib.h>
#define N 32
int My_atoi (char *s);
int main ()
{
Char A[n];
scanf ("%s", a);
int num = 0;
num = My_atoi (a);
printf ("%d\n", num);
return 0;
}
int My_atoi (char *s)
{
char *p = s;
char c;
int i = 0;
while (c=*p++)
{
if (c>= ' 0 ' && c<= ' 9 ')
{
i = i*10 + (c ' 0 ');
}
Else
return-1;
}
return i;
}
8. Design an algorithm that moves all odd numbers in array s[n to all even numbers. Require no additional storage space;
#include <stdio.h>
#define N 10
void sort (int *a, int n);
int main ()
{
int A[n] = {1,2,3,4,5,6,7,8,9,10};
int I, J;
Sort (A, N);
for (i = 0; i < N; i++)
{
printf ("%d", a[i]);
}
Puts ("");
return 0;
}
void sort (int *a, int n)
{
int i,len,temp;
len=n-1;
for (i=0;i<len;)
{
if ((a[i]%2) ==0)//To determine if an even number
{
A[len] ^= a[i];
A[i] ^= A[len];
A[len] ^= a[i];
len--;
}
Else
i++;
}
Return
}

9. Implement function char *fun (char *str1, char *str2); Returns the address of the string str2 from the str1 of the strings, if STR2 does not exist, returns-1.
#include <stdio.h>
#include <string.h>
#define N 32
char * STRSTR1 (char * str1,char * str2);
int main ()
{
Char Str1[n], str2[n];
Char *STR3;
scanf ("%[^\n]", str1);
GetChar ();
scanf ("%[^\n]", str2);
STR3 = Strstr1 (str1, str2);
printf ("%s\n", STR3);
return 0;
}
char * STRSTR1 (char * str1,char * str2)
{
char *CP = str1;
Char *s1, *S2;

if (!*STR2)
return-1;
while (*CP)
{
S1 = CP;
s2 = str2;
while (*s1 && *s2 &&! ( *S1-*S2))
s1++, s2++;
if (!*S2)
return CP;
cp++;
}
return CP;
}

10. Implement the following functions unsigned int sum (unsigned int val); The value of each byte in the Val is incremented and returned.
#include <stdio.h>
int function (int val);

int main () {
int A;
scanf ("%d", &a);
int sum;
sum = function (a);
printf ("sum =%d\n", sum);
return 0;
}
int function (int val)
{
int sum = 0;
sum = ((0xff000000&val) >>24) + ((val& 0x00ff0000) >>16) +
((VAL&0X0000FF00) >>8) + (VAL&0X000000FF);
return sum;
}




























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.