PAT-乙-1023 1023 组个最小数 (20 分))

代码

#include <iostream>
#include <sstream>

using namespace std;

const int MAX = 10;

string intToString(int t){
	stringstream ss;
	ss<<t;
	string tmp;
	ss>>tmp;
	return tmp;
}

int main(){
	
	int digit[MAX] = {0};
	for(int i=0; i<MAX; i++){
		cin>>digit[i]; 
	}
	
	string ans;
	
	//find first number
	for(int i=1; i<MAX; i++){
		if(digit[i]>0){
			ans = ans + intToString(i);
			digit[i]--;
			break;
		}
	}
	//find other number
	for(int i=0; i<MAX; i++){
		while(digit[i]>0){
			ans = ans + intToString(i);
			digit[i]--;
		}
	}
	
	cout<<ans<<endl;
	return 0;
}

注解

1、利用stringstream,整型转String

#include <sstream>
string intToString(int t){
    	stringstream ss;
    	ss<<t;
    	string tmp;
    	ss>>tmp;
    	return tmp;
    }

结果

PAT-乙-1023 1023 组个最小数 (20 分))

相关文章:

  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2021-05-26
  • 2021-07-25
  • 2022-01-09
猜你喜欢
  • 2021-08-11
  • 2021-10-16
  • 2021-05-01
  • 2021-09-27
  • 2021-07-17
  • 2021-11-11
相关资源
相似解决方案