C - Letters

  • There are n dormitories in Berland State University, they are numbered with integers from 1 to n. Each dormitory consists of rooms, there are ai rooms in i-th dormitory. The rooms in i-th dormitory are numbered from 1 to ai

    .

    A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all n

    dormitories is written on an envelope. In this case, assume that all the rooms are numbered from 1 to a1+a2++an

    and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.

    For example, in case n=2

    , a1=3 and a2=5 an envelope can have any integer from 1 to 8 written on it. If the number 7 is written on an envelope, it means that the letter should be delivered to the room number 4

    of the second dormitory.

    For each of m

    letters by the room number among all n

    dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.

    Input

    The first line contains two integers n

    and m (1n,m2105)

    — the number of dormitories and the number of letters.

    The second line contains a sequence a1,a2,,an

    (1ai1010), where ai equals to the number of rooms in the i-th dormitory. The third line contains a sequence b1,b2,,bm (1bja1+a2++an), where bj equals to the room number (among all rooms of all dormitories) for the j-th letter. All bj

    are given in increasing order.

    Output

    Print m

    lines. For each letter print two integers f and k — the dormitory number f (1fn) and the room number k in this dormitory (1kaf)

    to deliver the letter.

    Examples
    Input
    3 6
    10 15 12
    1 9 12 23 26 37
    
    Output
    1 1
    1 9
    2 2
    2 13
    3 1
    3 12
    
    Input
    2 3
    5 10000000000
    5 6 9999999999
    
    Output
    1 5
    2 1
    2 9999999994
    
    Note

    In the first example letters should be delivered in the following order:

    • the first letter in room 1
  • of the first dormitory
  • the second letter in room 9
  • of the first dormitory
  • the third letter in room 2
  • of the second dormitory
  • the fourth letter in room 13
  • of the second dormitory
  • the fifth letter in room 1
  • of the third dormitory
  • the sixth letter in room 12 of the third dormitory


AC代码(一开始超时了,后来因为信封是上升序列改成一个循环就可以了)

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long n, m, x, h;
    int i;
    long long a[200000+10], b[200000+10];
    while(scanf("%lld %lld",&n, &m)!=EOF)
    {
        h = 1;
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        a[0] = 0;
        for(i = 1; i<=n; i++)
        {
            scanf("%lld",&x);
            a[i] = a[i-1]+x;
        }
        for(i = 0; i<m; i++)
            scanf("%lld",&b[i]);
       for(i = 0;i<m;i++)
       {
        if(b[i]<=a[h])
        printf("%lld %lld\n",h, b[i]-a[h-1]);
        else
        {
         h++;
         i--;
        }
       }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤来实现 Objective-C 图片验证码: 1. 首先,你需要创建一个包含数字和字母的随机字符串,作为验证码。你可以使用以下代码来生成随机字符串: ``` NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; NSMutableString *randomString = [NSMutableString stringWithCapacity:10]; for (int i = 0; i < 10; i++) { uint32_t randomIndex = arc4random_uniform((uint32_t)[letters length]); [randomString appendFormat:@"%C", [letters characterAtIndex:randomIndex]]; } ``` 2. 接下来,你需要将随机字符串渲染成图片。你可以使用以下代码来创建一个 UIImage 对象,将字符串绘制到图像上: ``` CGSize imageSize = CGSizeMake(100, 50); UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); NSString *text = randomString; UIFont *font = [UIFont systemFontOfSize:20.0]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.alignment = NSTextAlignmentCenter; NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle }; [text drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height) withAttributes:attributes]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); ``` 3. 最后,你需要将图像和验证码字符串一起返回给用户。你可以使用以下代码将图像和字符串包装到 NSDictionary 对象中: ``` NSDictionary *response = @{ @"image": image, @"code": randomString }; ``` 这些代码将生成一个包含图像和验证码字符串的字典,你可以将其返回给客户端使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值