Typical C language 100 questions (51-100)

Source: Internet
Author: User
Tags bitwise

This typical C language 100 question is found online. This is also available in programming enthusiast forums.

Note: I may have skipped some questions, but I have not done anything about the graphics. There are still a few questions that are not a question at all, so they are not done either.

Some questions appear twice because two different answers are written!

/* [Program 51 ~ 60] Question: learning to use bitwise AND &, bitwise OR operations |, bitwise XOR operations ^ bitwise inverse operations ~. I didn't give a specific question, so I just summarized that bitwise AND operations are bitwise "sum" for two operands. When they are both 1, the result is 1, otherwise, the result is 0 1 & 1 = & 1 = & 1 = & 1 = & 0 = 0, that is, 1 & x = 1. Therefore, use 1 and to locate the specific number. 0 & x = 0, so 0 and can be used to clear some bits in the number by bit or "|" the bitwise OR operation is a bitwise "phase or" operation on two operands ". If they are all 0, the result is 0. Otherwise, the result is 1 | x = 1. Therefore, use 1 or use 10 | x = x, therefore, zero or some bits of the number can be kept unchanged. The bitwise OR operation can also connect a string of binary numbers to another string: add N zeros to the end of the object string, and N is the number of digits of the string to be connected, then, perform a bitwise OR operation to obtain the expected result. Bitwise exclusive or operation "^" bitwise exclusive or operation is to make two numbers different or by bit. When one of them is 1 and the other is 0 (that is, when they are different), the result is 1, otherwise, the value is 0.1 ^ 0 = ^ 1 = ^ 0 ^ 1 = ^ 0 = 0, that is, 1 ^ x = ~ X, 0 ^ x = x can be used to reverse some bits of the number. Bitwise inversion "~" Bitwise inversion is to perform bitwise inversion on the operands ". The binary left-shift operator "<" indicates that the binary left-shift operation moves data several bits to the left. All bits that are removed from the left boundary disappear, and the new bits on the right side are 0. moving one bit to the left is equivalent to multiplying by two binary shifts to the right operator ">" when the binary shift to the right moves data to several places, the digits that are removed from the right boundary are lost, the rules for adding new bits on the left side are as follows: 1. for the unsigned number, the new digits on the left side are all supplemented with 0, which is called "logical right shift" 2. for the number of signed bits, if the sign bit is 0, all the new BITs to the left will be filled with 0; if the sign bit is 1, all the new bits on the left will be filled with 1, it is called "arithmetic shift right". Therefore, the first digit shifted left is increased by 2 times, and the first digit shifted right is reduced to 1/2 times. Move left to increase and move right to decrease. * // * [Program 61] Question: print out the Yang Hui triangle (10 rows required, for example) 1. program Analysis: 1 1 1 1 2 1 3 3 1 4 6 4 1 1 5 10 5 1 */# include <stdio. h> # include <stdlib. h> int main () {int I, j; int a [10] [10] = {0}; for (I = 0; I <10; I ++) a [I] [0] = 1; for (I = 1; I <10; I ++) for (j = 1; j <I + 1; j ++) a [I] [j] = a [I-1] [J-1] + a [I-1] [j]; for (I = 0; I <10; I ++) {for (j = 0; j <I + 1; j ++) printf ("% 3d", a [I] [j]); printf ("\ n") ;}system ("pause"); return 0 ;}/ * [Program 66] Question: Enter 3 numbers a, B, c, output in order of size. */# Include <stdio. h> # include <stdlib. h> int main () {int a, B, c, tem; scanf ("% d", & a, & B, & c ); if (a <B) {tem = a; a = B; B = tem;} if (B <c) {tem = B; B = c; c = tem ;} if (a <B) {tem = a; a = B; B = tem;} printf ("% d \ n", a, B, c ); system ("pause"); return 0;}/* [Program 67] Question: The input array, the largest exchange with the first element, the smallest exchange with the last element, the output array. Use two pointers to point to the largest and smallest */# include <stdio. h> # include <stdlib. h> # define N 10int main () {int a [N], * pl, * ps, tem, I; for (I = 0; I <N; I ++) scanf ("% d", & a [I]); ps = pl = & a [0]; for (I = 1; I <N; I ++) {if (* pl <a [I]) pl = & a [I]; if (* ps> a [I]) ps = & a [I];} tem = a [0]; a [0] = * pl; * pl = tem; tem = a [N-1]; a [N-1] = * ps; * ps = tem; for (I = 0; I <N; I ++) printf ("% d", a [I]); system ("pause"); return 0 ;} /* [Program 68] Question: There are n integers, so that the numbers in the front are shifted to the back m position, and the last m number is changed to the front m Number */# include <stdio. H> # include <stdlib. h> void move (int array [], int n, int m); int main () {int n, m, a [20], I, j, tem; printf ("input n and m: \ n"); scanf ("% d", & n, & m); printf ("input array: \ n "); for (I = 0; I <n; I ++) scanf ("% d", & a [I]); move (a, n, m ); system ("pause"); return 0;} void move (int array [], int n, int m) {int temarray [20]; int I, j; for (I = n-m, j = 0; I <n; I ++, j ++) temarray [j] = array [I]; for (I = 0; I <n-m + 1; I ++, j ++) temarray [j] = array [I]; For (I = 0; I <n; I ++) array [I] = temarray [I];}/* [Program 69] Question: There are n people in a circle, sequential Sn. When the first person reports the number (from 1 to 3), the person who reports the number 3 leaves the circle and asks the last person who left the number. */# Include <stdio. h> # include <stdlib. h> # define NMax 40 int main () {int a [NMax], I, j, count, n; printf ("input n: \ n "); scanf ("% d", & n); getchar (); for (I = 0; I <n; I ++) a [I] = I + 1; I = 0; j = 0; count = 0; while (count <n-1) {if (a [j]! = 0) I ++; if (I = 3) {count ++; I = 0; a [j] = 0;} j ++; if (j = n) j = 0 ;}for (I = 0; I <n; I ++) if (a [I]! = 0) {printf ("The last thing left is the original % d bits \ n", I + 1); break;} system ("pause "); return 0;}/* [Program 70] Question: Write a function, evaluate the length of a string, input a string in the main function, and output its length. */# Include <stdio. h> # include <stdlib. h> int getstrlen (char * str); int main () {char str [50]; gets (str); printf ("% d \ n ", getstrlen (str); system ("pause"); return 0;} int getstrlen (char * str) {char * p = str; while (* (p ++ )! = '\ 0'); return p-str-1;}/* [Program 71] Question: Write input () and output () function input, output data records of five students. */# Include <stdio. h> # include <stdlib. h> typedef struct {char name [20]; char sex [5]; int age;} Stu; void input (Stu * stu); void output (Stu * stu ); int main () {Stu stu [5]; printf ("enter the information of five students: Name gender age: \ n"); input (stu ); printf ("five student information: \ n name gender age \ n"); output (stu); system ("pause"); return 0 ;} void input (Stu * stu) {int I; for (I = 0; I <5; I ++) scanf ("% s % d ", stu [I]. name, stu [I]. sex, & (stu [I]. age);} void output (Stu * stu) {int I; for (I = 0; I <5; I ++) printf ("% s % d \ n", stu [I]. name, stu [I]. sex, stu [I]. age);}/* [Program 72] Question: Create a linked list. */# Include <stdio. h> # include <stdlib. h> # include <malloc. h> typedef struct LNode {int data; struct LNode * next;} LNode, * LinkList; LinkList CreateList (int n); void print (LinkList h); int main () {LinkList Head = NULL; int n; scanf ("% d", & n); Head = CreateList (n); printf ("the values of the elements of the created linked list are: \ n "); print (Head); printf (" \ n "); system (" pause "); return 0;} LinkList CreateList (int n) {LinkList L, p, q; int I; L = (LNode *) malloc (si Zeof (LNode); if (! L) return 0; L-> next = NULL; q = L; for (I = 1; I <= n; I ++) {p = (LinkList) malloc (sizeof (LNode); printf ("Enter the value of the % d element:", I); scanf ("% d ", & (p-> data); p-> next = NULL; q-> next = p; q = p;} return L;} void print (LinkList h) {LinkList p = h-> next; while (p! = NULL) {printf ("% d", p-> data); p = p-> next ;}/ * [Program 76] Question: compile a function, when n is an even number, call the function to calculate 1/2 + 1/4 +... + 1/n. When n is an odd number, call the function 1/1 + 1/3 +... + 1/n (using pointer functions) */# include <stdio. h> # include <stdlib. h> double evenumber (int n); double oddnumber (int n); int main () {int n; double r; double (* pfunc) (int ); scanf ("% d", & n); if (n % 2 = 0) pfunc = evenumber; else pfunc = oddnumber; r = (* pfunc) (n ); printf ("% lf \ n", r); system ("pause"); return 0;} double even Umber (int n) {double s = 0, a = 0; int I; for (I = 2; I <= n; I + = 2) {a = (double) 1/I; s + = a;} return s;} double oddnumber (int n) {double s = 0, a = 0; int I; for (I = 1; I <= n; I + = 2) {a = (double) 1/I; s + = a;} return s;}/* [Program 77] Question: (pointer to pointer) */# include <stdio. h> # include <stdlib. h> int main () {char * s [] = {"man", "woman", "girl", "boy", "sister"}; char ** q; int k; for (k = 0; k <5; k ++) {q = & s [k]; /* enter the content here */printf ("% s \ n", * q);} system ("pause "); Return 0;}/* [Program 78] Question: Find the person of the greatest age and output it. Please find out what is wrong with the program. */# Include <stdio. h> # include <stdlib. h> struct man {char name [20]; int age;} person [3] = {"li", 18, "wang", 19, "sun", 22 }; int main () {struct man * q, * p; int I, m = 0; p = person; for (I = 0; I <3; I ++) {if (m <p-> age) m = p-> age; q = p ++;} printf ("% s % d \ n", q-> name, q-> age); system ("pause"); return 0;}/* [Program 79] Question: String sorting. */# Include <stdio. h> # include <stdlib. h> void swap (char * str1, char * str2); int main () {char str1 [20], str2 [20], str3 [20]; printf ("Enter three strings. Each string ends with a carriage return!: \ N "); gets (str1); gets (str2); gets (str3); if (strcmp (str1, str2)> 0) swap (str1, str2 ); if (strcmp (str2, str3)> 0) swap (str2, str3); if (strcmp (str1, str2)> 0) swap (str1, str2 ); printf ("the sorted result is \ n"); printf ("% s \ n", str1, str2, str3 ); system ("pause"); return 0;} void swap (char * str1, char * str2) {char tem [20]; strcpy (tem, str1); strcpy (str1, str2); strcpy (str2, tem);}/* [Program 80] question: there are a bunch of peaches on the beach, five monkeys to score. The first monkey divided the peaches into five equal portions and one more portion. The monkey threw one more portion into the sea and took the other portion. The second monkey divides the remaining peaches into five equal portions and adds one more portion. It also throws one more portion into the sea and takes one portion, the third, fourth, and fifth monkeys did this. How many peaches did they have on the beach? */# Include <stdio. h> # include <stdlib. h> int main () {int x, I = 0, j = 1; while (I <5) {x = 4 * j; for (I = 0; I <5; I ++) {if (x % 4! = 0) {break;} x = (x/4) * 5 + 1;} j ++;} printf ("% d \ n", x ); system ("pause"); return 0;}/* [Program 80] question: there are a bunch of peaches on the beach, five monkeys to score. The first monkey divided the peaches into five equal portions and one more portion. The monkey threw one more portion into the sea and took the other portion. The second monkey divides the remaining peaches into five equal portions and adds one more portion. It also throws one more portion into the sea and takes one portion, the third, fourth, and fifth monkeys did this. How many peaches did they have on the beach? This topic has a very simple mathematical practice. For more information, see the blog of this great God at http://blog.csdn.net/yupenghua/article/details/6803494#/#include <stdio. h> # include <stdlib. h> int main () {int x, I = 0, j = 1; while (I <5) {x = 4 * j; for (I = 0; I <5; I ++) {if (x % 4! = 0) {break;} x = (x/4) * 5 + 1;} j ++;} printf ("% d \ n", x ); system ("pause"); return 0;}/* [Program 81] Question: 809 *?? = 800 *?? + 9 *?? + 1 where ?? Two digits, 8 *?? The result is two digits, 9 *?? The result is 3 digits. Please ?? Represents two digits, and 809 *??. */# Include <stdio. h> # include <stdlib. h> int main () {int a, I; for (I = 10; I <100; I ++) if (8 * I <100 & 9 * I> 99 & 9 * I <1000) {printf ("?? Double digits: % d \ n ", I); break ;} printf ("809 * % d = 800 * % d + 9 * % d + 1 \ n", I, I); system ("pause "); return 0;}/* [Program 82] Question: Convert octal to decimal */# include <stdio. h> # include <stdlib. h> int main () {int n = 0, I = 0; char s [20]; printf ("enter an octal number: \ n "); gets (s); while (s [I]! = '\ 0') {n = n * 8 + s [I]-'0'; I ++ ;} printf ("convert the input octal number to % d in decimal format", n); system ("pause"); return 0 ;}/ * [Program 83] Question: calculate the number of odd numbers that can be composed of 0 to 7. Use 1, 3, 5, and 7 as the single position. 0 cannot be the highest position. One digit, two digits... 7-digit */# include <stdio. h> # include <stdlib. h> int factorial (int a, int B);/* calculate the factorial */int main () {int sum = 0, I; sum + = 4; /* the single digit cannot be processed as follows. The odd number of a single digit has four */for (I = 2; I <8; I ++) sum + = 4 * (factorial (7, I-1)-factorial (6, I-2); printf ("can make up % d odd \ n", sum ); system ("pause"); return 0;} int factorial (int a, int B) {int I, sum = 1; if (B <= 0) return 1; for (I = 0; I <B; I ++) sum * = (a-I); return sum;}/* [Program 84] Question: an even number can always represent the sum of two prime numbers. I'm going. What is this question? Do you want me to prove it? I really don't know how to prove it. Divide an even number into two prime numbers. */# Include <stdio. h> # include <stdlib. h> int Isprimer (unsigned int n); int main () {unsigned int n, I; do {printf ("enter an even number: \ n "); scanf ("% d", & n);} while (n % 2! = 0); for (I = 1; I <n; I ++) if (Isprimer (I) & Isprimer (n-I) break; printf ("even % d can be divided into two prime numbers: % d and \ n", n, I, n-I); system ("pause "); return 0;} int Isprimer (unsigned int n) {int I; if (n <4) return 1; else if (n % 2 = 0) return 0; else for (I = 3; I <sqrt (n) + 1; I ++) if (n % I = 0) return 0; return 1 ;} /* [Program 85] Question: judge whether a prime number can be divisible by a few 9! This question is to judge whether a prime number can divide a number of 9, right? That's how I understand it. Prime number is a number that cannot be divisible by one and itself */# include <stdio. h> # include <stdlib. h> int main () {int p, I; long int sum = 9; printf ("enter a prime number: \ n"); scanf ("% d ", & p); for (I = 1; I ++) if (sum % p = 0) break; else sum = sum * 10 + 9; printf ("Prime Number % d can divide % d into 9 Numbers % ld \ n", p, I, sum); system ("pause"); return 0 ;} /* [Program 86] Question: Is it so simple to connect two strings? Use strcat */# include <stdio. h> # include <stdlib. h> # include <string. h> # include <malloc. h> char * strconnect (char * str1, char * str2); int main () {char str1 [20], str2 [20]; char * str; puts ("enter two strings separated by carriage return:"); gets (str1); gets (str2); str = strconnect (str1, str2 ); puts ("connected string:"); puts (str); system ("pause"); return 0;} char * strconnect (char * str1, char * str2) {char * str; str = (char *) malloc (strlen (str1) + strlen (str2) + 1); Str [0] = '\ 0'; strcat (str, str1); strcat (str, str2); return str ;}/ * [Program 88] Question: read the integer of 7 Numbers (1-50). Each time a value is read, the program prints * of the number of values *. */# Include <stdio. h> # include <stdlib. h> int main () {int n, I, j; for (I = 0; I <7; I ++) {scanf ("% d", & n ); if (n> 50) {printf ("Please input again: \ n"); I --;} else {for (j = 0; j <n; j ++) printf ("*");} printf ("\ n");} system ("pause"); return 0;}/* [Program 89] Question: A company uses a public phone to transmit data. The data is a four-digit integer and encrypted during the transmission process. The encryption rules are as follows: add 5 to each number, then, replace the number with the remainder divided by 10, and then exchange the first and fourth digits, and the second and third digits. What is this question? What if I want to encrypt it? Or decryption? Then I will encrypt it! */# Include <stdio. h> # include <stdlib. h> int main () {int a, B, c, d, tem; char str [5]; printf ("enter a four-digit integer to be encrypted: \ n "); gets (str); a = (str [0]-'0' + 5) % 10; B = (str [1]-'0' + 5) % 10; c = (str [2]-'0' + 5) % 10; d = (str [3]-'0' + 5) % 10; tem = a; a = d; d = tem; tem = B; B = c; c = tem; tem = a * 1000 + B * 100 + c * 10 + d; printf ("the encrypted number is % d \ n", tem ); system ("pause"); return 0;}/* [Program 96] Question: I do not want to use the KMP algorithm to calculate the number of times a string neutron string appears */# include <stdio. h> # include <stdlib. h> # include <string. h> int Main () {int I, j, k, TLen, PLen, count = 0; char T [50], P [10]; printf ("enter two strings, separated by carriage return, the parent string is in front, and the Child string is in the back: \ n "); gets (T); gets (P); TLen = strlen (T ); PLen = strlen (P); for (I = 0; I <= TLen-PLen; I ++) {for (j = 0, k = I; j <PLen & P [j] = T [k]; j ++, k ++); if (j = PLen) count ++ ;} printf ("% d \ n", count); system ("pause"); return 0;}/* [Program 97] Question: Enter some characters from the keyboard, send them to the disk one by one until a # is entered. */# Include <stdio. h> # include <stdlib. h> int main () {FILE * fp = NULL; char filename [25]; char ch; printf ("enter the name of the FILE to be saved: \ n "); gets (filename); if (fp = fopen (filename, "w") = NULL) {printf ("error: cannot open file! \ N "); exit (0);} printf (" Now you can enter the characters you want to save, ending with #: \ n "); getchar (); while (ch = getchar ())! = '#') {Fputc (ch, fp) ;}fclose (fp); system ("pause"); return 0 ;}/ * [Program 98] Question: input a string from the keyboard, convert all lowercase letters into uppercase letters, and output the string to a disk file "test" for saving. Input string! End. */# Include <stdio. h> # include <stdlib. h> int main () {FILE * fp = NULL; char str [50]; int I, len; printf ("input a string: \ n "); gets (str); len = strlen (str); for (I = 0; I <len; I ++) {if (str [I] <= 'Z' & str [I]> = 'A') str [I]-= 32 ;} if (fp = fopen ("test", "w") = NULL) {printf ("error: cannot open file! \ N "); exit (0);} fprintf (fp," % s ", str); fclose (fp); system (" pause "); return 0 ;} /* [Program 99] Question: There are two disk files A and B, each containing A row of letters, the information in the two files must be combined (in alphabetical order ), output to a new file C. The question does not clarify whether the letters in file A and file B are sequential. I think there is no order! After merging, Bubble Sorting is good! */# Include <stdio. h> # include <stdlib. h> # include <string. h> int main () {FILE * fa, * fb, * fc; int I, j, k; char str [100], str1 [100]; char tem; if (fa = fopen ("A.txt", "r") = NULL) {printf ("error: cannot open A file! \ N "); exit (0);} fgets (str, 99, fa); fclose (fa); if (fb = fopen (" B .txt ", "r") = NULL) {printf ("error: cannot open B file! \ N "); exit (0);} fgets (str1, 100, fb); fclose (fb); strcat (str, str1); for (I = strlen (str) -1; I> 1; I --) for (j = 0; j <I; j ++) if (str [j]> str [j + 1]) {tem = str [j]; str [j] = str [j + 1]; str [j + 1] = tem ;} if (fc = fopen ("C.txt", "w") = NULL) {printf ("error: cannot open C file! \ N "); exit (0);} fputs (str, fc); fclose (fc); system (" pause "); return 0 ;} /* [program 100] Subject: five students have scores for each of the three courses. Enter the above data (including student ID, name, and score for the three courses) on the keyboard ), calculate the average score. The original data and the calculated average score are stored in the disk file "stud. */# Include <stdio. h> # include <stdlib. h> typedef struct {int ID; int math; int English; int C; int avargrade; char name [20] ;} Stu; int main () {FILE * fp; stu stu [5]; int I, avargrade = 0; printf ("Enter the student ID, name, and score of five students: \ n "); for (I = 0; I <5; I ++) {scanf ("% d % s % d", & (stu [I]. ID), stu [I]. name, & (stu [I]. math), & (stu [I]. english), & (stu [I]. c); stu [I]. avargrade = (stu [I]. math + stu [I]. english + stu [I]. c)/3;} if (fp = fopen ("stud", "w ")) = NULL) {printf ("error: cannot open file! \ N "); exit (0) ;}for (I = 0; I <5; I ++) fprintf (fp, "% d % s % d \ n", stu [I]. ID, stu [I]. name, stu [I]. math, stu [I]. english, stu [I]. c, stu [I]. avargrade); fclose (fp); system ("pause"); return 0 ;}

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.