人在江湖【走】,怎能不挨【刀】;为了能活【口】,唯有把己【超】!come on...
不对不对。的确就是存的时候不对。
a[i]=a[i-1]+i
这个的时候就不对了。10是当成1个数用的。不信斑竹你自己数一下。
#include <stdio.h>
#include <math.h>
#define N 5
const unsigned long total_length[N] = { 0, 45, 9045, 1395495, 189414495};
const unsigned long first[N + 1] = {0, 1, 11, 192, 2893, 38894};
const unsigned long last[N] = {0, 9, 189, 2889, 38889};
int main()
{
unsigned i, j, n;
unsigned long index;
scanf("%ld", &n);
for (i = 0; i < n; i++)
{
unsigned len, t, k;
scanf("%ld", &index);
for (j = 1; j < N; j++)
if (index <= total_length[j])
break;
len = j;
index -= total_length[len - 1];
for (j = 1; ; j++)
if ((first[len] * j + j * (j - 1) * len / 2) >= index)
break;
index = index - first[len] * (j - 1) - (j - 1) * (j - 2) * len / 2;
for (j = 1; j < N; j++)
if (index <= last[j])
break;
if (j == 1)
printf("%d\n", index);
else
{
index -= last[j - 1];
t = (index - 1) / j + (int) pow(10, j - 1);
k = t / (int) pow(10, j - 1 - (index-1) % j) % 10;
printf("%d\n", k);
}
}
return 0;
}
那我加一点注释,希望有助理解
#include <stdio.h>
#include <math.h>
#define N 5
const unsigned long total_length[N] = { 0, 45, 9045, 1395495, 189414495};/*位数分别为1,2,3,4时的序列总长度*/
const unsigned long first[N + 1] = {0, 1, 11, 192, 2893, 38894};/*位数分别为1,2,3,4,5时第一个序列的长度*/
const unsigned long last[N] = {0, 9, 189, 2889, 38889};/*位数分别为1,2,3,4时最后一个序列的长度*/
int main()
{
unsigned i, j, n;
unsigned long index;
scanf("%ld", &n);
for (i = 0; i < n; i++)
{
unsigned len, t, k;
scanf("%ld", &index);
for (j = 1; j < N; j++) /*判断index所处序列的位数,并存如len*/
if (index <= total_length[j])
break;
len = j;
index -= total_length[len - 1];
for (j = 1; ; j++) /*判断在位数为len的序列中所处的位置*/
if ((first[len] * j + j * (j - 1) * len / 2) >= index) /*等差序列求和公式a1*n+n*(n-1)*k/2 */
break;
index = index - first[len] * (j - 1) - (j - 1) * (j - 2) * len / 2;/*得到小序列中的位置*/
for (j = 1; j < N; j++) /*判断所求数字在几位数区域*/
if (index <= last[j])
break;
if (j == 1)
printf("%d\n", index);
else
{
index -= last[j - 1];
t = (index - 1) / j + (int) pow(10, j - 1);/*t为所求数字所在具体的数中*/
k = t / (int) pow(10, j - 1 - (index-1) % j) % 10;
printf("%d\n", k);
}
}
return 0;
}
第二个
#include<iostream.h>
#include<stdio.h>
int main()
{
int i;
long s,n;
while(EOF!=(scanf("%ld",&n))&&n!=0)
{
if(n%2==0||n<2)//是偶数或小于2就直接输出找不到.
{
printf("2^? mod %ld = 1\n",n);
}
else
{
s=1;i=0;
while(1)//这里主要是找规律,想了很久的,不过以前做的,代几个数看看吧.
{
if(s<n)
{
s=s*2;
i++;
}
else
{
s=s-n;
}
if(s==1)
break;
}
printf("2^%d mod %ld = 1\n",i,n);
}
}
return(0);
}