goto+双指针运用示例

例题 简单版本
链接:https://ac.nowcoder.com/acm/contest/3571/B?&headNav=acm
来源:牛客网

“一刀建林流泪,两刀马云都得跪。”摆在你面前的一长条切糕,你想尝到切糕里面所有的果仁,什么核桃呀,杏仁呀,巴旦木呀…但因为切糕很贵,你要选取一段连续的切糕,使得你能吃到这份切糕里所有的果仁,切记切糕贵,所以要选取最短的长度并且要包含所有的果仁,这里的果仁可以简单的看做a果仁,b果仁,c果仁….,输出能包含所有果仁的最短长度。换句话说出现的果仁都要出现在你所选的区间里面,输出这个区间的最短长度。
输入描述:
第一行包含整数n(1≤n≤1000)——切糕的长度。第二行包含长度为n的字符串,它由英文字母表中的大写字母和小写字母组成。

输出描述:
输出一个整数,表示最小选取的长度。

示例1

输入

1
A

输出

1

示例2

输入

4
qqqE

输出

2

示例3

输入

9
bcdddbddc

输出
复制

3
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int main()
{
    int n,max1=0,sum=0;
    int vis[200]={0};
    char a[maxn];
    cin>>n;
    getchar();
    gets(a);
    int j=0;
    for(int i=0;i<n;i++)
    {
        if(vis[a[i]]==0)
        {
            vis[a[i]]=1;
            sum++;
        }
    }
    int len=1000005;
    for(int i=0;i<n;i++)
    {
        int ans=0;int vis1[200]={0};
        for(int j=i;j<n;j++)
        {
            if(vis1[a[j]]==0)
            {
                vis1[a[j]]=1;
                ans++;
            }
            if(ans==sum) 
            {
                len=min(len,j-i+1);
                if(len==sum)
                {
                    goto in;//直接跳出函数到in
                }
            }
        }
    }
    in:;
    cout<<len; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好!这是一个如何用 Python 实现指针时钟的代码示例: ``` import turtle import time def setup_window(): window = turtle.Screen() window.title("Pointer Clock") window.bgcolor("white") def draw_circle(): pen = turtle.Turtle() pen.penup() pen.goto(0,210) pen.pendown() pen.circle(210) def draw_line(angle, length): pen = turtle.Turtle() pen.penup() pen.goto(0,0) pen.setheading(angle) pen.pendown() pen.fd(length) def draw_pointer(angle, length): pen = turtle.Turtle() pen.penup() pen.goto(0,0) pen.setheading(angle) pen.pendown() pen.pensize(2) pen.fd(length) def draw_clock(): draw_circle() for i in range(12): angle = i * 30 draw_line(angle, 190) def update_clock(): current_time = time.gmtime() hour_angle = 30 * current_time.tm_hour + 0.5 * current_time.tm_min minute_angle = 6 * current_time.tm_min second_angle = 6 * current_time.tm_sec draw_pointer(hour_angle, 120) draw_pointer(minute_angle, 180) draw_pointer(second_angle, 190) if __name__ == "__main__": setup_window() draw_clock() while True: update_clock() turtle.update() time.sleep(1) ``` 这是一个使用 Turtle 库实现的指针时钟,可以实时显示当前的时间。在这个代码中,我们首先设置了窗口的标题、背景颜色,然后绘制了时钟的圆形外框和刻度线。接着,我们实现了一个 `update_clock` 函数,该函数会计算出当前时间,并更新时针、分针、秒针的位置。最后,我们在主函数中循环调用 `update_clock` 函数 ### 回答2: 在Python中,没有直接的指针类型,但我们可以使用变量和运算符来模拟指针的行为来实现指针时钟。下面是一个300字的示例代码: ```python import time def pointer_clock(): while True: # 获取当前时间 current_time = time.localtime() # 计算秒针的角度 second_angle = (current_time.tm_sec / 60) * 360 # 计算分针的角度 minute_angle = (current_time.tm_min / 60) * 360 + (current_time.tm_sec / 60) * (360 / 60) # 计算时针的角度 hour_angle = (current_time.tm_hour / 12) * 360 + (current_time.tm_min / 60) * (360 / 12) # 打印时间和对应的角度 print(f"当前时间:{current_time.tm_hour:02d}:{current_time.tm_min:02d}:{current_time.tm_sec:02d}") print(f"秒针角度:{second_angle:.2f}°") print(f"分针角度:{minute_angle:.2f}°") print(f"时针角度:{hour_angle:.2f}°") # 休眠1秒,继续下一轮循环 time.sleep(1) # 运行指针时钟函数 pointer_clock() ``` 这个程序会不断地打印当前时间以及秒针、分针和时针的角度。通过使用时间模块来获取当前时分秒,并根据每个指针的比例关系计算对应的角度。然后将这些信息打印出来。程序会使用`time.sleep(1)`来使程序休眠1秒钟,以实现秒针的动态效果,然后进入下一轮循环更新时间和角度。 ### 回答3: 指针时钟是一种用于显示时间的时钟,它由时、分、秒三个指针组成。下面是使用Python语言实现指针时钟的代码: ```python import time while True: # 获取当前时间 current_time = time.localtime() # 获取时、分、秒的数值 hour = current_time.tm_hour minute = current_time.tm_min second = current_time.tm_sec # 清空屏幕 print("\033[2J") # 绘制时针 print(' '*30 + '|') # 绘制分针 print(' '*(30 + hour//2) + '|') # 绘制秒针 print(' '*(30 + minute//2) + '|') # 打印时间 print('当前时间:%02d:%02d:%02d' % (hour, minute, second)) # 每秒刷新一次 time.sleep(1) ``` 上述代码中,我们使用了time模块来获取当前的系统时间。然后,将时、分、秒分别赋值给变量hour、minute和second。接下来,我们使用字符串的乘法操作来绘制时针、分针和秒针在终端上的位置。最后,使用time模块的sleep函数来使程序暂停1秒钟,然后再次更新时间。 代码中的`\033[2J`是用来清空终端屏幕的特殊字符。这样,我们每秒都可以在同一个位置上显示最新的时间,并模拟指针时钟运行的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值