ACM第二周训练

C题——子段求和

给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和。

例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1。3 + 7 + 9 = 19,输出19。

Input

第1行:一个数N,N为数组的长度(2 <= N <= 50000)。 第2 至 N + 1行:数组的N个元素。(-10^9 <= N[i] <= 10^9) 第N + 2行:1个数Q,Q为查询的数量。 第N + 3 至 N + Q + 2行:每行2个数,i,l(1 <= i <= N,i + l <= N)

Output

共Q行,对应Q次查询的计算结果。

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; //不要忘写 
	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;
}

D题——ASCII码排序

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input

输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output

对于每组输入数据,输出一行,字符中间用一个空格分开。

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;
} 

E题——成绩转换

输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;

Input

输入数据有多组,每组占一行,由一个整数组成。

Output

对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“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;
	
}

F题——第几天?

给定一个日期,输出这个日期是该年的第几天。

Input

输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。

Output

对于每组输入数据,输出一行,表示该日期是该年的第几天。

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=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题—— 求奇数的乘积

给你n个整数,求他们中所有奇数的乘积。

Input

输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。

Output

输出每组数中的所有奇数的乘积,对于测试实例,输出一行。

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题——素数判定

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input

输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output

对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

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;
}

I题——蟠桃记

这个难题,呵呵^-^
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?

Input

输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output

对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input

2
4

Sample Output

4
22
#include<stdio.h>
int main()
{
	int n;//输入的天数 ,1<n<30 
	int i;//控制循环变量 
	int x=1;//蟠桃总数 
	while(~scanf("%d",&n))//输入天数n 
	{
		for(i=1;i<n;i++)
		{
			x=(x+1)*2;
			
		}
	    printf("%d\n",x);//输出蟠桃个数x 
	x=1;//切记循环完一次,总数要清空,不然下一组计算会有误 
	}
}
//n x
//1 1
//2 4
//3 10
//4 22
  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值