codeforces 339A.Helpful Maths B.Xenia and Ringroad 两水题
  TEZNKK3IfmPf 2023年11月12日 17 0

A.题意就是把字符串里面的数字按增序排列,直接上代码。

#include <string.h>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
char s[1005];
int num[105];
while (scanf("%s" , s) != EOF)
{
int l = strlen(s);
int t = 0;
int cnt = 0;
for (int i = 0; i <= l; i++)
{
if (s[i] == '+' || !s[i])
{
num[++cnt] = t;
t = 0;
continue;
}
t = t*10 + s[i]-'0';
}
sort(num+1, num+cnt+1);
for (int i = 1; i < cnt; i++)
printf("%d+", num[i]);
printf("%d\n", num[cnt]);
}
return 0;
}

 

 

B.题意,有n个房子顺时针排成一圈,标号从1到n,只能顺时针走,要按次序到达规定的位置,从一个房子到旁边的房子需要1单位的时间,求总共要多长时间(注意要用64位整形)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>

using namespace std;

int a[100005];

int main()
{
int n, m;
__int64 ans;
a[0] = 1;
while (scanf("%d %d", &n, &m) != EOF)
{
ans = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d", &a[i]);
if (a[i] >= a[i-1])
ans += (__int64)(a[i] - a[i-1]);
else
ans += (n-a[i-1]+a[i]);
}
printf("%I64d\n", ans);
}
return 0;
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

TEZNKK3IfmPf