刚学半年の新手求调,写得迷迷糊糊的
查看原帖
刚学半年の新手求调,写得迷迷糊糊的
1029978
zyh1145楼主2023/9/24 22:43

也是数越大越不准

emm 这就是尝试用数组代替字符串的后果(

代码:(shuwei函数会返回输入数的数位总数如114514返回6)

数组a的前面(大概a[70]以前)我都检查了没错,后面根本没法检查(悲

#include <bits/stdc++.h>
using namespace std;
int n,l,r;
int x,sum,ans,swx,px;
int a[1000001];
bool flag;
int shuwei(int x)
{
	int p = x,s = 0;
	if (p < 10)
	{
		return 1;
	}
	while(p)
	{
		p /= 10;
		s++;
	}
	return s;
}
int main()
{
	cin>>n;
	int cnt = 1;
	while(cnt <= 20001)
	{
		x++;
		swx = shuwei(x);
		for (int j = 1;j <= x;j++)
		{
			if (swx == 1)
			{
				a[cnt] = x;
				//a[cnt] += a[cnt-1];
				cnt++;
				continue;
			}
			else
			{
				px = x;
				//cout<<"swx="<<swx<<endl;
				for (int k = 0;k < swx;k++)
				{
					a[cnt+swx-k-2] = px % 10;
					px /= 10;
					//cout<<"cnt+swx-k-2="<<cnt+swx-k-2<<" px%10="<<px%10<<'\n'; 
				}
				cnt += swx;
			}
		}
	}
	cnt = 1;
	while(cnt <= 20001)
	{
		//a[cnt] += a[cnt-1];
		cout<<"a["<<cnt<<"]="<<a[cnt]<<endl;
		cnt++;
	}
	while(n--)
	{
		scanf("%d%d",&l,&r);
		int s = 0;
		for (int i = l;i <= r;i++)
		{
			s += a[i]; 
		} 
		printf("%d\n",s);
		//printf("%d\n",a[r] - a[l-1]);
	}
    return 0;
}
2023/9/24 22:43
加载中...