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'; //每次查字W时的前一个字W?/span> for (size_t i = 0; i != expr.size(); ++i) { constchar ch = expr[i]; if (isspace(ch)) { //如果是空|卌q?/span> continue; } elseif (isdigit(ch)) { //如果是数字,则压入操作数?/span> opnd.push(ch - '0'); } else { if ((ch == '-' || ch == '+') && (last_ch == '\0' || last_ch == '(')) //遇到 '+'?-'Q则压入0q操作数?/span> opnd.push(0); //?nbsp;7-1Q遇'-'则压?q栈Q,'-'则进操作W栈Q遇C一个数1Q计?-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); //数字{换ؓ字符数组Q也可以用itoa函数?sprintf函数 for(i=0,j=len-1;i < len/2;i++,j--) { if(str[i] != str[j]) ok = 0;
]]>子串是否存?/title>http://m.shnenglu.com/zhenglinbo/archive/2012/08/18/187611.htmlhoshellyhoshellySat, 18 Aug 2012 13:14:00 GMThttp://m.shnenglu.com/zhenglinbo/archive/2012/08/18/187611.htmlhttp://m.shnenglu.com/zhenglinbo/comments/187611.htmlhttp://m.shnenglu.com/zhenglinbo/archive/2012/08/18/187611.html#Feedback0http://m.shnenglu.com/zhenglinbo/comments/commentRss/187611.htmlhttp://m.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: "); //输入的子串Q按先a的子Ԍ后非a的子串输?/span> gets(b);
]]>链表节点?/title>http://m.shnenglu.com/zhenglinbo/archive/2012/08/17/187433.htmlhoshellyhoshellyThu, 16 Aug 2012 17:43:00 GMThttp://m.shnenglu.com/zhenglinbo/archive/2012/08/17/187433.htmlhttp://m.shnenglu.com/zhenglinbo/comments/187433.htmlhttp://m.shnenglu.com/zhenglinbo/archive/2012/08/17/187433.html#Feedback0http://m.shnenglu.com/zhenglinbo/comments/commentRss/187433.htmlhttp://m.shnenglu.com/zhenglinbo/services/trackbacks/187433.html 实现代码如下Q?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; }