zoukankan      html  css  js  c++  java
  • 【PAT】1005 Spell It Right

    题目:http://pat.zju.edu.cn/contests/pat-a-practise/1005

    分析:简单题。将输入的字符串一个个的转换成数字再相加,然后将相加的结果用英文打印出来就可以。输入输出的顺序需要用到栈的知识。

    题目描述:

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

    Input Specification:

    Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

    Output Specification:

    For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

    Sample Input:
    12345
    
    Sample Output:
    one five
    

    参考代码:

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stack>
    using namespace std;
    #define max 102
    char input[max];
    int main()
    {
    	cin>>input;
    	int len;
    	int sum=0,i,temp;
    	stack<string> result;
    	len = strlen(input);
    	for(i=0; i<len; i++)
    		sum += input[i] - '0';	
    
    	do
    	{
    		temp = sum%10;
    		if(temp == 0) result.push("zero");
    		else if(temp == 1) result.push("one");
    		else if(temp == 2) result.push("two");
    		else if(temp == 3) result.push("three");
    		else if(temp == 4) result.push("four");
    		else if(temp == 5) result.push("five");
    		else if(temp == 6) result.push("six");
    		else if(temp == 7) result.push("seven");
    		else if(temp == 8) result.push("eight");
    		else if(temp == 9) result.push("nine");
            sum /= 10;
    	}while(sum != 0);
    
    	cout<<result.top();
    	result.pop();
    	while(!result.empty())
    	{
    		cout<<" "<<result.top();
    		result.pop();
    	}
    	cout<<endl;
    	return 0;
    }


  • 相关阅读:
    Android自定义Dialog效果
    Attempt to invoke virtual method 'void android.app.ActionBar.setTitle的解决方法
    Android 震动 和 停止 代码
    任意手机虚拟按键增加方法
    [vijos]1066弱弱的战壕<线段树>
    [bzoj]1059矩阵游戏<二分图匹配*匈牙利算法>
    [bzoj]1053反质数<暴搜>
    [codevs]1250斐波那契数列<矩阵乘法&快速幂>
    [codevs2597]团伙<并查集>
    [noip模拟赛]虫洞holes<SPFA>
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3278428.html
Copyright © 2011-2022 走看看