Hello everyone, I am deep fish~
Table of contents
1. Calculate the grade point average
8. Determine whether it is a vowel or a consonant
&& Summary:
Foreword:
Today is the fourth day of updating the topic. Although the topic is not difficult, I hope that we can read this article with an empty cup mentality. After calming down, we can still learn a lot of new knowledge, come on! ! !
1. Calculate the grade point average
topic:
Solution: For this question, please note that the last line is sum/5.0, 5 must be learned as a decimal, or define sum as a double type
#include <stdio.h>
int main()
{
//输入
int i=0;
int num=0;
int sum=0;
for(i=0;i<5;i++)
{
scanf("%d",&num);
sum+=num;
}
//计算并输出
printf("%.1lf",sum/5.0);
return 0;
}
2. Base A+B
topic:
answer:
%x is the hexadecimal data format
%o is an octal data format
#include <stdio.h>
int main()
{
int a,b=0;
scanf("%x %o",&a,&b);
printf("%d\n",a+b);
return 0;
}
3. Online shopping
topic:
Solution: For this question, we should learn to simplify the code continuously. This question is the result of simplification one by one. This process is the same as merging similar items in mathematics.
The flag indicates whether there is a coupon, directly -50*flag, which is very concise
It should be noted that if the price is less than 0 , you need to print 0.00 directly
#include<stdio.h>
int main()
{
double m,price = 0;//m表示原价
int month, day, flag = 0;//flag表示是否有优惠价
//输入
scanf("%lf %d %d %d", &m, &month, &day, &flag);
//计算
if (month == 11 && day == 11)
price = 0.7 * m - flag * 50;
else if (month == 12 && day == 12)
price = 0.8 * m - flag * 50;
//输出
if (price <= 0)
printf("0.00");
else
printf("%.2lf", price);
return 0;
}
4. Compete for the top five
Topic link: Compete for the top five
topic:
Solution: This question needs to learn bubble sorting
The idea of bubble sorting: two adjacent elements are compared, and if possible, need to be exchanged (nested loop)
The first is 9 for the first bubble sort
9 8 7 6 5 4 3 2 1 0
8 9 7 6 5 4 3 2 1 0
8 7 9 6 5 4 3 2 1 0
...
8 7 6 5 4 3 2 1 0 9 //This is a bubble sort
Then there is 8 for the next bubble sort, there are n-1 times in total, n is the number of numbers
Then the idea of this question is to enter the grades first, then perform bubble sorting from large to small, and finally print the first five
#include <stdio.h>
int main()
{
//输入
int n,tmp=0;//n是个数
int arr[50]={0};//存放成绩
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//冒泡排序
for(int j=0;j<n-1;j++)//趟数
{
//一趟冒泡排序要进行多少对元素的比较
for(int i=0;i<n-1-j;i++)
{
if(arr[i]<arr[i+1])
{
tmp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=tmp;
}
}
}
//输出
for(int i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
Solution 2: Use the qsort function to sort directly
The qsort library function can automatically sort, but this function needs to input 4 parameters, and the header file #include<stdlib.h> is required before use
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );
qsort (the initial position of the sorted array, the number of elements of the array to be sorted, the bytes occupied by an element, the comparison function)
The focus is on the last parameter, the comparison function:
If you want ascending order (elem1<elem2), return < 0, then return elem1-elem2
If you want to descend (elem1>elem2), then return > 0, you must return elem2-elem1
#include <stdio.h>
#include<stdlib.h>
int cmp_int(const void*e1,const void*e2)//这个函数的写法也要学会
{
return *(int *)e2-*(int *)e1;
}
int main()
{
//输入
int n,tmp=0;//n是个数
int arr[50]={0};//存放成绩
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
qsort(arr, n, 4, cmp_int);
//输出
for(int i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
5. Run for president
Topic Link: Running for President
topic:
Solution 1: The idea of this question: gets() directly enters the character array, and then starts traversing the buf array to count the number of A and B
In this question, you need to learn how to get () to directly enter the string
Note: When using gets(), the system will take the last "knocked" line break out of the buffer and discard it, so there will be no \n left in the buffer
#include <stdio.h>
int main()
{
char buf[100]={0};
//输入
gets(buf);
//统计
int count_a=0;
int count_b=0;
int i=0;
while(buf[i]!='0')
{
if(buf[i]=='A')
count_a++;
if(buf[i]=='B')
count_b++;
i++;
}
if(count_a>count_b)
printf("A");
else if(count_a==count_b)
printf("E");
else
printf("B");
return 0;
}
Solution 2: Use only one variable flag, add 1 if it is A, subtract 1 if it is B, and finally check whether the flag is greater than or less than or equal to 0
#include <stdio.h>
int main()
{
char buf[100]={0};
//输入
gets(buf);
//统计
int flag=0;
int i=0;
while(buf[i]!='0')
{
if(buf[i]=='A')
flag++;
if(buf[i]=='B')
flag--;
i++;
}
if(flag>0)
printf("A");
else if(flag==0)
printf("E");
else
printf("B");
return 0;
}
Solution 3: Another way to enter a string (ch=getchar())! ='0')
#include <stdio.h>
int main()
{
int flag=0;
char ch=0;
while(((ch=getchar())!='0')&&ch!=EOF)
{
if(ch=='A')
flag++;
else if(ch=='B')
flag--;
}
if(flag>0)
printf("A");
else if(flag==0)
printf("E");
else
printf("B");
return 0;
}
6. Perfect score
topic:
Solution: Here we look at another method for multiple sets of input:
while(~scanf("%d",&a)) means the end of EOF
~ is an operator in C language - bitwise negation
EOF represents -1 ( the integer is stored in memory as a complement )
10000000000000000000000000000001 original code
11111111111111111111111111111111110 Inverse
1111111111111111111111111111111111 complement code This also represents -1 (that is, EOF)
Then ~EOF is 00000000000000000000000000000000, which is 0, 0 is false, do not enter the loop
#include <stdio.h>
int main()
{
int n=0;
while(~scanf("%d",&n))
//while((scanf("%d",&n))!=EOF)也可以这样写
{
if(n>=90)
printf("Perfect\n");
}
return 0;
}
7. Highest score
topic:
Solution 1: Idea: Finding the largest of three numbers only needs to be compared twice
The conditional operator exp1 is used here? exp2: exp3
Explanation: If condition 1 is met, the output value is exp2; if condition 1 is not met, the output value is exp3
#include <stdio.h>
int main()
{
int n1=0;
int n2=0;
int n3=0;
while(~scanf("%d%d%d",&n1,&n2,&n3));
{
int max=n1>n2?n1:n2;
max=max>n3?max:n3;
printf("%d",max);
}
return 0;
}
Solution 2 : The idea of this approach: after entering the comparison, first let the first number be the maximum value, and then start the comparison from the second number. If the second number is large, let the second number be the maximum value. By analogy, max is compared with the third number
#include <stdio.h>
int main()
{
int num[3]={0};
while(~scanf("%d %d %d",&num[0],&num[1],&num[2]));
{
int max=num[0];
int i=0;
for(i=1;i<3;i++)
{
if(num[i]>max)
max=num[i];
}
printf("%d",max);
}
return 0;
}
Solution 3 : Directly define max as INT_MIN , warm reminder: before using INT_MIN, declare: #include<limits.h>
#include<limits.h>
int main()
{
int max = INT_MIN;
int num = 0;
for (int i = 1; i <= 3; i++)
{
scanf("%d", &num);
if (num > max)
max = num;
}
printf("%d", num);
return 0;
}
8. Determine whether it is a vowel or a consonant
Topic link: Judging whether it is a vowel or a consonant
topic:
Solution 1: Idea: first put the vowels into a character array , then input the letters, traverse the array to see if there are any similar ones, if there are, directly output Vowel and add break (jump out of the entire loop), if i=10 (after finishing The whole cycle) or nothing, directly output Consonant
#include <stdio.h>
int main()
{
char ch=0;
char vowel[]="AaEeIiOoUu";
while((ch=getchar())!=EOF)
{
getchar();//处理\n
int i=0;
for( i=0;i<10;i++)
{
if(vowel[i]==ch)
{
printf("Vowel\n");
break;
}
}
if(i==10)
printf("Consonant\n");
}
return 0;
}
Solution 2: Here is the strchr function to find letters
The strchr function is used to determine whether ch appears in vowel in the string
If present, returns the address in the vowel string
If not present, returns NULL (null pointer)
Follow this function to lead the file #include<string.h>
#include <stdio.h>
#include<string.h>
int main()
{
char ch=0;
char vowel[]="AaEeIiOoUu";
while((ch=getchar())!=EOF)
{
getchar();//处理\n
if(strchr(vowel,ch))
{
printf("Vowel\n");
}
else
{
printf("Consonant\n");
}
}
return 0;
}
Solution 3: while(scanf("%c",&ch)!=EOF) can also be input like this
Note that if you add a space in front of %c, all the preceding blank characters will be eliminated, and then a character will be read. The advantage of this is that you don’t need to add another sentence of getchar() to absorb\n
Or: while(scanf("%c\n",&ch)!=EOF) plus a \n
#include <stdio.h>
#include<string.h>
int main()
{
char ch=0;
char vowel[]="AaEeIiOoUu";
while(scanf(" %c",&ch)!=EOF)
{
if(strchr(vowel,ch))
{
printf("Vowel\n");
}
else
{
printf("Consonant\n");
}
}
return 0;
}
9. Letter case conversion
Topic link: letter case conversion
topic:
Solution 1: Normal version: One conclusion to know is that the ASCII value of uppercase letters and lowercase letters differs by 32 (the ASCII value of A is 65, and the ASCII value of a is 72)
#include <stdio.h>
int main()
{
char ch='0';
while((scanf("%c",&ch))!=EOF)
{
if(ch>='a'&&ch<='z')
ch-=32;
else if(ch>='A'&&ch<='Z')
ch+=32;
getchar();
printf("%c\n",ch);
}
return 0;
}
Solution 2: Here are four functions
isupper(ch) : Determine whether a letter is an uppercase letter
islower(ch) : Determine whether a letter is a lowercase letter
toupper (ch) : Convert a lowercase letter to uppercase
tolower (ch) : Convert an uppercase letter to lowercase
These functions require a header file: #include<ctype.h>
#include <stdio.h>
#include<ctype.h>
int main()
{
char ch=0;
while(~scanf(" %c",&ch))
{
if(isupper(ch))
{
printf("%c\n",tolower(ch));
}
else if(islower(ch))
{
printf("%c\n",toupper(ch));
}
}
return 0;
}
10. Triangle Judgment
topic:
Solution: Conditions for forming a triangle: the sum of any two sides is greater than the third side (so all three cases must be written)
Idea: If it is a triangle, further judge what kind of triangle it is. If it is not a triangle, it will be directly output; if it is a triangle, then the three sides are equal, and it is an equilateral triangle (note that == is a binocular operator, you cannot write three variables equal, but write as a==b&&b==c )
Then if it is not equilateral, it may be that the two sides are equal. At this time, there is no need to write the third side is not equal, because if the third side is equal, the first judgment statement will be output directly, and the last is the else ordinary triangle
#include <stdio.h>
int main()
{
int a,b,c=0;
while(~scanf("%d %d %d",&a,&b,&c))
{
if(a+b>c&&a+c>b&&b+c>a)
{
if(a==b&&b==c)
printf("Equilateral triangle!\n");
else if (a==b||a==c||b==c)
printf("Isosceles triangle!\n");
else
printf("Ordinary triangle!\n");
}
else
printf("Not a triangle!\n");
}
return 0;
}
Summarize:
1. %x is a hexadecimal data format %o is an octal data format
2. Learn to keep streamlining your own code instead of just writing it out. Strictly ask yourself eg: flag indicates whether there is a coupon, directly -50*flag, which is very concise
3. The idea of bubble sorting : compare two adjacent elements, and if possible, need to exchange (nested loops) The outer loop represents the number of times, and the inner loop represents how many elements to perform in a bubble sort Comparison
4. qsort function : sorting function
Header file: #include<stdlib.h>
Parameters: qsort (the initial position of the array to be sorted, the number of elements in the array to be sorted, the bytes occupied by an element, and the comparison function)
Ascending order : return<0 means return elem1-elem2
Descending order : return>0 means return elem2-elem1
How to write a custom pointer function :
int cmp_int(const void*e1,const void*e2)
{
return *(int *)e2-*(int *)e1;
}
5.gets () input string directly: no \n will be left in the buffer
6. Multiple input of common variables: while(~scanf("%d",&a))
Multi-group input of characters: while(~scanf("%c\n",&ch)) so don't write getchar() to absorb\n
7. Conditional operator exp1? exp2: exp3 (if condition 1 is met, the output value is exp2, if condition 1 is not met, the output value is exp3)
8. The strchr function is used to determine whether a character appears in a string
If present, returns the address in the string
If not present, returns NULL (null pointer)
Follow this function to lead the file #include<string.h>
9. The ASCII value difference between uppercase and lowercase letters is 32 (the ASCII value of A is 65 , and the ASCII value of a is 72 )
10. Functions for judging case and converting uppercase and lowercase letters
isupper(ch) : Determine whether a letter is an uppercase letter
islower(ch) : Determine whether a letter is a lowercase letter
toupper (ch) : Convert a lowercase letter to uppercase
tolower (ch): Convert an uppercase letter to lowercase
These functions require a header file: #include<ctype.h>
11. Note that == is a binocular operator, you cannot write three variables equal , but write a==b&&b==c
Conclusion: There is still glory in the other side, and the youth is not afraid of the long years
I feel that the author's writing is not bad, or when I have gained a little bit, I would like to trouble the iron juice to move my hands and give me a one-click three-link, thank you very much!