

http://oj.ecustacm.cn/problem.php?id=1333
题目分析: 26进制转换问题
#include<cstdio>
int main(void)
{
int n;
int a[200];
int j=0;
scanf("%d",&n);
while(n)
{
a[j]=n%26;
j++;
n--;//注意的点
n=n/26;
}
j--;
for(j;j>=0;j--)//逆序输出
{
if(a[j]==0)
{
printf("Z");
}
else
{
printf("%c",a[j]+'A'-1);
}
}
return 0;
}
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string s;
int n;
int main(void)
{
while(cin>>n)
{
while(n)
{
int t=n%26;
char c='A'-1+t;
if(t==0) s+='Z';
else s+=c;
n--;
n=n/26;
}
reverse(s.begin(),s.end());
cout<<s<<endl;
s.clear();
}
return 0;
}
简化的方法:
你会发现数字的低位 是保存在数组的低位的。
那么当我们输出数字的时候得倒着输出。
这个就是一个栈么? 那么我们就用栈来代替数组。
那么将会减少我们的代码量。
#include<stack>
#include<cstdio>
using namespace std;
int main(void)
{
int n;
stack<int>st;
scanf("%d",&n);
while(n)
{
st.push(n%26);
n--;
n=n/26;
}
while(!st.empty())
{
if(st.top()==0)
printf("Z");
else
printf("%c",st.top()+'A'-1);
st.pop();
}
return 0;
}
本文讨论了如何使用栈简化26进制转换问题的代码,通过对比两种方法并引入栈数据结构来减少代码量。重点介绍了两种C/C++代码实现和栈在解决此问题中的应用.
578

被折叠的 条评论
为什么被折叠?



