Several basic C language questions

1. Recursive factorial

Analysis : n!=1*2*...*n
Then we have two methods

  • Recursive
    thinking : n!=n*(n-1)!
    define the function f (x) according to the law of factorial , and make x*f(x-1)the value returned by the function until n = 1. Find the result after the function call itself.
    Code :

      #include<stdio.h>
      int cnt(int n);//调用声明函数
      int main()
      {
      	int n;
      	scanf("%d",&n);
      	printf("%d",cnt(n));
      	return 0;
      }
    
      int cnt(int n)//定义声明函数
      {
      	if(n>0)
      	n*=cnt(n-1);
      	else n=1;
      	return n;
      }
    
  • Circular
    idea : Because n!=n*(n-1)*...*1, it starts from a = 1 and multiplies it by a after +1.
    Code :

      #include<stdio.h>
      int main()
      {
      	int n;
      	scanf("%d",&n);
      	int a=1;	//a要初始化为1 
      	for(int i=1;i<=n;i++) //注意i应从1取到n,因此判断条件为<=
      	a=a*i;
      	printf("%d",a);
      	return 0;
      }
    

2. Subsystem conversion

Title : Give a 100-point score and ask for the output grade 'A' (90 points and above), 'B' (80 to 89 points), 'C' (70 to 79 points), 'D' (60 to 69 points), 'E' (60 points or less).
Input format
Percentage scores, integers, for example, 100, 89
Output format
Grades scores, such as A, B, C, etc.
eg85 B
Analysis : This question is very simple. When I reviewed the rewriting, I found it was more concise than before. Quite a lot, because the idea of ​​mapping is used, the score is reduced from 5 categories to 3 categories: 100 points, 60 to 99 points, 60 points or less.
Code :

#include<stdio.h>
int main()
{
	int s;
	scanf("%d",&s);
	s/=10;//成绩映射为0-10 
	if(s==10)//10(满分)的情况 
	printf("A");
	else if(s>=6)//将6到9转换为字母等级 
	printf("%c",('E'-s+5));
	else printf("E");//小于6(不及格)的情况
	return 0;
}

3. Determine whether it is a prime number (<1000)

Analysis : Prime numbers are numbers that can only be divided by 1 and themselves, so we can loop through all the numbers smaller than 2 from the beginning.
Optimization :
The multiple of 2 is not prime, so first determine whether it is a multiple of 2.
When judging whether a certain number is a factor, you can determine whether multiple numbers are a factor together.
For example, to determine whether 11 is a prime number, you need to judge from 2 to 9, and 2 is not a factor, and you can also determine that 6 to 10 is also not a factor. Because 2 times these numbers are greater than 11, it is impossible to have a number greater than 2 and 6 to 10 Multiplied by 11.
Code :

#include<stdio.h>
int main()
{
	int n;
	int r=1; //用r记录是否为素数。1为素数0非素数 
	scanf("%d",&n);
	//大于2的偶数非素数
	if((n/2>1)&&(n%2==0)) 
	r=0;
	//判断是否为素数
	for(int i=3;r==1&&i*i<=n;i+=2) //更新直接+2,因到只用判断奇数
	{
		if(n%i==0) 
		{
			r=0;
			break; //跳出for循环
		}
	}
	//通过r的值(是否为素数)进行输出
	if(r==0)
	printf("NO");
	else
	printf("YES");
	return 0;
}

4. Today is the day

Title : Enter three integers for the year, month, and day, and the output is the day of the year
eg: input: 1990 9 20 output: 263

#include<stdio.h>
int main()
{
	int y,m,d;
	scanf("%d%d%d",&y,&m,&d);
	int r=d; //结果初始化为天数d
	for(int i=1;i<m;i++)//逐月增加天数:从1月加到m-1月 
	{
		switch(i) 
		{
			case 2:
			r+=28;
			break;
			case 4:
			case 6:
			case 9:
			case 11:
			r+=30;
			break;
			default://31天的月份较多放在default中 
			r+=31;			
		}
	}
	//判断平年闰年,若为闰年2月有29天,再加1
	if((y%400==0)||((y%4==0)&&(y%100!=0))) 
	r++;
	printf("%d",r);
	return 0;
}

5. Output as required

Title : Write a program, the input is three lines, the first line is integer a, the second line is character c, and the third line is integer b. (0 <a, b <100000).
There are four lines of output. The first line sequentially outputs a, b, c, and the three values ​​are separated by a space; the second sequentially outputs the values ​​of a + b, ab, a * b, a / b, and a% b, between each value Separated by a space; the third line outputs the ratio of a and b (floating point, accurate to two decimal places); the fourth line outputs the percentage rate of a and b (floating point, accurate to two decimal places). See the output sample for the specific format.
Input sample
12
b
234
Output sample
12 234 b
246 -222 2808 0 12
The ratio of 12 versus 234 is 0.05.
The ratio of 12/234 is 5.13%.
Code :

