1.14-1.15 题解

 

A.统计字母的题,需要仔细看下题。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string.h>
#include <cctype>
#include <climits>
#include <cmath>
#include <time.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
char ch[N];
int cnt[266];
int main()
{
    scanf("%s",ch);
    for(int i=0;ch[i];i++)
    {
        cnt[ch[i]]++;
    }
    int ans = 0;
    ans = min(cnt['b'],cnt['l']);
    ans = min(min(cnt['u']/2,cnt['s']),min(ans,cnt['r']));
    ans = min(cnt['B'],ans);
    ans = min(cnt['a']/2,ans);
    printf("%d\n",ans);
    return 0;
}
AC代码

B.筛。 也可以分解(质)因子,复杂度O(n*sqrt(max(si)))。T的同学应该是用了O(max(si))的做法分解因子

坑点,答案至少为1

#include<bits/stdc++.h>
using namespace std;
int arr[100022];
int main()
{
    int n,ans=0,i,j;
    cin>>n;
    for(i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        arr[a]++;
    }
    for(i=2;i<=100000;i++)
    {
        int cum=0;
        for(j=i;j<=100000;j+=i)
        cum+=arr[j];
        ans=max(cum,ans);
    }
    if(arr[1]>0)
    ans=max(ans,1);
    cout<<ans;
    return 0;
}
#include<bits/stdc++.h>
int cnt[100010];
int main(){
    int n,ans=1;
    scanf("%d",&n);
    for(int k=1;k<=n;k++){
        int x;
        scanf("%d",&x);
        for(int i=2;i<=sqrt(x);i++){//不要用i×i<=x 
            if(x%i==0){
                while(x%i==0) x/=i;
                cnt[i]++;
            }
        }
        if(x>1) cnt[x]++;
    }
    for(int k=1;k<=100000;k++) if(ans<cnt[k]) ans=cnt[k];
    printf("%d",ans);
}
分解因子

C.模拟。一旦到达南北极,必须符合: 南极不能向东西南走,北极不能向东西北走。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int main()
{
    int n;
    cin>>n;
    char ch[10];
    int l;
    int now = 0;
    bool mk = true;
    for(int i=0;i<n&&mk;i++)
    {
        cin>>l>>ch;
        if((ch[0]=='E'||ch[0]=='W'))
            if(now<20000&&now>0) continue;
            else mk = false;
        if(ch[0]=='S')
        {
            
            if(now+l>20000)
            {
                mk = false;
            }
            now = min(now+l,20000);
        }
        else{
            if(now-l<0)
            {
                mk = false;
            }
            now = max(0,now-l);
        }
    }
    if(now==0&&mk) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}
AC代码

D.相当于告诉你合法的条件,然后依次缩小区间,发现L>R自然无解,R 足够大则是inf 其他情况随便输出一个答案即可

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <climits>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int MAX = INT_MAX;
int main()
{
    int L = INT_MIN,R = INT_MAX;
    int pre=0,now,div;
    int n;
    scanf("%d",&n);
    for(int i = 0; i<n;i++)
    {
        scanf("%d%d",&now,&div);
        if(div==1)
        {
            L = max(L,1900-pre);
        }
        else{
            R = min(R,1899-pre);
        }
        pre+=now;
    }
    if(L<=R)
    {
        if(R<MAX)
            
            printf("%d",R+pre);
        else
            printf("Infinity");
    }
    else{
        printf("Impossible");
    }
    return 0;
}
AC代码

E.简单的搜索题,暴力for循环也能做。4×4的棋盘 ‘.’表示能下,连成至少三个获胜

 

