PTA数学问题(1069->1104->1008->1049)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37360631/article/details/88839187

一、前言:

简单数学、公约数+公倍数、分数四则运算、质数分解、大数运算、扩展欧几里得、组合数

二、简单数学部分

1、1069 The Black Hole of Numbers
分析:简单数学

#include<bits/stdc++.h>
using namespace std;
int n,a[1100];
int minn,maxx;
bool cmp(int a,int b){
	return a>b;
}
void toArray(int x){
	int pos=0;
	while(x>0){
		a[pos++]=x%10;
		x=x/10;
	}
}
int toInt(){
	int ans=0;
	for(int i=0;i<4;i++){
		ans=ans*10+a[i];
	}
	return ans;
}
int main(){
	scanf("%d",&n);
	while(1){
		toArray(n);
		sort(a,a+4);
		minn=toInt();
		sort(a,a+4,cmp);
		maxx=toInt();
		int ans=maxx-minn;
		printf("%04d - %04d = %04d\n",maxx,minn,ans);
		if(ans==0||ans==6174) break;
		n=ans;
	}
	return 0;
}

2、1104 Sum of Number Segments
分析:简单数学,找规律的一道题目,找出每一个数出现的次数,相乘打印结果即可。
重要的是学会找规律的方法。
我们现在要求第i项在一个长度为n的片段内部总共出现了多少次,可以想到构造一个 f ( i , n ) f(i,n) 的关系式,通过观察大体上应该是一个一次或者二次的表达式,我们可以假设 f ( i , n ) = a n n + b n + c i i + d i + e f(i,n)=a*n*n+b*n+c*i*i+d*i+e ,列举几个数据带进去算一算,得到结果。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
 
using namespace std;
int n;
double x;
int main()
{
    double ans;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf",&x);
        ans+=x*i*(n-i+1);
    }
    printf("%.2f\n",ans);
    return 0;
}

3、1008 Elevator(水题)

#include<bits/stdc++.h>
int a[11000],n;
using namespace std;
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int ans=0,sta=0;
    for(int i=0;i<n;i++){
        int edd=a[i];
        if(edd>sta) {ans=ans+(edd-sta)*6+5;}
        else if(edd<sta) {ans=ans+(sta-edd)*4+5;}
        else {ans=ans+5;}
        sta=edd;
        //cout<<ans<<endl;
    }
    cout<<ans<<endl;
    return  0;
}

4、1049 Counting Ones
分析:给你一个数字n,问1-n中有多少数字里面有1。

猜你喜欢

转载自blog.csdn.net/qq_37360631/article/details/88839187