scanf的疑惑 待解决
2016-01-25 18:08
302 查看
讲scanf函数 http://c.biancheng.net/cpp/html/34.html
---------------------------------------------------------------------------------------------------
前一天做一道题,九度oj的1108,栈。
题目不难,很简单,吭哧吭哧敲完之后运行,整个人都斯巴达了。不仅仅是结果,而且运行的过程,我看到简直崩溃!!!
今天,2016年1月25号,在参(piao)考(qie)了别人的代码之后,尝试着把
的地方全变成
然后就对了。
先把九度oj1108的题目贴上来:
题目描述:
堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。Push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。
输入:
对于每组测试数据,第一行是一个正整数 n,0<n<=10000(n=0 结束)。而后的 n 行,每行的第一个字符可能是'P’或者'O’或者'A’;如果是'P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O’,表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽略本次操作;如果是'A’,表示询问当前栈顶的值,如果当时栈为空,则输出'E'。堆栈开始为空。
输出:
对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的'A’操作,输出当时栈顶的值,每个占据一行,如果当时栈为空,则输出'E’。当每组测试数据完成后,输出一个空行。
样例输入:
样例输出:
为了直观看出scanf的诡异之处,测试用例为
1
A
0
现在,我把这两个代码贴上来,以及运行的结果。
首先是scanf:
诡异的地方有:
1,输入n,时,为啥,会输出i,输出i在for循环里
2,对于每次输入c,为啥i会自动增加?
cin版:
---------------------------------------------------------------------------------------------------
前一天做一道题,九度oj的1108,栈。
题目不难,很简单,吭哧吭哧敲完之后运行,整个人都斯巴达了。不仅仅是结果,而且运行的过程,我看到简直崩溃!!!
今天,2016年1月25号,在参(piao)考(qie)了别人的代码之后,尝试着把
scanf("%c",&c);
的地方全变成
cin>>c;
然后就对了。
先把九度oj1108的题目贴上来:
题目描述:
堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。Push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。
输入:
对于每组测试数据,第一行是一个正整数 n,0<n<=10000(n=0 结束)。而后的 n 行,每行的第一个字符可能是'P’或者'O’或者'A’;如果是'P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O’,表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽略本次操作;如果是'A’,表示询问当前栈顶的值,如果当时栈为空,则输出'E'。堆栈开始为空。
输出:
对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的'A’操作,输出当时栈顶的值,每个占据一行,如果当时栈为空,则输出'E’。当每组测试数据完成后,输出一个空行。
样例输入:
3 A P 5 A 4 P 3 P 6 O A 0
样例输出:
E 5 3
为了直观看出scanf的诡异之处,测试用例为
1
A
0
现在,我把这两个代码贴上来,以及运行的结果。
首先是scanf:
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stack> #define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0 using namespace std; int main(){ int n; int num; char c; int i=0; while(scanf("%d",&n), n){ stack<int> st; i=0; for(i=0; i<n ; i++) { scanf("%c",&c); printf("i=%d\n",i); if(c=='A'){ if(st.empty()) printf("E\n"); else{ printf("%d\n",st.top()); } } printf("i==%d\n",i); if(c=='P'){ // printf("wwwww\n"); scanf("%d",&num); // printf("%d\n",num); st.push(num); } printf("i===%d\n",i); if(c=='O') { if(st.empty()){ } else{ st.pop(); } } printf("i====%d\n",i); } // printf("i==%d\n",i); printf("----------------\n"); while(!st.empty()){ st.pop(); } } return 0; }
诡异的地方有:
1,输入n,时,为啥,会输出i,输出i在for循环里
2,对于每次输入c,为啥i会自动增加?
cin版:
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stack> #define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0 using namespace std; int main(){ int n; int num; // char c; int i=0; while(cin>>n, n){ stack<int> st; i=0; char c; // printf("yikaishi\n"); for(i=0; i<n ; i++) { cin>>c; // printf("i===%d\n",i); if(c=='A'){ if(st.empty()) printf("E\n"); else{ printf("%d\n",st.top()); } } // printf("i====%d\n",i); if(c=='P'){ // printf("wwwww\n"); scanf("%d",&num); // printf("%d\n",num); st.push(num); } // printf("i=====%d\n",i); if(c=='O') { if(st.empty()){ } else{ st.pop(); } } // printf("i========%d\n",i); } printf("\n"); // printf("i==%d\n",i); // printf("----------------\n"); while(!st.empty()){ st.pop(); } } return 0; } /* int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,i,j,a,k; while(cin>>n,n) { char c; stack<int> s; for(i=0; i<n; i++) { cin>>c; if(c=='P') { cin>>a; s.push(a); } else if(c=='O') { if(!s.empty()) s.pop(); } else { if(!s.empty()) cout<<s.top()<<endl; else cout<<'E'<<endl; } } cout<<endl; } return 0; } */ /************************************************************** Problem: 1108 User: zhouyudut Language: C++ Result: Accepted Time:110 ms Memory:1520 kb ****************************************************************/
相关文章推荐
- android:windowSoftInputMode
- 在电脑上调试手机上的webview
- Javascript 函数声明及使用
- 很全的AlartView和AlertViewController用法
- iOS中使用AutoLayout动态调整Cell高度
- 敏捷术语
- 自学Python十二 战斗吧Scrapy!
- 验证码生成
- Android网络解析
- 蔡康永--说话之道(一)
- Android之Handler详解
- 89. Excel Sheet Column Title
- iOS 字典转json字符串
- 【Unity入门】碰撞检测与触发检测
- Codeforces Round #340 (Div. 2) E XOR and Favorite Number
- hud 1467(动态规划。解题报告)
- Java基础学习8_封装
- 待学习资源记录
- 内容营销三大实用法则(内含干货)-同样可运用在EDM数据营销中
- The Java Programming Language4th读书笔记-第十五章 注解