#include<iostream>
using namespace std;
int main()
{
    char a[20][20];
    for(int i=6;i<10;i++)
        {
        for(int j=6;j<10;j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=6;i<10;i++)
        {
        for(int j=6;j<10;j++)
        {
            if(a[i][j]=='.')
                {
                if((a[i+1][j]=='x' && a[i+2][j]=='x')||(a[i+1][j]=='x' && a[i-1][j]=='x') ||(a[i-1][j]=='x' && a[i-2][j]=='x') ||
                (a[i][j+1]=='x' && a[i][j+2]=='x') || (a[i][j+1]=='x' && a[i][j-1]=='x') || (a[i][j-1]=='x' && a[i][j-2]=='x') ||
                (a[i-1][j+1]=='x' && a[i-2][j+2]=='x') ||(a[i-1][j+1]=='x' && a[i+1][j-1]=='x') || (a[i+1][j-1]=='x' && a[i+2][j-2]=='x')||
                (a[i-1][j-1]=='x' && a[i-2][j-2]=='x')|| (a[i-1][j-1]=='x' && a[i+1][j+1]=='x')||(a[i+1][j+1]=='x' && a[i+2][j+2]=='x'))
                {
                     cout<<"YES";
                     return 0;
                 }
            }
        }
    }
    cout<<"NO";
}
for循环

 

转载于:https://www.cnblogs.com/chdacm/p/6288193.html

<think>好的,用户需要PT-1题解或练习题解析。首先得确定PT-1具体指什么,可能是指某个编程题库的第1题,或者某个教材、竞赛中的题目。常见的PT-1可能出现在PAT(浙江大学程序设计能力考试)或者PTA平台的题目中。需要进一步确认,但用户没有提供更多信息,所以得假设是常见的可能性。 接下来,调用搜索工具查找“PT-1 题解”、“PT-1 练习题解析”等相关内容。搜索结果中发现,PT-1PAT乙级或甲级考试中的第一题相关,比如A+B类型的问题。例如,PAT乙级1001题是“害死人不偿命的(3n+1)猜想”,这类题目通常需要循环和条件判断。 另外,PTA平台上的题目可能涉及不同的算法,比如PT-1可能考察基础输入输出、简单数学运算或模拟题。例如,PT-1可能要求处理字符串反转、数字各位数求和等基础操作。需要根据不同来源的题解,整理出常见的解题思路和代码示例。 还要注意用户可能需要的答案结构,需要分方法或步骤,避免使用第一人称。可能需要将不同题型的解法归类,比如数学问题、字符串处理、数组操作等,并给出对应的代码示例和公式。例如,对于涉及数论的题目,使用数学公式描述算法步骤,如$n = n / 2$(偶数)和$n = (3n + 1)/2$(奇数),直到n变为1,统计步数。 同时,要确保代码格式正确,用三个反引号包裹,并注明语言类型。比如Python或C++的实现。此外,在最后生成相关问题,需基于PT-1的可能扩展,比如具体题号的解析、不同难度题目的策略,或者常见错误分析。 最后,整合所有信息,确保回答清晰、分步骤,并符合格式要求,避免引用堆砌,每个方法间换行,正确使用数学公式和代码块。相关问题部分要列出三个相关且具体的问题,帮助用户进一步查找所需内容。</think>根据网络搜索结果,PT-1通常指程序设计类考试/练习中的基础题型。以下是针对PT-1常见题型的解析方法及代码实现: **数值计算类题型** 典型例题:实现输入两个整数A和B,输出A+B的结果 ```python a, b = map(int, input().split()) print(a + b) ``` 此类题目重点考察输入输出格式处理,注意特殊数据边界值测试 **条件判断类题型** 常见考法:给定数字n,判断其是否为素数 数学判定条件:若n不能被$2 \leq i \leq \sqrt{n}$范围内的任意整数整除则为素数 ```c++ bool isPrime(int n) { if(n <= 1) return false; for(int i=2; i*i<=n; i++) if(n%i == 0) return false; return true; } ``` **循环控制类题型** 经典案例:3n+1猜想(PAT乙级1001) 处理流程: 1. 输入正整数n 2. 循环执行: - 当n为偶数时:$n = n/2$ - 当n为奇数时:$n = (3n + 1)/2$ 3. 统计操作步数直到n=1 ```python n = int(input()) count = 0 while n != 1: n = n//2 if n%2 ==0 else (3*n+1)//2 count +=1 print(count) ``` **字符串处理类题型** 常见形式:字符串反转/统计特定字符 ASCII码转换公式:$ch_{new} = (ch_{original} + shift) \% 128$ ```java public static String reverseString(String s) { return new StringBuilder(s).reverse().toString(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值