int calculate(int a, char optr, int b) { switch (optr) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } }
int evaluate(conststring& expr) { stack<int> opnd; stack<char> optr; char last_ch = '\0'; //姣忔媯鏌ュ瓧絎︽椂鐨勫墠涓涓瓧絎?/span> for (size_t i = 0; i != expr.size(); ++i) { constchar ch = expr[i]; if (isspace(ch)) { //濡傛灉鏄┖鏍鹼紝鍗寵煩榪?/span> continue; } elseif (isdigit(ch)) { //濡傛灉鏄暟瀛楋紝鍒欏帇鍏ユ搷浣滄暟鏍?/span> opnd.push(ch - '0'); } else { if ((ch == '-' || ch == '+') && (last_ch == '\0' || last_ch == '(')) //閬囧埌 '+'銆?-'錛屽垯鍘嬪叆0榪涙搷浣滄暟鏍?/span> opnd.push(0); //濡?nbsp;7-1錛岄亣'-'鍒欏帇鍏?榪涙爤錛岋紝'-'鍒欒繘鎿嶄綔絎︽爤錛岄亣鍒頒笅涓涓暟1錛岃綆?-1寰?1 if (optr.empty() || ch == '(' || (optr.top() == '(' && ch != ')') || getPrecedence(ch) > getPrecedence(optr.top())) { optr.push(ch); } else { while (!optr.empty() && optr.top() != '(' && getPrecedence(ch) <= getPrecedence(optr.top())) { int b = opnd.top(); opnd.pop(); int a = opnd.top(); opnd.pop(); opnd.push(calculate(a, optr.top(), b)); optr.pop(); } if (ch == ')') optr.pop(); else optr.push(ch); } } last_ch = ch; } while (!optr.empty()) { int b = opnd.top(); opnd.pop(); int a = opnd.top(); opnd.pop(); opnd.push(calculate(a, optr.top(), b)); optr.pop(); } int result = opnd.top(); opnd.pop(); return result; }
int main() { int n; cin >> n; while (n-- > 0) { string expr; cin>>expr; cout << evaluate(expr) << endl; } return 0; }
The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 1000,000,000); both a and b are considered to be within the range .
Input
Line 1: Two integers, a and b
Output
The list of palindromic primes in numerical order, one per line.
Sample Input
5 500
Sample Output
5
7
11
101
131
151
181
191
313
353
373
383
#include<stdio.h> #include<math.h> #include<stdlib.h> int i; int Is_Prime(int a) { double t = a; for(i=2;i<=sqrt(t);i++) { if(a % i == 0) return 0; } return 1; } int diglen(int c) { if(c/10 == 0) return 1; int count=0; while(c) { c=c/10; count++; } return count; }
void IntoStr(int a,char* str) //灝嗘暟瀛楄漿鎹負瀛楃涓?/span> { int h= diglen(a)-1,i=0; while(h+1) { int k,c,d=1; for(k=0;k<h;k++) { d=d*10; }
int Is_Pal(int b) { int len = diglen(b); int ok = 1,j; char* str = (char*)malloc(sizeof(char)*(len+1)); IntoStr(b,str); //灝嗘暟瀛楄漿鎹負瀛楃鏁扮粍錛屼篃鍙互鐢╥toa鍑芥暟鎴?sprintf鍑芥暟 for(i=0,j=len-1;i < len/2;i++,j--) { if(str[i] != str[j]) ok = 0;
]]>媯嫻嬪瓙涓叉槸鍚﹀瓨鍦?/title>http://www.shnenglu.com/zhenglinbo/archive/2012/08/18/187611.htmlhoshellyhoshellySat, 18 Aug 2012 13:14:00 GMThttp://www.shnenglu.com/zhenglinbo/archive/2012/08/18/187611.htmlhttp://www.shnenglu.com/zhenglinbo/comments/187611.htmlhttp://www.shnenglu.com/zhenglinbo/archive/2012/08/18/187611.html#Feedback0http://www.shnenglu.com/zhenglinbo/comments/commentRss/187611.htmlhttp://www.shnenglu.com/zhenglinbo/services/trackbacks/187611.html#include<stdio.h> #include<string.h> #define N 1000 int main() { char a[N],b[N]; int i,j=0,k,count=1,z; staticint c=0; printf("Input the a string: "); //杈撳叆瀛楃涓?/span> gets(a); printf("Input the substring: "); //杈撳叆媯嫻嬬殑瀛愪覆錛屾寜鍏坅鐨勫瓙涓詫紝鍚庨潪a鐨勫瓙涓茶緭鍏?/span> gets(b);
]]>閾捐〃鑺傜偣鏁?/title>http://www.shnenglu.com/zhenglinbo/archive/2012/08/17/187433.htmlhoshellyhoshellyThu, 16 Aug 2012 17:43:00 GMThttp://www.shnenglu.com/zhenglinbo/archive/2012/08/17/187433.htmlhttp://www.shnenglu.com/zhenglinbo/comments/187433.htmlhttp://www.shnenglu.com/zhenglinbo/archive/2012/08/17/187433.html#Feedback0http://www.shnenglu.com/zhenglinbo/comments/commentRss/187433.htmlhttp://www.shnenglu.com/zhenglinbo/services/trackbacks/187433.html 瀹炵幇浠g爜濡備笅錛?br />
#include<stdio.h> #include<stdlib.h> typedef struct node *link; struct node{ int item; link next; };
int node_number(link p,int n) { int count=0,i; for(i=0;i<n-1;i++) { p=p->next; } while(p->item) { p->item=0; p=p->next; count++; } return count; }
int main() {
int i,N; link t=(link)malloc(sizeof(node)); t->item=1; t->next=t; link x=t; for(i=2;i<=10;i++) { x = (x->next= (link)malloc(sizeof(node))); x->item=i; x->next=t; } printf("Please input the order of node: "); scanf("%d",&N); printf("total number of nodes is: %d\n",node_number(t,N)); return 0; }