ACM Second Week Training

Posted by konigwolf on Wed, 05 Jan 2022 22:17:21 +0100

Question C - Subparagraph Summation

Gives an array of length N, makes Q queries, and queries the sum of all elements in a subsection of length l starting from the first element.

For example, 1 137 9-1, query for subsections and 1 {37 9}-1 with a starting length of 3 for the second element. 3 + 7 + 9 = 19, output 19.

Input

Line 1: A number N, N is the length of the array (2 <= N <= 50000). Lines 2 to N + 1: N elements of the array. Line N + 2 (-10^9 <= N[i] <= 10^9): 1 number Q, Q is the number of queries. Lines N + 3 to N + Q + 2: 2 numbers per line, i, l (1 <= I <= N, I + l <= N)

Output

A total of Q rows, corresponding to the calculation results of Q queries.

Sample Input

5
1
3
7
9
-1
4
1 2
2 2
3 2
1 5

Sample Output

4
10
16
19
#include<stdio.h>  
long long  sum[1000000],x;  
int main()  
{	
	int n;  
	scanf("%d",&n);  
	sum[0]=0; //Don't forget to write 
	for(int i=1;i<=n;i++)  
	{
		scanf("%lld",&x);  
		sum[i]=sum[i-1]+x;  
	}  
	scanf("%d",&n);  
	int a,b;  
	for(int i=1;i<=n;i++)  
	{
		scanf("%d%d",&a,&b);  
		printf("%lld\n",sum[a+b-1]-sum[a-1]);  
	}
	return 0;
}

Question D - Sorting by ASCII Code

After three characters are input, the ASCII codes of each character are output in order from smallest to largest.

Input

There are several groups of input data, each on a single line, consisting of three characters with no spaces between them.

Output

For each set of input data, the output line is separated by a space in the middle of the characters.

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z
#include<stdio.h>
#include<string.h>
int main()
{
	char a,b,c,temp;
	while(~scanf("%c%c%c",&a,&b,&c))
	{
		getchar();
		if(a>b)
		{
			temp=a;
			a=b;
			b=temp;	
		}
		if(a>c)
		{
			temp=a;
			a=c;
			c=temp;
		}
		if(b>c)
		{
			temp=b;
			b=c;
			c=temp;
		}
		printf("%c %c %c\n",a,b,c);
		//printf("\n");
	}
	return 0;
} 

Question E - Achievement Conversion

Enter a percentage score t and convert it to the corresponding grade with the following rules:
90~100 is A;
80~89 is B;
70-79 is C;
60~69 is D;
0~59 is E;

Input

The input data has multiple groups, each of which takes up one row and consists of an integer.

Output

For each set of input data, output one row. If the input data is not in the range of 0-100, output a line:'Score is error!'.

Sample Input

56
67
100
123

Sample Output

E
D
A
Score is error!
#include<stdio.h>

int main()
{
	int x;
	while(scanf("%d",&x)!=EOF)
	{
			if(x>=90&&x<=100)
			    printf("A\n");

			else if(x>=80&&x<=89)
			    printf("B\n");
			
			else if(x>=70&&x<=79)
			    printf("C\n");
			
			else if(x>=60&&x<=69)
			    printf("D\n");

			else if(x>=0&&x<=59)
			    printf("E\n");
		
		    else
		        printf("Score is error!\n");
    }
    return 0;
	
}

Question F - What day?

Given a date, the output date is the day of the year.

Input

There are several groups of input data, each group occupies one row, and the data format is YYYY/MM/DD. See sample input specifically. In addition, you can be sure that all input data is legitimate.

Output

For each set of input data, an output line indicates the date is the day of the year.

Sample Input

1985/1/20
2006/3/12

Sample Output

20
71
#include<stdio.h>
int main()
{
	int year,month,day;
	int leap;
	int count;
	
	
	
	while(scanf("%d/%d/%d",&year,&month,&day)!=EOF)
	{
		count=0;
		if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)))//Leap year 
		{
			leap=29;
		}
		else
		{
			leap=28;
		}
		
		month=month-1;
		
		switch(month)
		{
			case 11:count+=30;
			case 10:count+=31;
			case 9:count+=30;
			case 8:count+=31;
			case 7:count+=31;
			case 6:count+=30;
			case 5:count+=31;
			case 4:count+=30;
			case 3:count+=31;
			case 2:count+=leap;
			case 1:count+=31; 
		}
		count+=day;
		printf("%d\n",count);
	}
	return 0;
} 

G Question - Product of Odd Numbers

Give you n integers and find the product of all the odd numbers in them.

Input

The input data contains multiple test instances, each of which takes up one row, and the first number of each row is n, which means there are n in total for this set of data, followed by n integers. You can assume that there must be at least one odd number for each set of data.

Output

Output the product of all odd numbers in each array, and one line for the test case.

Sample Input

3 1 2 3
4 2 3 4 5

Sample Output

3
15
#include<stdio.h>

int main()
{
	int n,i,a;
	int m=1;
	
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{   
		  	scanf(" %d",&a);
			if(a%2==1)
			m*=a;
				
     	}
		printf("%d\n",m);
		m=1;	
	}
} 

H Question - Decision of Prime Numbers

For the expression n^2+n+41, when n takes an integer value in the (x,y) range (including x,y) (-39<=x<y<=50), determine whether the values of the expression are all prime numbers.

Input

The input data has multiple groups, each of which takes up one row and consists of two integers, X and y. When x=0 and y=0, the input ends and the row is not processed.

Output

For each given range of values, if the value of the expression is a prime number, output "OK", otherwise output "Sorry", with one line for each set of outputs.

Sample Input

0 1
0 0

Sample Output

OK
#include<stdio.h>
int main()
{
	int n,i,x,y;
	while(scanf("%d%d",&x,&y)!=EOF&&(x||y))//-39<=x<y<=50
	{
		int isprime=1;
		for(n=x;n<=y;n++)
		{
			for(i=2;i<(n*n+n+41);i++)
			{
			    if((n*n+n+41)%i==0)
			    {
				   isprime=0; 
				   break;
			    }
			}
        }
		if(isprime)
			printf("OK");
		else
			printf("Sorry");
		printf("\n");
	}
    return 0;
}

Title I - The Story of Ta Tao

This puzzle, oh ^-^
The situation was as follows:
Monkey eats more than half of the peaches on the first day, eats more than half of the remaining peaches on the next day, and then eats more than half of the remaining peaches on the previous day every day, leaving only one peach when he prepares to eat on the next day. Clever you, please help Wukong figure out how many peaches he had when he started eating on the first day.

Input

The input data has multiple groups, each of which takes up one row, and contains a positive integer n (1 < n < 30), which means that when only one peach is left, it occurs on the n-th day.

Output

For each set of input data, the total number of peaches at the beginning of the first day of eating is output, with one row per test instance.

Sample Input

2
4

Sample Output

4
22
#include<stdio.h>
int main()
{
	int n;//Days entered, 1<n<30 
	int i;//Control loop variable 
	int x=1;//Total number of peaches 
	while(~scanf("%d",&n))//Enter days n 
	{
		for(i=1;i<n;i++)
		{
			x=(x+1)*2;
			
		}
	    printf("%d\n",x);//Number of output peaches x 
	x=1;//Keep in mind that once the loop is completed, the total number should be cleared, otherwise the next set of calculations will be incorrect 
	}
}
//n x
//1 1
//2 4
//3 10
//4 22
  

Topics: C