Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the elements are numerated from one).
Find the number on the n-th position of the sequence.
The only line contains integer n (1 ≤ n ≤ 1014) — the position of the number to find.
Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Print the element in the n-th position of the sequence (the elements are numerated from one).
3
2
5
2
10
4
55
10
56
1
题意:给一串数字,找出规律,求第n个数是多少
解:找规律
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define LL __int64
int main()
{
LL n;
while(scanf("%I64d",&n)!=EOF)
{
LL m=floor((-1+sqrt(8*n-7))/2);
LL x=n-m*(m+1)/2;
printf("%I64d\n",x);
}
return 0;
}</span>
水过吧
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int h,m,a;
while(scanf("%d:%d",&h,&m)!=EOF)
{
scanf("%d",&a);
int M=(a+m)%60;
int x=(a+m)/60;
int H=(h+x)%24;
printf("%02d:%02d\n",H,M);
}
return 0;
}</span>
解:可以这样做,如果上一个元素和当前元素相同,表示成pre[i]=pre[i-1],不同就pre[i]=i-1;最后如果a[r]!=x输出r,否则的话就pre[r]>=l的话,就输出pre[r];如果不满足上诉情况,直接输出-1;
<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=2e5+10;
int a[maxm];
int pre[maxm];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(pre,0,sizeof(pre));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]!=a[i-1])
{
pre[i]=i-1;
}
else
{
pre[i]=pre[i-1];
}
}
int l,r,x;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&l,&r,&x);
int id=-1;
if(a[r]!=x)
{
id=r;
}
else if(pre[r]>=l)
{
id=pre[r];
}
printf("%d\n",id);
}
}
return 0;
}
</span>