7.20前两天电脑坏了拿去修 TAT今天好好加油

1104 Sum of Number Segments (20)(20 分)

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 10^5^. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4
0.1 0.2 0.3 0.4 

Sample Output:

5.00
#include <cstdio>
double a[100010];
int main()
{
	int n;
	double sum=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	  scanf("%lf",&a[i]);
	for(int i=1;i<=n;i++)
      sum+=a[i-1]*i*(n+1-i);
	printf("%.2f",sum);
	return 0;
}

找规律的数学题 

先考虑边值极端情况 如第一位和第n位观察可得这两位最后出现的次数都为n次

由此可以想到i*(n+1-i)  再带几个值验算即可

1008 Elevator (20)(20 分)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41
#include <cstdio>
int main()
{
	int n;
	scanf("%d",&n);
	int now=0,next,min=0;
	while(n--)
	{
		scanf("%d",&next);
		if(next>now)
		{
		  min+=6*(next-now);
		  now=next;
		}
		else if(next<now)
		{
			min+=4*(now-next);
			now=next;
		}
		min+=5;
    }
    printf("%d\n",min);
	return 0;
}

 有点简单^_^

1049 Counting Ones (30)(30 分)

The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.

Input Specification:

Each input file contains one test case which gives the positive N (<=2^30^).

Output Specification:

For each test case, print the number of 1's in one line.

Sample Input:

12

Sample Output:

5
#include <cstdio>
int main()
{
	int n,a=1,ans=0;
	int left,now,right;
	scanf("%d",&n);
	while(n/a!=0)//a表示当前位的计量单位 
	{
		left=n/(a*10);
		now=n/a%10;//考虑左边位中间位右边位
		right=n%a;
		if(now==0) ans+=left*a; 
		else if(now==1) ans+=left*a+1+right;//1013 前面已计算过10*10 最后加上101层的3个数 
		else if(now>=2) ans+=(left+1)*a;
		a*=10; 
	}
    printf("%d\n",ans);
	return 0;
}

刚开始总觉得一定会重复 后来想想不会 因为计算的是每个位上所有可能为1的个数

每个位单个计算  不会重复

看了好久 像这种数学类的题还是多找找规律吧

1081 Rational Sum (20)(20 分)

Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:

5
2/5 4/15 1/30 -2/60 8/3

Sample Output 1:

3 1/3

Sample Input 2:

2
4/3 2/3

Sample Output 2:

2

Sample Input 3:

3
1/3 -1/6 1/8

Sample Output 3:

7/24
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int gcd(int a,int b) {
	return b==0?a:gcd(b,a%b);//记忆 求a与b的最大公约数
}
struct fraction { //定义分数结构体
	ll up,down;
};
fraction reduce(fraction result) { //分数化简
	if(result.down<0) { //题目规定符号要在分子上
		result.up=-result.up;
		result.down=-result.down;
	}
	if(result.up==0) { //若分子为0 定义分母为1
		result.down=1;
	} else { //约分
		int d=gcd(result.up,result.down);
		result.up/=d;
		result.down/=d;
	}
	return result;
}
fraction add(fraction f1,fraction f2) {
	fraction result;
	result.down=f1.down*f2.down;
	result.up=f1.up*f2.down+f2.up*f1.down;
	return reduce(result);
}
void showresult(fraction r) {
//	reduce(r);
	if(r.down==1) printf("%lld\n",r.up);
	else if(abs(r.up)>r.down) {
		printf("%lld %lld/%lld\n",r.up/r.down,abs(r.up)%r.down,r.down);
	} else printf("%lld/%lld\n",r.up,r.down);
}
int main() {
	int n;
	scanf("%d",&n);
	fraction sum,temp;
	sum.up=0;
	sum.down=1;
	for(int i=0; i<n; i++) {
		scanf("%lld/%lld",&temp.up,&temp.down);
		sum=add(sum,temp);
	}
	showresult(sum);
	return 0;
}

模板还是挺重要的  记忆 

试了一下因为前面已经reduce过了  show中去掉reduce也是可以的

记下一版化简里面约分  show里面处理假分数

1088 Rational Arithmetic (20)(20 分)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
struct fraction
{
	ll up,down;
};
ll gcd(ll a,ll b)
{
	return b==0? a:gcd(b,a%b);
}
fraction reduce(fraction r)//讨论分母小于0 分子等于0 大于0的情况 
{
	if(r.down<0) 
	{
		r.up=-r.up;
		r.down=-r.down;
	}
	if(r.up==0)
	   r.down=1;
	else
	{
		int d=gcd(abs(r.up),abs(r.down));
		r.up/=d;
		r.down/=d;
	} 
	return r;
}
fraction add(fraction a,fraction b)
{
	fraction r;
	r.up=a.up*b.down+a.down*b.up;
	r.down=a.down*b.down;
	return (reduce(r));
}
fraction min(fraction a,fraction b)
{
	fraction r;
	r.up=a.up*b.down-a.down*b.up;
	r.down=a.down*b.down;
	return (reduce(r));
}
fraction mul(fraction a,fraction b)
{
	fraction r;
	r.up=a.up*b.up;
	r.down=a.down*b.down;
	return (reduce(r));
}
fraction quo(fraction a,fraction b)
{
	fraction r;
	r.up=a.up*b.down;
	r.down=a.down*b.up;
	return (reduce(r));
}
void show(fraction r)
{
	r=reduce(r);//reduce(r);
	if(r.up<0) printf("(");
	if(r.down==1) printf("%lld",r.up);//整数
	else if(abs(r.up)>r.down)
	  printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down); 
	else 
	  printf("%lld/%lld",r.up,r.down);
	if(r.up<0) printf(")");
}
int main()
{
     fraction f1,f2;
     scanf("%lld/%lld %lld/%lld",&f1.up,&f1.down,&f2.up,&f2.down);
     show(f1);
     printf(" + ");
     show(f2);
     printf(" = ");
     show(add(f1,f2));
     printf("\n");
     //减法
	 show(f1);
     printf(" - ");
     show(f2);
     printf(" = ");
     show(min(f1,f2));
     printf("\n"); 
     //乘法
	 show(f1);
     printf(" * ");
     show(f2);
     printf(" = ");
     show(mul(f1,f2));
     printf("\n");  
     //除法 
     show(f1);
     printf(" / ");
     show(f2);
     printf(" = ");//分子分母都是分别存储不会有溢出的现象 
     if(f2.up==0)
     printf("Inf");
     else  show(quo(f1,f2));
     printf("\n"); 
	return 0;
}

 一百多行啊

1.刚开始约分的时候没取绝对值

2.除法的特殊情况可以放到主函数里解决

3.+=*/记得中间打空格

4.最严重的错误没有之一!!!!检查了好多遍 发现还是有0/6的现象才看到注解里的错误  reduce(r);直接这样写 什么作用都没啊  约分也没有  记得细心细心再细心

猜你喜欢

转载自blog.csdn.net/Coding18/article/details/81138616
TAT
今日推荐