catalogue
2. Chicken and rabbit in the same cage
Input and output of single character:
The first three questions in the first game of SZTU-ACM novice weekly competition
Review and practice with the help of Chapter 1 of introduction to algorithm competition (Second Edition)
About floating point numbers
Integer values are output with% d and real numbers with% f.
Integer / integer = integer, floating point / floating point = floating point.
The output of double must use% f, while the input needs to use% lf
5-0.1 is integer - floating point number = floating point number (but the statement is not accurate. Integer first "becomes" floating point number, and then floating point number - floating point number = floating point number.)
Let's take an example
#include<stdio.h>
int main()
{
double a;
a = 5*(34 - 32) /9;
printf("%.1f",a) ;
return 0;
}
Judging only by code, the output value is 1.0
Because the number to the right of the equal sign is int, even if a is double, the calculation of int has been completed and is 1. After double, it is 1.0
To get the result of 1.1, just change an integer to the left of the equals sign into a floating-point number, such as 5 to 5.0;
1. Three digit inversion
#include<stdio.h> int main() { int n, m; scanf("%d", &n); m = (n%10)*100 + (n/10%10)*10 + (n/100); printf("%03d\n", m); return 0; }
(to enable input 250 to output 025, change the output format to% 03d)
2. Chicken and rabbit in the same cage
#include<stdio.h> int main() { int a,b,n,m; scanf("%d",&n,&m); a = (4*n-m)/2; b = n-a; if(m%2==1||a<0||b<0){ printf("no answer"); }else{ printf("%d %d",a,b); } return 0; }
3. Three digit sorting
#include<stdio.h> int main() { int a, b, c, t; scanf("%d%d%d", &a, &b, &c); if(a > b) { t = a; a = b; b = t; } //After execution, a ≤ b if(a > c) { t = a; a = c; c = t; } //After execution, a ≤ c and a ≤ b are still valid if(b > c) { t = b; b = c; c = t; } printf("%d %d %d\n", a, b, c); return 0; }
Use this method to complete the problem of putting boxes in homework:
#include<stdio.h> int main() { int n; int a,b,c,d,e,f; scanf("%d",&n); int i,t; for(i=0;i<n;i++){ scanf("%d %d %d",&a,&b,&c); if(a>b){ t = a; a = b; b = t; } if(a>c){ t = c; c = a; a = t; } if(b>c){ t = c; c = b; b = t; } scanf("%d %d %d",&d,&e,&f); if(d>e){ t = d; d = e; e = t; } if(d>f){ t = f; f = d; d = t; } if(e>f){ t = f; f = e; e = t; } if((a<=d&&b<=e&&c<=f)||(d<=a&&e<=b&&f<=c)){ printf("yes\n"); }else{ printf("no\n"); } } return 0; }
If you don't only have three digits to sort, you need to use the array:
#include <stdio.h> int main(void) { int a[1001]; int n, i, j, t; scanf("%d", &n); //n is the number of numbers to sort //Enter the number to sort for(i = 0; i < n; i++) { scanf("%d", &a[i]); } //Next, sort for(i = 0; i < n - 1;i++) //N numbers, n-1 times in total { //After n-1 numbers are arranged, the first number must have returned //The maximum (ascending) or minimum (descending) is placed last each time for(j = 0; j < n - i - 1; j++) { if(a[j] > a[j + 1]) //Exchange for each bubble { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } for(j = 0; j < n; ++j) { printf("%-5d ", a[j]); } return 0; }
4. Discount
A dress is 95 yuan. If you spend more than 300 yuan, you can get a 15% discount. Enter the number of clothes purchased, and output the amount to be paid (unit: yuan), with two decimal places:
#include<stdio.h> int main() { const int price=95; int number; scanf("%d",&number); double sum; sum = price*number; if(sum>=300){ sum *=0.75; } printf("%.2f",sum); }
5. Form a triangle
Enter the length value of the three sides of the triangle (all positive integers) to judge whether it can be the length of the three sides of a right triangle. If yes, yes is output; if not, no is output. If the triangle cannot be formed at all, output not a triangle.
#include<stdio.h> int main() { int a,b,c; scanf("%d %d %d",&a,&b,&c); if(a+b>c&&a+c>b&&b+c>a) { if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) { printf("yes"); } else { printf("no"); } } else { printf("not a triangle"); } return 0; }
6. Is it a leap year
Enter the year to judge whether it is a leap year. If yes, yes is output; otherwise, no is output
Character variable
Define character variable op, which is used to store character data, such as:
Character constants: '+', '-', '*', '/'
Character variables: op,a,b
char op1,op2;
op1='A';
op2=op1;
Input and output of single character:
Note: a space is a character!!!!!
Otherwise, the output will look like this:
Next, let's look at the problem in the homework
#include<stdio.h> int main() { int a,b; char c; double m=0,s; scanf("%d %d %c",&a,&b,&c); if(b == 90 ) m = 6.95; if(b == 93 ) m = 7.44; if(b == 97 ) m = 7.93; if(c=='m'){ s = a*m*0.95; }else{ s = a*m*0.97; } printf("%.2f",s); return 0; }
The first three questions in the first game of SZTU-ACM novice weekly competition
1.log2(n):
#include<stdio.h> #include<math.h> int main() { int n; scanf("%d",&n); if(n>=1&&n<=pow(10,18)){ int i,k; for(i=0;;i++){ if(pow(2,i)<=n){ k = i; } else{ break; } } printf("%d\n",k); } return 0; }
2. How to slip faster
#include<stdio.h> #include<math.h> int main() { int r,x,y; scanf("%d %d %d",&r,&x,&y); if{ double d=sqrt(pow(x,2)+pow(y,2)); int i; for(i=0;i*r<d+1;i++){ } printf("%d",i); }else{ printf("no"); } return 0; }
3. Two bread with cheese
#include<stdio.h> int main() { int n=0; scanf("%d\n",&n); int a[n]; int b[n]; int x=0; int t=0; int maxa=0,maxb=0; int i; for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=0;i<n;i++){ if(a[i]>maxa){ maxa=a[i]; } } for(i=0;i<n;i++){ scanf("%d",&b[i]); } for(i=0;i<n;i++){ if(a[i]>maxb){ maxb=b[i]; } } for(x=maxa;x<=maxb;x++){ t++; } printf("%d",t); return 0; }
The remaining two questions are too difficult to be typed independently, so they are not written in this blog post.