Algorithm and analysis of statistical problems and integer factorization problem?

Source: Internet
Author: User


The page number of a book is encoded from the natural number 1 until the number of natural numbers n is arranged according to the usual custom, and each page number contains no extra leading digits 0 For example, the 6th page is represented by the number 6 instead of 06 or 006. The number counting problem requires that the total page number of the given book be calculated by the number of times the page is used in all pages of the book 0 1 2 ..., 9
Programming tasks:
A 10-binary integer n (1≤n≤1000000000) Given the total page number of the representation book. How many numbers are used in the total number of programming books 0 1 2 ..., 9
Data input:
There are m input data, the 1th line is the number of M, and the next M-line gives an integer n representing the total page number of the book.
Result output:
Output has M line, in each row output page number used to number k-1 number of times, K=1 2 ..., 10, separated by a space in the middle.
Input Sample:
2
5
11
Sample output:
1 1 1 1 1 0 0 0 0 0
1 4 1 1 1 1 1 1 1 1
Run time limit 30 seconds run memory limit 8192K


2 Problem Description:
A positive integer greater than 1 n can be decomposed into: N=X1*X2*...*XM.
For example, when n=12, there are 8 different types of decomposition:
12=12;
12=6*2;
12=4*3;
12=3*4;
12=3*2*2;
12=2*6;
12=2*3*2;
12=2*2*3.
Programming tasks:
For a given positive integer n, there are a number of different decomposition types for programming n.
Data input:
There are m input data, the 1th line is the number of M, the next m row has 1 positive integer n (1≤n≤1000000).
Result output:
The output has m rows, each of which is calculated as a different number of decomposition types.
Input Sample:
1
12
Sample output:
8

Run time limit 30 seconds run memory limit 8192K
Programming 09-08-154 sticks 99 released
2 Answer time votes

0
A person's decadence


1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 2048//If you have more than 2048 books to calculate, then you need to apply for memory.

void func (unsigned int (*ptr) [ten], int m, int idx)
{
int I, J, H;
for (I=1; i<=m; ++i) {//This for can be improved by the algorithm to improve efficiency, that is, according to high-level numbers to get low 0-9 of the number of occurrences, accumulate on the line, the algorithm is simple
j = i;
while (J > 0) {
++PTR[IDX][J%10];
J/= 10;
}
}
}

int main ()
{
unsigned int arr[2048][10] = {0}; Save 0-9 times per book
unsigned int (*ptr) [10] = 0;

unsigned int m[n], n;//m[n] Save the number of pages per book
unsigned int *m_ptr;
unsigned int i;

printf ("Input N (the lines of M):");
scanf ("%u", &n);
if (n > N) {//If the book is more than 2048, request memory to save
PTR = (unsigned int (*) [ten]) malloc (n*10*sizeof (unsigned int));
M_ptr = (unsigned int *) malloc (n*sizeof (unsigned int));
}
else {
ptr = arr;
M_ptr = m;
}

printf ("Input%u numbers:", n);
for (i=0; i<n; ++i) {
scanf ("%u", m_ptr + i);
}

for (i=0; i<n; ++i) {/////with the above for separate, you can avoid disturbing the compiler to optimize the input buffer, you can close up.
Func (PTR, m_ptr[i], i);
}
Output 0-9
printf ("Number:%5u%5u%5u%5u%5u%5u%5u%5u%5u%5u\n", 0, 1, 2, 3, 4, 5, 6, 7, 8,
9);
for (i=0; i<n; ++i) {//output each book 0-9 times and Total pages (with a single page book, such a book is really powerful ...)
printf ("Times:%5u%5u%5u%5u%5u%5u%5u%5u%5u%5u [total pages:%5u]\
N ", Ptr[i][0], ptr[i][1], ptr[i][2], ptr[i][3], ptr[i][4], ptr[i][5], ptr[i][6], ptr[i][7], ptr
I [8], ptr[i][9], m_ptr[i]);
}

if (ptr! = arr) {
Free (PTR);
Free (M_PTR);
}
}

Algorithm and analysis of statistical problems and integer factorization problem?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.