Poj 1090 chain

Source: Internet
Author: User
A 0 1 string requires you to change all to 0. The change rule is that when the nth string is changed, the n-1 string must be 1, and all the values before n-1 must be 0, n> = 3; 1st can be changed at will, and 2nd must be changed when 1st is set to 1. How many times can we change the value of all the given 0 1 strings to 0?
First of all, I personally think about this type of question: when I look at the data and results so big that the search will definitely not work, it should be a regular formula question. When I think of hanruata, I should first look for a formula; Z [N] Function Representation
For 0 0 0 ...... The Nth Number of 0 is 1, and X [N] indicates 0 0 ...... The Nth number (1) of 1 0 is 0
Then there is Z [N] = Z [n-1] + 1 + x [n-1];… 1
     x[n]=z[n-1]+1+x[n-1];  ……2
Z [N] = 2 ^ n-1; X [N] = 2 ^ n-1;
 
 
 
A For loop can find the answer ans. The answer is stored in binary. The problem is mainly difficult to convert the binary into decimal because I feel that my thinking is correct, but the teacher times out, I am so angry that I have been making code changes, and I am confused by my own ideas. Therefore, the code quality is very poor. For details, see comments. Haha # include <stdio. h> # include <string. h> int array [1001]; int res [1001], ANS [1001]; // The binary string of ANS is saved. The decimal value converted from ANS is int INI () in res () {int Z, flag = 0; For (Z = 1; Z <= 1000; Z ++) {res [Z] + = flag; flag = res [Z]/10; Res [Z] % = 10;} return 0;} int add (int * flag, int fnum) {int I; for (I = 1; I <= fnum; I ++) RES [I] + = Flag [I]; return INI ();} int getres (Int n) {int I, j, Z, flag [1001], FF, fnum = 1; memset (flag, 0, sizeof (FLAG )); flag [1] = 1; for (I = 1; I <= N; I ++) {If (ANS [I]) add (flag, fnum ); for (j = 1; j <= fnum; j ++) {flag [J] * = 2;} FF = 0; For (Z = 1; Z <= fnum; Z ++) {flag [Z] + = ff; FF = Flag [Z]/10; flag [Z] % = 10;} while (FF! = 0) {flag [++ fnum] = FF % 10; FF/= 10 ;}return 0 ;}int main () {int I; int N, flag [1001], xx; while (scanf ("% d", & N )! = EOF) {memset (flag, 0, sizeof (FLAG); // the target State is 0 at the beginning; memset (Res, 0, sizeof (RES )); // initialize memset (ANS, 0, sizeof (ANS) for the decimal answer; // initialize the binary answer for (I = 1; I <= N; I ++) scanf ("% d", & array [I]); for (I = N; I> = 1; I --) // greedy forward from the back, for example, to solve the fourth different problem, you must change the first three States to 0 0 1 {If (flag [I] = array [I]). // if the problem is the same, no conversion is required; continue; flag [I-1] = 1; // if not the same, then you need to convert the previous 0 0 0 ...... 1 and then add step I to the same, and then add the first N-1 (0 0 0 ...... 1) The result is 0, and the formula is 2 ^ n-1, which corresponds to the n + 1 digit in the binary. ans [I] = 1 ;} // The binary calculation is followed by the conversion problem. You need to pay attention to it in one place, and there is nothing to mention about getres (n); xx = 0; for (I = 1000; i> = 1; I --) {If (xx = 0 & res [I] = 0) continue; xx = 1; printf ("% d ", res [I]);} If (xx = 0) printf ("0"); printf ("\ n");} return 0 ;}

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.