codeforces 339A.Helpful Maths B.Xenia and Ringroad 两水题
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; }