Acm--children ' s Queue

Source: Internet
Author: User

Topic Background:


Previous revision code:

#include <iostream> #include <string> using namespace std;
string getcnt (int);
String Sumstring (String, string);
char Inttochar (int);

int Chartoint (char);
	
	int main () {int m;
	while (cin>>m) {cout<<getcnt (m) <<endl;
} return 0;
	} string getcnt (int n) {if (n = = 1) {return "1";
	} if (n = = 2) {return "2";
	} if (n = = 3) {return "4";
	} if (n = = 4) {return "7";
	} String f1 = "1";
	string F2 = "2";
	String F3 = "4";
	String F4 = "7";
	
 	string sum;
		for (int i=4; i<n; i++) {sum = sumstring (F4, F3); 
		sum = sumstring (sum, F1);
		F1 = F2;
		F2 = F3;
		F3 = F4;
	F4 = sum;
} return sum;
	} string Sumstring (String f1, string f2) {int len1 = F1.length ();
	int len2 = F2.length ();
	
	int minlen = (len1>len2)? Len2:len1;
		if (LEN2&GT;LEN1) {string str;
		str = F2;
		F2 = F1;
	f1 = str;
	}//the F1 to the longer one, and the final return is F1 int lastinteger;
	int Jinwei = 0;
	int Val; for (int i=0; i<minlen; i++) {val = Chartoint (F1[len1-i-1]) + chartoint (F2[len2-i-1]) + Jinwei;
		Jinwei = 0;
			if (Val > 9) {jinwei = 1;
		val = val% 10;
	} f1[len1-i-1] = Inttochar (val);
			} if (Jinwei! = 0) {for (int i=minlen; i<f1.length (); i++) {val = Chartoint (F1[len1-i-1]) + Jinwei;
			Jinwei = 0;
				if (Val > 9) {jinwei = 1;
			val = val% 10;
		} f1[len1-i-1] = Inttochar (val);
	}} if (Jinwei! = 0) {f1 = "1" + F1; 
} return F1;

} int Chartoint (char c) {return (int) c-48;} char Inttochar (int n) {return (char) (n + 48);}
Sorry, Runtimee rror.

#include <iostream> #include <string> using namespace std;
string getcnt (int);
String Sumstring (String, String, string);
char Inttochar (int);

int Chartoint (char);
	
	int main () {int m;
	while (cin>>m) {cout<<getcnt (m) <<endl;
} return 0;
	} string getcnt (int n) {if (n = = 1) {return "1";
	} if (n = = 2) {return "2";
	} if (n = = 3) {return "4";
	} if (n = = 4) {return "7";
	} String f1 = "1";
	string F2 = "2";
	String F3 = "4";
	String F4 = "7";
	
 	string sum;
		for (int i=4; i<n; i++) {sum = sumstring (F4, F3, F1);
		F1 = F2;
		F2 = F3;
		F3 = F4;
	F4 = sum;
} return sum;
	} int max (int A, int b, int c) {if (a>=b) {return (a>c) a:c;
	} else {return (b>c)? B:c;
	}} string Sumstring (String f1, String F2, string f3) {int len1 = F1.length ();
	int len2 = F2.length ();
	int len3 = F3.length ();
	
	int maxlen = max (Len1, Len2, Len3);	
	String str;
		if (len2 = = maxlen) {str = F2;
		F2 = F1;		
	f1 = str; } if (lEn3 = = maxlen) {str = F3;
		F3 = F1;
	f1 = str;
	}//the F1 to the longer one, and the final return is F1 len1 = F1.length ();
	Len2 = F2.length ();
	
	Len3 = F3.length ();
	int Lastinteger;
	int Jinwei = 0;
	int val = 0; for (int i=0; i<maxlen; i++) {//val = Chartoint (F1[len1-i-1]) + chartoint (F2[len2-i-1]) + chartoint (F3[len3-
		I-1]) + Jinwei;
		if (len1-i-1>=0) {val = (int) (F1[len1-i -1]-48);
		} if (len2-i-1>=0) {val = val + (int) (F2[len2-i-1]-48);
		} if (len3-i-1>=0) {val = val + (int) (F3[len3-i-1]-48);
		
		} val + = Jinwei;
		Jinwei = 0;
			if (Val > 9) {jinwei = VAL/10;
		val = val% 10;
	} f1[len1-i-1] = (char) (Val + 48);
	} if (Jinwei = = 1) {f1 = "1" + F1;
	} else if (Jinwei = = 2) {f1 = "2" + F1; 
} return F1;

} int Chartoint (char c) {return (int) c-48;} char Inttochar (int n) {return (char) (n + 48);}
Improvement, the two-time addition is changed to one addition, that is, three string once to do the addition, the result or runtime error, in fact, this efficiency must be improved, but has not improved to the point.

