码迷,mamicode.com
首页 > 其他好文 > 详细

1001. A+B Format (20)

时间:2018-03-21 00:05:46      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:分享   printf   ace   mat   div   pac   先进后出   保存   for   

 

这题就是对输出的格式化,注意这里是从后数每三个输出一个逗号,考虑到先进后出的关系这里用栈保存一下

 

#include <cstdio>
#include <string>
#include <stack>
using namespace std;
int main() {
  int a,b;
  scanf("%d %d",&a,&b);
  int result=a+b;
  string stres=to_string(result);
  stack<char> st;
  int size=stres.size();
  int cnt=1;
  for(int i=size-1;i>=0;i--) {
    if(cnt == 3) {
      if((i == 1 && stres[0]==-) || i == 0) {
    st.push(stres[i]);
      } else {
    st.push(stres[i]);
    st.push(,);
      }
      cnt=1;
    } else {
      st.push(stres[i]);
      cnt++;
    }
  }
  while(!st.empty()) {
    char temp=st.top();
    st.pop();
    printf("%c",temp);
  }
  return 0;
}

 

技术分享图片

 

1001. A+B Format (20)

标签:分享   printf   ace   mat   div   pac   先进后出   保存   for   

原文地址:https://www.cnblogs.com/tclan126/p/8613490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!