#include<stdio.h>
 int main()
 {
	//输出控制字符:加\;输出%:%% 
	long long a,b; //整型出现乘法:用lld
	char c;
	scanf("%lld",&a);
	scanf("\n%c",&c);
//scanf:1.按格式要求。2.输入非c时自动忽略前导空白符号,不用加\n。
//3.输入为c时会读入空白符号->如何解决上一行的换行符残留:加\n或用getchar 
	scanf("%lld",&b);
	printf("%lld %lld %c\n",a,b,c);
	printf("%lld %lld %lld %lld %lld\n",a+b,a-b,a*b,a/b,a%b);
	printf("The ratio of %lld versus %lld is %.2lf.\n",a,b,(double)a/b);
	printf("The ratio of %lld / %lld is %.2lf%%.",a,b,100*(double)a/b);
	return 0;
 }

6. Output as required

Title : A program, the input is an integer a, a floating-point number f, an integer b. (-10000 <a, b, f <10000).
There are five lines of output. The first line outputs a, f, b in sequence, each number occupies 10 character bits, right aligned. No space is added between any two numbers, f is accurate to one decimal place; the second line outputs a, f, b in sequence, each number occupies 10 characters, with positive and negative signs, left aligned, f is accurate to Two decimal places. No spaces are added between any two numbers. The end of the output string is Hello; the third line outputs 35 #; the fourth line sequentially outputs a, f, b, each number occupies 10 characters, including positive and negative signs, right aligned, f is accurate to two decimal places , No space is added between any two numbers; the fifth line continuously outputs three groups of 0123456789.
Input sample
12 34.567 89
Output sample
12 34.6 89
+12 +34.57 +89 Hello
############################## ####
+12 +34.57 +89
012345678901234567890123456789
Code :

#include<stdio.h>
int main()
{
	int a,b;
	double f;
	scanf("%d %lf %d",&a,&f,&b); //double类型输入用lf
	//+为整数时显示+;-左对齐(默认右对齐);
	//%a.bf保留小数点后b位,所占长度至少为a
	printf("%10d%10.1lf%10d\n",a,f,b);
	printf("%-+10d%-+10.2lf%-+10dHello\n",a,f,b);
	printf("###################################\n");
	printf("%+10d%+10.2lf%+10d\n",a,f,b);
	for(int i=0;i<3;i++)
	printf("0123456789");
	return 0;
}

7. Temperature conversion

Code :

#include<stdio.h>
//宏定义提高代码可读性 
#define bei 1.8 
#define cha 32
#define low -273.15
int main()
{
	double num;
	char c;
	scanf("%lf %c",&num,&c);
	if(c=='C'&&num>=low)
	printf("%.2lf F",num*bei+cha);
	else if(c=='F'&&(num-cha)/bei>=low)
	//计算转化后的是否满足高于绝对零度 
	printf("%.2lf C",(num-cha)/bei);
	else
	printf("invalid");
	return 0;
}

8. Floating point comparison

Problem : The
input is only one line, and the three floating-point numbers a, b, c separated by spaces (0 <a, b, c <100)
are also only one line. If a-b is equal to c, then yes is output, otherwise no .
Input sample
3.4 2.2 1.3
Output sample
no
analysis :

  1. Use double for floating-point input to prevent data from being too large
    . 2. Use fabs for floating-point comparison:
    equal: fabs (mn) <= 1E-6
    range: fabs (mn)> 1E-6
    comparison size: (to satisfy the size relationship and not Equal) m> n && fabs (mn)> 1E-6 or mn> 1E-6
    principle: converted to binary storage, only floating point numbers that can be expressed as powers of 2 multiplied by integer numbers can be accurately expressed, the
    rest of the numbers are Approximate
    code :

     #include<stdio.h>
     #include<math.h>//fabs函数需要引入数学库
     int main()
     {
     	double a,b,c;
     	scanf("%lf %lf %lf",&a,&b,&c);
     	if(fabs(a-b-c)<1E-6)
     	printf("yes");
     	else printf("no");
     	return 0;
      } 
    

9. Pay Party Fee

Topic : Monthly wage income (after tax) of less than 3,000 yuan (including 3,000 yuan), pay 0.5% of monthly wage income; from 3,000 yuan to 5,000 yuan (including 5,000 yuan), pay 1%; from 5,000 yuan to For 10,000 yuan (including 10,000 yuan), pay 1.5%; for those above 10,000 yuan, pay 2%.
Now I would like to ask you to write a program to calculate the amount of party fees that a serving teacher party member should pay in a month.
The input format is
only one line, which is a floating-point salary (0 <salary <20000), which is the teacher ’s monthly income (after tax).
The output format is
only one line, which is the amount of the party fee that the teacher should pay in the month (retain 1 decimal place).
Input sample
3000
Output sample
15.0
code

#include<stdio.h>
#include<math.h>
int main()
{
	const double rate[4]={0.005,0.01,0.015,0.02};
	//党费比例写在数组中,代码可读性高 
	double s,d;
	scanf("%lf",&s);
	if(s<3000||fabs(s-3000)<=1E-6)//小于等于一个浮点数的判断方法 
	d=rate[0]*s;
	else if(s<5000||fabs(s-5000)<=1E-6)
	d=rate[1]*s;
	else if(s<10000||fabs(s-10000)<=1E-6)
	d=rate[2]*s;
	else
	d=rate[3]*s;
	printf("%.1lf",d);
	return 0;
}

10. To be continued. . .

Guess you like

Origin www.cnblogs.com/shanestella/p/12700963.html