#include <iostream> #include <string> using namespace std;
string getcnt (int);
String Sumstring (String, String, string);
char Inttochar (int);
int Chartoint (char);

String strarray[1001];
	int main () {int m;
	for (int i=1; i<1001; i++) {Strarray[i] = getcnt (i);
	} while (cin>>m) {cout<<strarray[m]<<endl;
} return 0;
	} string getcnt (int n) {if (n = = 1) {return "1";
	} if (n = = 2) {return "2";
	} if (n = = 3) {return "4";
	} if (n = = 4) {return "7";
	} String f1 = "1";
	string F2 = "2";
	String F3 = "4";
	String F4 = "7";
	
 	string sum;
		for (int i=4; i<n; i++) {sum = sumstring (F4, F3, F1);//This is still called, causing multiple computations f1 = F2;
		F2 = F3;
		F3 = F4;
	F4 = sum;
} return sum;
	} int max (int A, int b, int c) {if (a>=b) {return (a>c) a:c;
	} else {return (b>c)? B:c;
	}} string Sumstring (String f1, String F2, string f3) {int len1 = F1.length ();
	int len2 = F2.length ();
	int len3 = F3.length (); int maxlen = MAX (Len1, Len2, Len3);	
	String str;
		if (len2 = = maxlen) {str = F2;
		F2 = F1;		
	f1 = str;
		} if (Len3 = = maxlen) {str = F3;
		F3 = F1;
	f1 = str;
	}//the F1 to the longer one, and the final return is F1 len1 = F1.length ();
	Len2 = F2.length ();
	
	Len3 = F3.length ();
	int Lastinteger;
	int Jinwei = 0;
	int val = 0; for (int i=0; i<maxlen; i++) {//val = Chartoint (F1[len1-i-1]) + chartoint (F2[len2-i-1]) + chartoint (F3[len3-
		I-1]) + Jinwei;
		if (len1-i-1>=0) {val = (int) (F1[len1-i -1]-48);
		} if (len2-i-1>=0) {val = val + (int) (F2[len2-i-1]-48);
		} if (len3-i-1>=0) {val = val + (int) (F3[len3-i-1]-48);
		
		} val + = Jinwei;
		Jinwei = 0;
			if (Val > 9) {jinwei = VAL/10;
		val = val% 10;
	} f1[len1-i-1] = (char) (Val + 48);
	} if (Jinwei = = 1) {f1 = "1" + F1;
	} else if (Jinwei = = 2) {f1 = "2" + F1; 
} return F1;

} int Chartoint (char c) {return (int) c-48;} char Inttochar (int n) {return (char) (n + 48);}
Improved mode: In Main, the result exists in the array, directly according to the input index, to the array inside the result can be, think carefully, this is actually the last version of the program, executed 1001 times just, actually there is no improvement, to do improvement, it must be improved in the function, Improvements made at the point where the function is referenced are false.

#include <iostream> #include <string> using namespace std;
string getcnt (int);
String Sumstring (String, String, string);
char Inttochar (int);
int Chartoint (char);

