一. 有 n 个正整数, 将它们连接成一排, 组成一个最大的多位整数. 例如 n = 3 时, 3 个整数 13,312,343
连成的最大整数为 34331213; 又如 n = 4 时, 4 个整数 7,13,4,246 连成的最大整数为 7424613. 要
求输入 n 个正整数后, 输出连成的最大整数.
题解: 先把整数转换为字符串, 然后比较比较 a+b 和 b+a. 如果 a+b> b+a, 就把 a 排在 b 前面, 反之把
b 排在 a 前面.
- // 设 s 为将读入的 n 个整数转换为字符串后的字符串数组.
- for(int i = 0; i <n - 1; i ++)
- {
- for(int j = i + 1; j <n; j++)
- {
- if(strcmp(strcat(s[i],s[j]),strcat(s[j],s[i])) < 0)
- SwapStr(s[i],s[j]);
- }
- for(int i = 0; i < n; i++)
- printf("%s",s[i]);
- }
二. 有 n 个顾客同时等待一项服务. 顾客 i 需要的平均符外时间为 ti , 1<=i<=n. 如何安排 n 个顾客的服务次序, 才
- #include
- #include
- #include
- using namespace std;
- double greedy(vectorx)
- {
- int n = x.size();
- sort(x.begin(),x.end());
- for(int i = 1; i <n; i++)
- x[i] += x[i-1];
- double t = 0;
- for(int i = 0; i < n; i++)
- t += x[i];
- t /= n;
- return t;
- }
- int main()
- {
- int n;
- int i; // 等待服务的顾客人数
- int a;
- int t; // 平均服务时间
- vector x;
- cout<<"Please input the num of the customer:"<<endl;
- cin>>n;
- cout<<"Please input the need service times of each customer:"<<endl;
- for(i = 1; i <= n; i++)
- {
- cout<<"No."<<i<<endl;
- cin>>a;
- x.push_back(a);
- }
- t = greedy(x);
- cout<<"the least average waiting time is:"<<t<<endl;
- return 0;
- }
- //point[]: n 个点的整数坐标, 传入前由小到大排好序
- //k: 闭区间的长度
- int Get_solve(int p[], int n, int k)
- {
- int sum = 1;
- for(int i = 1,t=p[0]; i <n; i++)
- {
- if(p[i] - t> k)
- {
- sum++;
- t = p[i];
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3302997.html