- What is arithmetic? What is relational operation? What is logical operation?
Answer: arithmetic operation: addition, subtraction, multiplication and division; Relational operation: > < =; Logical operation: and or not;
- How to express "true" and "false" in C language? How does the system judge the "true" and "false" of a quantity?
Answer: 0 is false and non-zero is true
- Write the values of the following logical expressions. Let a=3,b=4,c=5.
(1)a + b > c && b == c
(2)a || b + c && b - c
(3)!(a > b) && !c || 1
(4)!(x = a) && (y = b) && 0
(5)!(a + b) + c - 1 && b + c / 2
Answer:
(1) Combine 0 directly from left to right
(2) From left to right, a|b + C is 6, then b-c is - 1, and finally 6& & - 1 is 1
(3)1
(4)0
(5)1
Summary:
- The essence of 1 & & 1 + 2 is equivalent to 1 & & (1 + 2) being 1
- There are three integers a, B and C, which are input by the keyboard and output the largest number.
Algorithm idea:
1. The input value of the function is three numbers, and the return value is the largest
Code implementation:
#include<stdio.h> int main(int argc, char const *argv[]) { int max(int a,int b,int c); int a,b,c; printf("please input three numbers:"); scanf("%d%d%d",&a,&b,&c); printf("the max number is:%d",max(a,b,c)); return 0; } int max(int a,int b,int c) { int max=a ; if (max<b) { max=b; } if (max<c) { max=c; } return max; }
result:
5. Input a positive number less than 1000 from the keyboard and output its square root (if the square root is not an integer, output its integer part). It is required to check whether the data is a positive number less than 1000 after entering it. If not, re-enter is required.
Algorithm idea:
1. Enter a number
2. If the number is > 1000, continue to enter
3. If it is less than 1000, call sqrt function to output its value
Code result:
#include<stdio.h> #include<math.h> int main(int argc, char const *argv[]) { double number; printf("please input a number:"); scanf("%lf",&number); while (1) { if (number<1000.0) { printf("the result is :%.0lf",sqrt(number)); break; } else { printf("please input a number:"); scanf("%lf",&number); } } return 0; }
Algorithm idea:
Three selection statements correspond to functions of three intervals
Code implementation:
#include<stdio.h> int main(int argc, char const *argv[]) { int x,y; printf("please input a number:"); scanf("%d",&x); if (x<1) { y=x; }else if (x<=10) { y=2*x-1; }else { y=3*x-11; } printf("the result is : %d",y); return 0; }
#include <stdio.h> int main() { int x, y; printf("enter x:"); scanf("%d", &x); y=-1; if (x != 0) if (x > 0) y=1; else y=0; printf("x=%d,y=%d\n", x, y); return 0; }
#include <stdio.h> int main() { int x, y; printf("enter x:"); scanf("%d", &x); y=0; if (x >= 0) if (x > 0) y=1; else y=-1; printf("x=%d,y=%d\n", x, y); return 0; }
answer:
The first program inputs x=-1 and y is 0. It does not meet the requirements of the topic piecewise function, and the second one meets.
Summary:
1. Of this question if...else It's easy to make mistakes, else It's the one closest to him if. If there are two if,Except for the second one if cover{ }Enclose, otherwise the second one if hinder else It's the second one if,Not the first, although there is a space in front
- Give the 100 point system score, and output the score grades' A ',' B ',' C ','D' and 'E'. More than 90 points are 'A', 80 ~ 89 points are 'B', 70 ~ 79 points are 'C', 60 ~ 69 points are'D ', and less than 60 points are' E '.
Algorithm idea:
Enter a score and constantly use if else to judge the interval
Code implementation:
#include<stdio.h> int main(int argc, char const *argv[]) { void printGrade(int grade); int grade; scanf("%d",&grade); printGrade(grade); return 0; } void printGrade(int grade) { if (grade>=90&&grade<=100) { printf("A"); }else if (grade>=80) { printf("B"); }else if (grade>=70) { printf("C"); }else if (grade>=60) { printf("D"); }else{ printf("E"); } }
result:
Summary:
- It is easy to think of using the switch function, but the case after the switch function can only be a constant and can not be replaced by unknowns and expressions, so you must use if else to select the structure
- Give a positive integer with no more than 5 digits. Requirements: ① find out how many digits it is; ② Output each digit separately; ③ Output the numbers in reverse order. For example, if the original number is 321, 123 should be output.
Algorithm idea:
1. The key to this question is to use number%10000 to get the lower four digits, number%1000 to get the lower three digits, and number%100 to get the lower two digits
2.(number%10000)/1000 takes the fourth digit based on the lower four digits, and three digits / 100 takes the third digit
Code implementation:
#include<stdio.h> int main(int argc, char const *argv[]) { int number; void numberOfQuantity(int number); void printEveryNumber(int number); void printEveryNumberReversed(int number); printf("please input a number:"); scanf("%d",&number); numberOfQuantity(number); printf("\n"); printEveryNumber(number); printf("\n"); printEveryNumberReversed(number); return 0; } void numberOfQuantity(int number)//Number of judgments { int first,second,third,fourth,fifth; if (number>=10000) { printf("five numbers"); }else if (number>=1000) { printf("four numbers"); }else if (number>=100) { printf("three numbers"); }else if (number>=10) { printf("two numbers"); }else if (number>=1) { printf("one numbers"); }else { printf("not in area!"); } } void printEveryNumber(int number)//Output each digit {//Don't explore the laws by yourself. Directly summarize the laws of predecessors if (number/10000>0) { printf("%d ",number/10000); } if (number/1000>0) { printf("%d ",(number%10000)/1000); } if (number/100>0) { printf("%d ",(number%1000)/100); } if (number/10>0) { printf("%d ",(number%100)/10); } if (number/1>0) { printf("%d ",number%10); } } void printEveryNumberReversed(int number)//Output each digit in reverse order { if (number/1>0) { printf("%d ",number%10); } if (number/10>0) { printf("%d ",(number%100)/10); } if (number/100>0) { printf("%d ",(number%1000)/100); } if (number/1000>0) { printf("%d ",(number%10000)/1000); } if (number/10000>0) { printf("%d ",number/10000); } }
Summary and error:
- This question is not solid enough or difficult for Xiaobai. We have to find out the usage of the% symbol and / or the number of digits. It is suggested that we can't think of a direct understanding and use the answer.
10. The bonus paid by the enterprise shall be deducted according to the profit. If the profit I is less than or equal to 100000 yuan, the bonus can be deducted by 10%; When the profit is higher than 100000 yuan and lower than 200000 yuan (100000 < I ≤ 200000), the part lower than 100000 yuan shall be deducted by 10%, and the part higher than 100000 yuan can be deducted by 7.5% 5%; When 200000 < I ≤ 400000, the part less than 200000 yuan shall still be deducted according to the above methods (the same below). 5% commission for the part higher than 200000 yuan; When 400000 < < I ≤ 600000 yuan, the part higher than 400000 yuan shall be deducted by 3%; When 600000 < 1 ≤ 1000000, the part higher than 600000 yuan shall be deducted by 1.5%; 1> In case of 1000000, 1% commission shall be charged for the part exceeding 1000000 yuan. Enter the profit I of the current month from the keyboard to calculate the total amount of bonus payable. Requirements: (1) write the program with if statement. (2) Write programs using switch statements.
(1) Writing programs using if statements
(2) Write programs using switch statements.
Algorithm idea:
1. Enter the profit and return the profit according to the corresponding requirements
Code implementation:
#include<stdio.h> int main(int argc, char const *argv[]) { double returnWard1(int profit); double returnWard2(int profit); int profit; double ward1,ward2; printf("please input profit:"); scanf("%d",&profit); ward1=returnWard1(profit); ward2=returnWard2(profit); printf("%lf %lf",ward1,ward2); return 0; } double returnWard1(int profit) { if (profit<100000) { return profit*0.1; }else if (profit<=200000) { return 100000*0.1+(profit-100000)*0.075; }else if (profit<400000) { return 200000*0.1+(profit-200000)*0.05; }else if (profit<=600000) { return 400000*0.1+(profit-400000)*0.03; }else if (profit<=1000000) { return 600000*0.015+(profit-600000)*0.015; }else if (profit>1000000) { return 1000000*0.01+(profit-1000000)*0.01; } } double returnWard2(int profit) { int grade; grade=profit/100000; switch (grade) { case 0: return profit*0.1; break; case 1: return 100000*0.1+(profit-100000)*0.075; break; case 2: case 3: return 200000*0.1+(profit-200000)*0.05; break; case 4: case 5: return 400000*0.1+(profit-400000)*0.03; break; case 6: case 7: case 8: case 9: return 600000*0.015+(profit-600000)*0.015; break; default: return 1000000*0.01+(profit-1000000)*0.01; break; } }
Summary and experience:
- When using switch(int profit), if you need to select a range, you can use profit divided by 100000 to obtain the corresponding level and determine the range in cooperation with break.
11. Input 4 integers and output them in descending order.
Algorithm idea:
1. Use an array to compare the size of two adjacent arrays and sort the array.
Code implementation:
#include<stdio.h> int main(int argc, char const *argv[]) { void printNumberOrderly(int a[]); int a[4]; printf("please input four numbers:"); for (int i = 0; i < 4; i++) { scanf("%d",&a[i]); } printNumberOrderly(a); return 0; } void printNumberOrderly(int a[]) { int temp=a[0]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { if (a[j]>a[j+1]) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } } } for (int i = 0; i < 4; i++) { printf("%d ",a[i]); } }
Summary and Thinking:
- Bubble sorting is used, which is not familiar
- You can use four variables a, b,c and d without array. First, take a as the smallest and compare it with the next three. If there is one smaller than a, exchange the position, b,c and so on.
12. There are 4 round towers with center of (2,2), (- 2,2), (- 2, - 2), (2, - 2) and radius of 1, as shown in the figure. The height of the four towers is 10m, and there are no buildings outside the towers. Now enter the coordinates of any point to calculate the building height of the point (the height outside the tower is zero).
Algorithm idea:
1. As long as it is within the circle of the first quadrant, the height is 10, otherwise it is 0
Code implementation:
#include<stdio.h> #include<math.h> int main(int argc, char const *argv[]) { int judgeCircleArea(int x ,int y ); int x,y,height; printf("please input x and y:"); scanf("%d%d",&x,&y); height=judgeCircleArea(x,y); printf("height is :%d",height); return 0; } int judgeCircleArea(int x ,int y ) { int flag,height; if (x*x+y*y-4*fabs(x)-4*fabs(y)+7<=1) { flag=1; } else { flag=0; } if (flag) { height=10; } else { height=0; } return height; }
Summary:
- After analyzing the mathematical model of this problem, it is still relatively simple to implement it in c language.