• PAT 1006 换个格式输出整数 (15)(C++&JAVA&Python)


    1006 换个格式输出整数 (15)(15 分)

    让我们用字母B来表示“百”、字母S表示“十”,用“12...n”来表示个位数字n(&lt10),换个格式来输出任一个不超过3位的正整数。例如234应该被输出为BBSSS1234,因为它有2个“百”、3个“十”、以及个位的4。

    输入格式:每个测试输入包含1个测试用例,给出正整数n(&lt1000)。

    输出格式:每个测试用例的输出占一行,用规定的格式输出n。

    输入样例1:

    234
    

    输出样例1:

    BBSSS1234
    

    输入样例2:

    23
    

    输出样例2:

    SS123

    C++: 

    #include<iostream>
    #include<string>
    using namespace std;
    int main() {
    	int n;
    	cin >> n;
    	int a[3];
    	string result;
    	for (int i = 0; i < 3; i++) {
    		if (n) 
    			a[i] = n % 10;
    		else 
    			a[i] = 0;
    		n /= 10;
    	}
    	for (int i = 1; i <= a[0]; i++) 
    		result += to_string(i);
    	for (int i = 1; i <= a[1]; i++) 
    		result = 'S' + result;
    	for (int i = 1; i <= a[2]; i++) 
    		result = 'B' + result;
    	cout << result;
    	return 0;
    }

    JAVA: 

    import java.util.Scanner;
    public class Main{
    	public static void main(String [] args){
    		Scanner input=new Scanner(System.in);
    		String str=new String();
    		int temp;
    		int N=input.nextInt();
    		if(N!=0){
    			temp=N%10;
    			N/=10;
    			for(int i=1;i<=temp;i++)
    				str=str+i;
    		}
    		if(N!=0){
    			temp=N%10;
    			N/=10;
    			for(int i=0;i<temp;i++)
    				str='S'+str;
    		}
    		if(N!=0){
    			temp=N%10;
    			N/=10;
    			for(int i=0;i<temp;i++)
    				str='B'+str;
    		}
    		System.out.println(str);
    	}
    }

    Python:

    if __name__=="__main__":
        N=int(input())
        s=''
        if N:
            for i in range(1,N%10+1):
                s+=str(i)
        N=N//10    #除法取整
        if N:
            for i in range(0,N%10):
                s='S'+s
        N=N//10
        if N:
            for i in range(0,N%10):
                s='B'+s
        print(s)
    
    
  • 相关阅读:
    亨元模式
    模板方法模式
    组合模式
    命令模式
    Android AIDL使用介绍(2)自定义数据类型的传递*
    Android主线程(ActivityThread)源代码分析
    一个简单的死锁代码*
    ABA问题的本质及其解决办法*
    Java 多线程Atomic 与CAS 原理剖析*
    Java并发编程:volatile关键字解析*
  • 原文地址:https://www.cnblogs.com/F-itachi/p/9974401.html
Copyright © 2020-2023  润新知