1006 部分A+B
题目描述
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。
例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。
现给定A、DA、B、DB,请编写程序计算PA + PB。
输入描述:
输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。
输出描述:
在一行中输出PA + PB的值。
输入例子:
3862767 6 13530293 3
输出例子:
399
就是找到每个数字串中相同的元素。
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main() {
string a, b;
char da, db;
cin >> a >> da >> b >> db;
long long pa = 0, pb = 0,k=0;
for (int i = 0; i < a.length(); i++) {
if (a[i] == da) {
//第一个数不需要乘10
if(k==0)
pa = pa + (da - '0');
else {
pa = pa * 10 + (da - '0');
}
k++;
}
}
k = 0;
for (int i = 0; i < b.length(); i++) {
if (b[i] == db) {
if(k==0)
pb = pb + (db - '0');
else {
pb = pb * 10 + (db - '0');
}
k++;
}
}
printf("%lld\n", pa + pb);
}
1007.A除以B
题目描述
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。
输入描述:
输入在1行中依次给出A和B,中间以1空格分隔。
输出描述:
在1行中依次输出Q和R,中间以1空格分隔。
输入例子:
123456789050987654321 7
输出例子:
17636684150141093474 3
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main() {
string a;
int b;
cin >> a >> b;
int yu = a[0] - '0';
for (int i = 1; i < a.size(); i++) {
int tem = yu * 10 + (a[i] - '0');
cout << tem / b;
yu = tem % b;
}
cout << " " << yu<<endl;
return 0;
}
1009.数字黑洞
题目描述
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排
序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字。
一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意4位正整数,请编写程序演示到达黑洞的过程。
输入描述:
输入给出一个[1000, 10000)区间内的正整数N。
输出描述:
如果N的4位数字全相等,则在一行内输出“N - N = 0000”;
否则将计算的每一步在一行内输出,直到6174作为差出现,
输出格式见样例,每行中间没有空行。注意每个数字按4位数格式输出。
输入例子:
6767
输出例子:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
bool cmp(char a, char b) {
return a > b;
}
int main() {
string s;
cin >> s;
if (s.length() == 1)
s = "000" + s;
else if (s.length() == 2)
s = "00" + s;
else if (s.length() == 3)
s = "0" + s;
sort(s.begin(), s.end(),cmp);
string temp = s;
sort(s.begin(), s.end());
for (int i = 0; ; i++) {
int a = (temp[0] - '0') * 1000 + (temp[1] - '0') * 100 + (temp[2] - '0') * 10 + (temp[3] - '0');
int b = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
int c = a - b;
if (c == 0) {
printf("%04d - %04d = %04d\n", a, b, c);
break;
}
printf("%04d - %04d = %04d\n", a, b, c);
if (c == 6174)
break;
else {
string t = to_string(c);
sort(t.begin(), t.end(),cmp);
temp = t;
sort(t.begin(), t.end());
s = t;
}
}
}
1010.月饼
题目描述
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。
现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获
得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月
饼,其库存量分别为18、15、10万吨,总售价分别为75、72、45亿元。如果市场
的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月
饼、以及5万吨第3种月饼,获得72 + 45/2 = 94.5(亿元)。
输入描述:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示
月饼的种类数、以及不超过500(以万吨为单位)的正整数D表示市场最大需求量。
随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个
正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
输出描述:
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。
输入例子:
3 20
18 15 10
75 72 45
输出例子:
94.50
贪心算法,每次选单价最高的那一个。
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
struct cake {
double kc;
double price;
};
//按单价降序排序
bool cmp(cake a, cake b) {
return a.price / a.kc > b.price / b.kc;
}
int main() {
int n;
double need;
cake c[1001];
scanf("%d%lf", &n, &need);
for (int i = 0; i < n; i++) {
scanf("%lf",&c[i].kc);
}
for (int i = 0; i < n; i++) {
scanf("%lf", &c[i].price);
}
sort(c, c + n, cmp);
double money=0;
for (int i = 0; i < n&&need>0; i++) {
if (c[i].kc > need) {//该类库存大于总需求
money += (c[i].price / c[i].kc)*need;
need -= need;
}
else {
money += c[i].price;
need = need - c[i].kc;
}
}
printf("%.2lf\n", money);
}
1011.个位数统计
题目描述
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9,
i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例
如:给定N = 100311,则有2个0,3个1,和1个3。
输入描述:
每个输入包含1个测试用例,即一个不超过1000位的正整数N。
输出描述:
对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出
现的次数M。要求按D的升序输出。
输入例子:
100311
输出例子:
0:2
1:3
3:1
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
string s;
cin >> s;
int num[10] = { 0 };
for (int i = 0; i < s.length(); i++) {
int temp = s[i] - '0';
num[temp]++;
}
for (int i = 0; i < 10; i++) {
if (num[i] != 0) {
printf("%d:%d\n", i, num[i]);
}
}
}
1012.D进制A+B
题目描述
输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数。
输入描述:
输入在一行中依次给出3个整数A、B和D。
输出描述:
输出A+B的D进制数。
输入例子:
123 456 8
输出例子:
1103
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
long long a,b;
int d;
scanf("%lld%lld%d", &a, &b, &d);
long long sum = a + b;
string s="";
//n进制,求余数
while (sum) {
long long y = sum % d;
s = to_string(y) + s;
sum = sum / d;
}
printf("%s\n", s.c_str());
}
1013.组个最小数
题目描述
给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标
是使得最后得到的数尽可能小(注意0不能做首位)。例如:给定两个0,两个1,
三个5,一个8,我们得到的最小的数就是10015558。
现给定数字,请编写程序输出能够组成的最小的数。
输入描述:
每个输入包含1个测试用例。每个测试用例在一行中给出10个非负整数,顺序表示
我们拥有数字0、数字1、……数字9的个数。整数间用一个空格分隔。10个数字的总
个数不超过50,且至少拥有1个非0的数字。
输出描述:
在一行中输出能够组成的最小的数。
输入例子:
2 2 0 0 0 3 0 0 1 0
输出例子:
10015558
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
int a[10] = { 0 };
for (int i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
string s = "";
for (int i = 0; i < 10; i++) {
for (int j = 0; j < a[i]; j++) {
s = s+to_string(i);
}
}
if (a[0] != 0) {
int t = s.find_last_of("0");
char temp = s[t + 1];
s[t + 1] = s[0];
s[0] = temp;
}
printf("%s\n", s.c_str());
}