M2021 spring C, Java introductory exercise paragraph I - variables, expressions, branches, loops 92-98

Keywords: C

7-92 sum of special a series (20 points)
Given two positive integers a and n that are not more than 9, it is required to write a program to find the sum of a+aa+aaa + +... + aa * a (n a).

Input format:
Enter positive integers a and n that give no more than 9 in one line.

Output format:
Output in one line in the format of "s = corresponding sum".

Input example:
2 3
No blank lines at the end
Output example:
s = 246
No blank lines at the end

#include<stdio.h>
#include<math.h>
int main()
{
	int a,n;
	scanf("%d%d",&a,&n);
	
	int i=0,sum=0,b=a;
	for(i=0;i<n;i++)
	{
		sum+=b;
		b=b*10+a;
	}
	printf("s = %d",sum);
	
	return 0;
}

7-93 daffodils (20 points)
Narcissus number refers to an n-bit positive integer (n ≥ 3), and the sum of the N powers of the numbers in each bit is equal to itself. For example: 153 = 1
3
+5
3
+3
3
. This problem requires writing a program to calculate the number of all N-bit daffodils.

Input format:
Input gives a positive integer N (3 ≤ N ≤ 7) in one line.

Output format:
Output the number of all N-bit daffodils in ascending order, and each number occupies one line.

Input example:
3
No blank lines at the end
Output example:
153
370
371
407
No blank lines at the end

#include<stdio.h>
#include<math.h>
int main()
{
	int n,max,min,d,i,y,z,l=1,sum=0;
	scanf("%d",&n);
	max=pow(10,n)-1;
	min=pow(10,n-1);
	
	for(y=min;y<=max;y++)
	{
		sum=0;
		i=y;
		while(i>0)
		{
			l=1;
			d=i%10;
			i/=10;
			for(z=0;z<n;z++)
			{
			l*=d;
			}	
			sum+=l;	
				
		}
		if(sum==y)
	   	{
			printf("%d\n",y);
	   	}
	}

	return 0;
}

7-94 calculate Fahrenheit temperature (5 points)
This problem requires writing a program to calculate the Fahrenheit temperature corresponding to the Celsius temperature of 26 ° C. Calculation formula: F=9 × C/5+32, where: C represents Celsius temperature, f represents Fahrenheit temperature, and the output data shall be integer.

Input format:
This topic is not entered.

Output format:
Output in the following format

celsius = 26, fahr = corresponding integer value of Fahrenheit temperature

#include<stdio.h>
int main()
{	
	printf("celsius = 26, fahr = %d",9*26/5+32);
	return 0;
}

7-95 calculation of train running time (15 minutes)
According to the departure time and arrival time of the train, write a program to calculate the time spent on the whole journey.

Input format:
Input two 4-digit positive integers in one line, separated by spaces, representing the departure time and arrival time of the train respectively. The format of each time is 2-digit hours (00-23) and 2-digit minutes (00-59). It is assumed that departure and arrival are on the same day.

Output format:
Output the travel time in one line in the format of "hh:mm", where hh is 2-bit hours and MM is 2-bit minutes.

Input example:
1201 1530
No blank lines at the end
Output example:
03:29
No blank lines at the end

#include<stdio.h>
#include<math.h>
int main()
{
	
//t1 represents the departure time and m1 represents converting the time into minutes
//t2 represents the end time and m2 represents converting the time into minutes
// m represents the minute change of time 
	int m1=0,t1=0;
	int m2=0,t2=0;
	int m=0;
	
	scanf("%d %d",&t1,&t2);
	
	m1=t1/100*60+t1%100;
	m2=t2/100*60+t2%100;
	m=m2-m1;
	
	
	printf("%02d:%02d",m/60,m%60);
			
	return 0;
}

7-96 calculation of deposit interest (10 points)
This topic requires the calculation of deposit interest. The calculation formula is interest=money × (1+rate)
year
− money, where interest is the interest at maturity (before tax), money is the deposit amount, year is the deposit period, and rate is the annual interest rate.

Input format:
The input gives three positive real numbers money, year and rate in one line, separated by spaces.

Output format:
Output in the format of "interest = interest" in one line, where the interest retains two decimal places.

Input example:
1000 3 0.025
No blank lines at the end
Output example:
interest = 76.89
No blank lines at the end

#include<stdio.h>
#include<math.h>
int main()
{	
	float m,y,r;
	scanf("%f%f%f",&m,&y,&r);
	printf("interest = %.2f",m*pow((1+r),y)-m);
	return 0;
}

7-97 calculation of individual income tax (10 points)
It is assumed that the individual income tax rate is: × (salary − 1600). Please write a program to calculate the income tax payable, where the tax rate is defined as:

When the salary does not exceed 1600, the tax rate is 0;
When the salary is in the range (1600, 2500], the tax rate is 5%;
When the salary is in the range (2500, 3500], the tax rate is 10%;
When the salary is in the range (3500, 4500], the tax rate is 15%;
When the salary exceeds 4500, the tax rate is 20%.
Input format:
The input gives a non negative salary in one line.

Output format:
Output individual income tax in one line, accurate to 2 decimal places.

Input example 1:
1600
No blank lines at the end
Output example 1:
0.00
No blank lines at the end
Input example 2:
1601
Output example 2:
0.05
Input example 3:
3000
Output example 3:
140.00
Input example 4:
4000
Output example 4:
360.00
Input example 5:
5000
Output example 5:
680.00

#include<stdio.h>
int main()
{	
	float x,y;
	scanf("%f",&x);
	if(x<=1600)y=0;
	else if(x<=2500)y=(x-1600)*0.05;
	else if(x<=3500)y=(x-1600)*0.10;
	else if(x<=4500)y=(x-1600)*0.15;
	else y=(x-1600)*0.20;
	printf("%.2f",y);
	return 0;
}

7-98 simple calculator for two numbers (10 points)
This problem requires the preparation of a simple calculator program, which can add, subtract, multiply, divide or remainder two integers according to the input operators. The problem ensures that the input and output do not exceed the integer range.

Input format:
Input in a row, enter operand 1, operator, and operand 2, separated by a space. The data type of the operand is integer, and the denominator of division and remainder is guaranteed to be non-zero.

Output format:
When the operators are +, -, *, /,%, the corresponding operation results are output on one line. If the input is illegal symbols (i.e. symbols other than addition, subtraction, multiplication, division and remainder operators), ERROR is output.

Input example 1:
-7 / 2
No blank lines at the end
Output example 1:
-3
No blank lines at the end
Input example 2:
3 & 6
Output example 2:
ERROR

#include<stdio.h>
#include<math.h>
int main()
{
	int a,b,c;
	char x;
	scanf("%d %c %d",&a,&x,&b);
	if(x=='+')
	{
		c=a+b;
			printf("%d",c);
	}
	else if(x=='-')
	{
		c=a-b;
			printf("%d",c);
	}
	else if(x=='*')
	{
		c=a*b;
			printf("%d",c);
	}
	else if(x=='/')
	{
		c=a/b;
			printf("%d",c);
	}
	else if(x=='%')
	{
		c=a%b;
			printf("%d",c);
	}
	else
	{
		printf("ERROR");
	}

	return 0;
}

Posted by jamesl73 on Sat, 23 Oct 2021 03:40:38 -0700