String strarray[1001];
	int main () {int m;
	for (int i=1; i<1001; i++) {Strarray[i] = getcnt (i);
	} while (cin>>m) {cout<<strarray[m]<<endl;
} return 0;
	} string getcnt (int n) {if (n = = 1) {return "1";
	} if (n = = 2) {return "2";
	} if (n = = 3) {return "4";
	} if (n = = 4) {return "7";
	} String f1 = "1";
	string F2 = "2";
	String F3 = "4";
	
	String F4 = "7";
	STRARRAY[1] = "1";
	STRARRAY[2] = "2";
	STRARRAY[3] = "4"; 
	STRARRAY[4] = "7";
	
 	string sum; for (int i=4; i<n; i++) {sum = sumstring (Strarray[i], strarray[i-1], strarray[i-3]);//This is still being called, resulting in multiple computations//strarray[
		
1] = strarray[2];
F1 = F2;
F2 = F3;
F3 = F4;
	F4 = sum;
} return sum;
	} int max (int A, int b, int c) {if (a>=b) {return (a>c) a:c; } else {return (b>c)?B:C;
	}} string Sumstring (String f1, String F2, string f3) {int len1 = F1.length ();
	int len2 = F2.length ();
	int len3 = F3.length ();
	
	int maxlen = max (Len1, Len2, Len3);	
	String str;
		if (len2 = = maxlen) {str = F2;
		F2 = F1;		
	f1 = str;
		} if (Len3 = = maxlen) {str = F3;
		F3 = F1;
	f1 = str;
	}//the F1 to the longer one, and the final return is F1 len1 = F1.length ();
	Len2 = F2.length ();
	
	Len3 = F3.length ();
	int Lastinteger;
	int Jinwei = 0;
	int val = 0;
		for (int i=0; i<maxlen; i++) {if (len1-i-1>=0) {val = (int) (F1[len1-i -1]-48);
		} if (len2-i-1>=0) {val = val + (int) (F2[len2-i-1]-48);
		} if (len3-i-1>=0) {val = val + (int) (F3[len3-i-1]-48);
		
		} val + = Jinwei;
		Jinwei = 0;
			if (Val > 9) {jinwei = VAL/10;
		val = val% 10;
	} f1[len1-i-1] = (char) (Val + 48);
	} if (Jinwei = = 1) {f1 = "1" + F1;
	} else if (Jinwei = = 2) {f1 = "2" + F1; 
} return F1;

} int Chartoint (char c) {return (int) c-48;} Char InttOchar (int n) {return (char) (n + 48);} 
In the getcnt (string) has been improved, the Strarray as a process variable, and abandoned the F1,F2 such an intermediate variable, where the change and the previous version of the same, main inside every for once, the Strarray has been assigned a value, no practical significance.

/** These versions of the problem review: Compared to the previous version, the main function has changed a lot, only once Getresultarray (), and the result is stored in the string array, according to the input subscript index to take it. The problem with the previous version is that the amount of process before the strarray is not actually used, and the number of GetString () is called for each time in main, which results in a process volume or no real use of */#include <iostream> #

Include<string> using namespace std;
void Getresultarray ();
String Sumstring (String, String, string);
char Inttochar (int);
int Chartoint (char);

String strarray[1001];
	
	int main () {int m;
	Getresultarray ();
	while (cin>>m) {cout<<strarray[m]<<endl;
} return 0;
	} void Getresultarray () {String f1 = "1";
	string F2 = "2";
	String F3 = "4";
	
	String F4 = "7";
	STRARRAY[1] = "1";
	STRARRAY[2] = "2";
	STRARRAY[3] = "4"; 
	
 	STRARRAY[4] = "7"; 
	for (int i=5; i<1001; i++) {Strarray[i] = sumstring (Strarray[i-1], strarray[i-2], strarray[i-4]);//This is still being called, resulting in multiple computations
	}} int max (int A, int b, int c) {if (a>=b) {return (a>c)? A:c;
	} else {return (b>c)? B:c; }} string Sumstring (String f1, String F2, string f3) {int len1 = F1.length ();
	int len2 = F2.length ();
	int len3 = F3.length ();
	
	int maxlen = max (Len1, Len2, Len3);	
	String str;
		if (len2 = = maxlen) {str = F2;
		F2 = F1;		
	f1 = str;
		} if (Len3 = = maxlen) {str = F3;
		F3 = F1;
	f1 = str;
	}//the F1 to the longer one, and the final return is F1 len1 = F1.length ();
	Len2 = F2.length ();
	
	Len3 = F3.length ();
	int Lastinteger;
	int Jinwei = 0;
	int val = 0;
		for (int i=0; i<maxlen; i++) {if (len1-i-1>=0) {val = (int) (F1[len1-i -1]-48);
		} if (len2-i-1>=0) {val = val + (int) (F2[len2-i-1]-48);
		} if (len3-i-1>=0) {val = val + (int) (F3[len3-i-1]-48);
		
		} val + = Jinwei;
		Jinwei = 0;
			if (Val > 9) {jinwei = VAL/10;
		val = val% 10;
	} f1[len1-i-1] = (char) (Val + 48);
	} if (Jinwei = = 1) {f1 = "1" + F1;
	} else if (Jinwei = = 2) {f1 = "2" + F1; 
} return F1;

} int Chartoint (char c) {return (int) c-48;} char Inttochar (int n) {return (char) (n + 48);}
The whole structure is changed, the main function only executes once getcnt (here changed to Getresultarray function), is to strarray only once assignment, efficiency greatly improved.

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.