首页 > 其他分享 >poj-2282

poj-2282

时间:2023-05-23 16:01:52浏览次数:31  
标签:leftPart 10 begin int 2282 curVal poj appearArray


// 380K	32MS	G++
#include <stdio.h>
#include <string.h>
#include <math.h>

long long appearTime1[10];
long long appearTime2[10];

void getAppearTime(int num, long long * appearArray) {
	appearArray[0] = 1;
	if (num == 0) {
		return;
	}

	int divisor1= 1;
	int divisor2= 10;
	int rightLength = 0;

	while(1) {
		if (divisor1 > num) {
			break;
		}
		int curVal = (num/divisor1)%10;
		int rightPart = num%divisor1;
		int leftPart = num/divisor2;

		// printf("curVal is %d, rightPart is %d, leftPart is %d\n", curVal, rightPart, leftPart);

		int begin = 0;
		// reach the most left unit, 0 can not appear here
		if (!leftPart) {
			begin = 1;
		}

		if (curVal > 0) {
			appearArray[0] += (leftPart)*pow(10, rightLength);
		} else {//curVal == 0; 
			appearArray[curVal] += (leftPart-1)*pow(10, rightLength);
			appearArray[curVal] += rightPart;
		}

		// for numbers < curVal
		for (int i = 1; i < curVal; i++) {
			appearArray[i] += (leftPart+1)*pow(10, rightLength);
		}

		// for number == curVal
		if (leftPart > 0 && curVal > 0) {
			appearArray[curVal] += (leftPart)*pow(10, rightLength);
		}

		if (rightPart > 0 && curVal > 0) {
			appearArray[curVal] += rightPart;
		}
		
		for (int i = curVal + 1; i <= 9; i++) {
			appearArray[i] += (leftPart)*pow(10, rightLength);
		}

		// for numbers > curVal
		rightLength++;
		divisor1 *= 10;
		divisor2 *= 10;
	}

}

int begin;
int end;
int main() {
	while(scanf("%d %d", &begin, &end) != EOF) {
		if (!begin && !end) {
			return 0;
		}

		int max = begin > end ? begin: end;
		int min = begin <= end ? begin: end;

		memset(appearTime1, 0, sizeof(appearTime1));
		memset(appearTime2, 0, sizeof(appearTime2));
		getAppearTime(max + 1, appearTime1);
		if (min > 0) {
			getAppearTime(min, appearTime2);
		}


		// for (int i = 0; i <= 9; i++) {
		// 	printf("%lld", appearTime1[i]);
		// 	if (i == 9) {
		// 		printf("\n");
		// 	} else {
		// 		printf(" ");
		// 	}
		// }

		// for (int i = 0; i <= 9; i++) {
		// 	printf("%lld", appearTime2[i]);
		// 	if (i == 9) {
		// 		printf("\n");
		// 	} else {
		// 		printf(" ");
		// 	}
		// }

		for (int i = 0; i <= 9; i++) {
			printf("%lld", appearTime1[i] - appearTime2[i]);
			if (i == 9) {
				printf("\n");
			} else {
				printf(" ");
			}
		}
	}
}

按照javascript:void(0)的思想进行了试刀,

果然比我原来的思路要来的简洁, 不过还是会存在一些特殊情况,比如某一位是0, 以及最高位.

标签:leftPart,10,begin,int,2282,curVal,poj,appearArray
From: https://blog.51cto.com/u_9420214/6332992

相关文章

  • poj-2249
    //356K16MSG++//356K0MSG++addm==0check//356K16MSG++//356K0MSG++addm==0check#include<stdio.h>#include<string.h>#include<math.h>intm;intn;//voidgetNum(unsignedintn,unsignedintm){//......
  • poj-1037
    //196K16MSC++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=25;longlongDP[MAX][MAX][2];//0:down.1:upvoidinit(){for(intcurPlankNum=1;curPlankNum<=20;curPlankNum++){for(......
  • poj-2140
    //132K 110MS C++#include<cstring>#include<cstdio>usingnamespacestd;intN;longlongcnt;voidsolve(intN){ intbegin=1; intend=1; longlongsum=1; while(1){ if(begin>N){ break; } //if(begin==......
  • poj-1988
    //564K 282MS C++#include<cstdio>#include<cstring>#include<iostream>usingnamespacestd;structUF_Node{ intup; intcount; intparent;};typedefstructUF_NodeUF_Node;UF_NodeUF_Array[30001];voidcreat(){ intc; for(......
  • poj-1693
    //136K 0MS C++#include<cstdio>#include<cstring>structLine{ intbx,ex; intby,ey;};typedefstructLineLine;LinehLine[110];LinevLine[110];intcaseNum;intLineNum;boolinsect(Line&vline,Line&hline){ //pr......
  • POJ1737 Connected Graph ( n点无向连通图计数
    题意说明:求\(n\)个点的无向连通图个数据说已经非常典了,但是我太菜了不会组合数学,最近补档时看到这道题,决定记录下来理理思路......
  • POJ--1163 The Triangle(DP)
    记录10:432023-5-15http://poj.org/problem?id=1163reference:《挑战程序设计竞赛(第2版)》第二章练习题索引p135DescriptionFigure1showsanumbertriangle.Writeaprogramthatcalculatesthehighestsumofnumberspassedonaroutethatstartsatthetopa......
  • 实验六-Salt本地pojie实验
    【实验目的】了解Salt型密码的加密机制,学会使用本地密码pojie工具来pojieSalt型密码,了解pojie密码原理。【知识点】Salt,密码pojie【实验原理】1.Salt概念在密码保护技术中,salt是用来修改口令散列的随机数据串。可将salt加入散列,即使系统的另一用户也选用了同一口令,也可通过唯......
  • POJ 动态规划题目列表
    声明:1.这份列表当然不是我原创的,从文库里下载了一份,放到这里便于自己浏览和查找题目。※最近更新:Poj斜率优化题目1180,2018,3709列表一:经典题目题号:容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458......
  • poj018(2)
    再贴一版poj1018,其实与之前的那一版差不多,只是去掉了注释,这样可能看起来会舒服一点packagecom.njupt.acm;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.util.Arrays;importjava.util.Scanner;publicclassTestPOJ1018{pu......