1001
using namespace std中存在std::count,所以再定义count会报错:count不明确
#include <iostream>
using namespace std;
int n, cnt = 0;
void cal(int num) {
if (num == 1)
{
return;
}
if (num % 2 == 0)
{
cnt++;
cal(num / 2);
return;
}
cnt++;
cal((3 * num + 1) / 2);
}
int main()
{
cin >> n;
cal(n);
cout << cnt << endl;
return 0;
}
1002
char转int方法:
可以定义一个宏#define charToNum(x) (x-'0')
#include <iostream>
#include <string>
#include <vector>
#define charToNum(x) (x-'0')
using namespace std;
string n;
vector<string> store;
void intToPY(int x) {
switch (x) {
case 1:
store.push_back("yi");
break;
case 2:
store.push_back("er");
break;
case 3:
store.push_back("san");
break;
case 4:
store.push_back("si");
break;
case 5:
store.push_back("wu");
break;
case 6:
store.push_back("liu");
break;
case 7:
store.push_back("qi");
break;
case 8:
store.push_back("ba");
break;
case 9:
store.push_back("jiu");
break;
case 0:
store.push_back("ling");
break;
}
}
void print(int sum) {
if (sum == 0)
return;
int temp = sum % 10;
print(sum/10);
intToPY(temp);
}
void solve() {
if (n == "0")
{
cout << "ling" << endl;
return;
}
int sum = 0;
for (int i = 0; i < n.size(); ++i) {
sum += charToNum(n[i]);
}
print(sum);
for (int i = 0; i < store.size() - 1; ++i) {
cout << store[i] << " ";
}
cout << store[store.size() - 1] << endl;
}
int main()
{
cin >> n;
solve();
return 0;
}
然而我的算法过于复杂,其实可以这么写:
输入无需储存起来,可以使用(c=getchar())!=\n
一个char一个char的扫描
由于0~9与数组下标0~9一一对应,可以定义一个字符串数组用于储存0~9的拼音。
由于最大和为9*100<1000,所以只需取余三次即可。
#include <stdio.h>
#include <string.h>
#define charToNum(x) (x-'0')
int main()
{
char c;
int sum = 0;
while ((c = getchar()) != '\n') sum += charToNum(c);
const char *intToPY[] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
if (sum / 100)
printf("%s ", intToPY[sum / 100]);
if (sum / 10)
printf("%s ", intToPY[sum / 10 % 10]);
printf("%s\n", intToPY[sum % 10]);
return 0;
}
1003
给定一个字符串,要求能化简成xPATx形式(x为只含A的字符串或者为空字符串)
如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
设P前A的个数为x
,P与T中间A的个数为y
,T之后A的个数为z
,
所以只需满足递推式,并且
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
void solve(string s) {
int x = 0, y = 0, z = 0;
int pCount = 0, tCount = 0;
for (int i = 0; i < s.size(); ++i)
{
if ((s[i] != 'P'&&s[i] != 'A'&&s[i] != 'T')|| (s[i] == 'P' && (pCount>0 || tCount>0))||(s[i]=='T'&&tCount>0))
{
cout << "NO" << endl;
return;
}
if (s[i] == 'P') pCount++;
if (s[i] == 'T') tCount++;
}
if (pCount != 1 || tCount != 1) {
cout << "NO" << endl;
return;
}
int k = 0;
while (s[k] != 'P'&&k<s.size()-1) {
x++;
k++;
}k++;
while (s[k] != 'T'&&k < s.size()) {
y++;
k++;
}k++;
while (k < s.size()) {
z++;
k++;
}
if (z - (y - 1)*x == x && y != 0)
{
cout << "YES" << endl;
return;
}
else {
cout << "NO" << endl;
return;
}
}
int main()
{
cin >> n;
vector<string> input(n);
for (int i = 0; i < n; ++i) {
cin >> input[i];
}
for (int i = 0; i < n; ++i) {
solve(input[i]);
}
return 0;
}
1004
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
int low = 100, high = 0;
int point;
string name, number;
string highP, lowP;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> name >> number >> point;
if (point >= high) {
highP = name + ' ' + number;
high = point;
}
if (point <= low) {
lowP = name + ' ' + number;
low = point;
}
}
cout << highP << endl << lowP << endl;
return 0;
}
1005
第一题的复杂版
思路是每一次运算都与输入的整数比较一次,如果有相同的,说明它是这条链上的一个节点,
将其设为1表示删除
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> input;
bool compare(int a, int b) {
return a > b;
}
void del(int x) {
if (x == 1)
return;
if (x % 2) {
int k = (3 * x + 1) / 2;
for (int i = 0; i < input.size(); ++i) {
if (k == input[i])
{
input[i] = 1;
break;
}
}
del(k);
}
else {
int k = x / 2;
for (int i = 0; i < input.size(); ++i) {
if (k == input[i])
{
input[i] = 1;
break;
}
}
del(k);
}
}
void solve() {
for (int i = 0; i < input.size(); ++i) {
del(input[i]);
}
sort(input.begin(), input.end(), compare);
int i;
for (i= 0; i < input.size() && input[i] != 1; ++i) {}
for (int k = 0; k < i - 1; k++) {
cout << input[k] << " ";
}
cout << input[i - 1] << endl;
}
int main()
{
int n;
cin >> n;
input.resize(n);
int temp;
for (int i = 0; i < n; ++i) {
cin >> input[i];
}
solve();
return 0;
}
1006
#include <iostream>
using namespace std;
int num;
void solve() {
if (num / 100) {
for (int i = 0; i < (num / 100); ++i) {
cout << 'B';
}
}
if (num % 100 / 10) {
for (int i = 0; i < (num % 100 / 10); ++i) {
cout << 'S';
}
}
for (int i = 1; i <= (num % 10); ++i) {
cout << i;
}
cout << endl;
}
int main()
{
cin >> num;
solve();
return 0;
}
1007
memset的头文件为<string.h>,记得引入
思路是首先用埃氏筛法得到n以内所有的素数,然后用相邻项相减,记录差为2的素数对的个数。
#include <iostream>
#include <vector>
#include <string.h>
#define MAX_N 100010
using namespace std;
int n;
bool Primes[MAX_N];
vector<int> save;
int cnt = 0;
bool isPrime(int x) {
for (int i = 2; i*i <= x; ++i) {
if (x%i == 0)
return false;
}
return true;
}
void solve() {
memset(Primes, 0, sizeof(Primes));
Primes[1] = Primes[0] = true;
for (int i = 2; i <= n; ++i) {
if (isPrime(i)) {
for (int k = i; k <= n; k += i) {
Primes[i] = true;
}
}
}
for (int i = 2; i <= n; ++i) {
if (Primes[i])
save.push_back(i);
}
for (int i = 0; i < save.size()-1; ++i) {
if (save[i + 1] - save[i] == 2)++cnt;
}
cout << cnt << endl;
}
int main()
{
cin >> n;
solve();
return 0;
}
1008
本题的关键是没有规定m一定小于n,所以要记得将m取n的模
#include <iostream>
#define MAX_N 110
using namespace std;
int n, m;
int Arr[MAX_N];
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void solve() {
for (int i = n - m; i < n; ++i) {
for (int j = i; j > i - n + m; --j) {
swap(Arr[j], Arr[j - 1]);
}
}
for (int i = 0; i < n - 1; i++)
cout << Arr[i] << " ";
cout << Arr[n-1] << endl;
}
int main()
{
cin >> n >> m;
m %= n;
for (int i = 0; i < n; ++i) {
cin >> Arr[i];
}
solve();
return 0;
}
1009
本题的关键是要将每个单词保存下来,可以用c=getchar()
检测每次的输入,
当输入字母时,将其与上一次输入相加。当输入空格时,先将字符串保存,再将字符串清空,并停止此次循环。
由于结尾没有空格,所以最后一个单词单独保存。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
char c;
vector<string> save;
string temp="";
while ((c = getchar()) != '\n') {
if (c == ' ')
{
save.push_back(temp);
temp = "";
continue;
}
temp += c;
}
save.push_back(temp);
for (int i = save.size() - 1; i > 0; --i) {
cout << save[i] << " ";
}
cout << save[0] << endl;
return 0;
}
1010
本题的难点在于如何判断输入结束
while (scanf("%d %d", &coef, &index) != EOF)
可以用于扫描一行的内容,EOF代表输入的末尾。
#include <stdio.h>
int main()
{
int coef, index, cnt = 0, flag = 1;
while (scanf("%d %d", &coef, &index) != EOF) {
if (index > 0) {
if (flag) {
printf("%d %d", coef*index, index - 1);
cnt++;
flag = 0;
}
else {
printf(" %d %d", coef*index, index - 1);
cnt++;
}
}
}
if (cnt == 0) {
printf("0 0");
}
return 0;
}
1011
由于输入A,B,C的范围为~,int占2个字节16位,long占4个字节32位,所以不能用int,要使用long。
#include <iostream>
#define MAX_N 20
using namespace std;
int n;
bool save[MAX_N];
int main()
{
cin >> n;
long a, b, c;
for (int i = 1; i <= n; ++i) {
cin >> a >> b >> c;
a + b > c ? save[i] = true : save[i] = false;
}
for (int i = 1; i <= n; ++i) {
cout << "Case #" << i << ": ";
save[i] ? cout << "true" : cout << "false";
cout << endl;
}
return 0;
}
1012
对于printf("%x,yf",a)
,若a的整数部分不足x位,则在左边补零,若超过x位,则正常输出,若a的小数部分不足y位,则在右边补零,若超过y位,则四舍五入保留y位小数。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#define MAX_N 1010
using namespace std;
int N;
int save[MAX_N];
int output[5];
void solve() {
bool flag = true;
int count = 0;
bool visit[5] = { false };
memset(output, 0, sizeof(output));
for (int i = 0; i < N; ++i) {
if (save[i] % 5 == 0 && save[i] % 2 == 0) {
output[0] += save[i];
visit[0] = true;
}
else if (save[i] % 5 == 1) {
flag ? output[1] += save[i] : output[1] -= save[i];
flag = !flag;
visit[1] = true;
}
else if (save[i] % 5 == 2) {
output[2] += 1;
visit[2] = true;
}
else if (save[i] % 5 == 3) {
output[3] += save[i];
++count;
visit[3] = true;
}
else if (save[i] % 5 == 4) {
output[4] = max(output[4], save[i]);
visit[4] = true;
}
else {
visit[0] == true;
}
}
for (int i = 0; i < 4; ++i) {
if (visit[i] == false) {
cout << "N" << " ";
}
else if (i != 3) {
cout << output[i] << " ";
}
else if (i == 3) {
printf("%.1f ", (float)output[i] / count);
}
}
visit[4] ? cout << output[4] << endl : cout << "N" << endl;
}
int main()
{
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> save[i];
}
solve();
return 0;
}
1013
2是最小的素数,1不是素数
#include <iostream>
#define MAX_S 10000010
using namespace std;
int lb, rb;
int save[MAX_S];
bool is_prime(int x) {
for (int i = 2; i*i <= x; ++i) {
if (x%i == 0)
return false;
}
return true;
}
void solve() {
int count = 1;
int index = 3;
save[1] = 2;
while (count < rb) {
if (is_prime(index)) {
save[++count] = index;
}
index++;
}
while (rb - lb >= 9) {
int i;
for (i = lb; i < lb + 9; ++i) {
cout << save[i] << " ";
}
cout << save[i] << endl;
lb += 10;
}
if (lb <= rb) {
for (int i = lb; i < rb; ++i) {
cout << save[i] << " ";
}
cout << save[rb] << endl;
}
}
int main()
{
cin >> lb >> rb;
solve();
return 0;
}
1014
本题的难点在于需要熟悉ASCII
#include <iostream>
#include <algorithm>
#include <string>
#include<stdio.h>
#include <vector>
#define charToNum(x) (x-'A')
using namespace std;
vector<string> input(4);
int main()
{
int flag = 0;
const char *week[] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
for (int i = 0; i < 4; ++i) {
cin >> input[i];
}
int index1= min(input[0].size(), input[1].size());
for (int i = 0; i < index1; ++i) {
if (input[0][i] == input[1][i]) {
if (flag == 0 && input[0][i] >= 'A'&&input[0][i] <= 'G') {
cout << week[charToNum(input[0][i])] << " ";
flag++;
}
else if (flag == 1 && input[0][i] == input[1][i]) {
if (input[0][i] >= '0' && input[0][i] <= '9') {
printf("%02d:", input[0][i]-'0');
flag++;
}
else if (input[0][i] >= 'A' && input[0][i] <= 'N') {
cout << charToNum(input[0][i])+10 << ":";
flag++;
}
}
}
}
int index2 = min(input[2].size(), input[3].size());
for (int i = 0; i < index2; ++i) {
if (input[2][i] == input[3][i]&&((input[2][i]>='a'&&input[2][i]<='z')|| (input[2][i] >= 'A'&&input[2][i] <= 'Z'))) {
printf("%02d\n", i);
break;
}
}
return 0;
}
1015
此题思路还是很简单的,但是由于数据量较大,用cin、cout会超时。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, L, H;
struct stu {
int id;
int de;
int cai;
};
bool compare(stu a, stu b) {
if (a.de + a.cai == b.de + b.cai) {
if (a.de == b.de)
return a.id < b.id;
return a.de > b.de;
}
return a.de + a.cai > b.de + b.cai;
}
int main()
{
int count = 0;
scanf("%d %d %d", &N, &L, &H);
vector<stu> people1;
vector<stu> people2;
vector<stu> people3;
vector<stu> people4;
stu pig;
for (int i = 0; i < N; ++i) {
scanf("%d %d %d", &pig.id, &pig.de, &pig.cai);
if (pig.de >= L && pig.cai >= L) {
if (pig.de >= H && pig.cai >= H) {
people1.push_back(pig);
}
else if (pig.de >= H && pig.cai < H) {
people2.push_back(pig);
}
else if (pig.de < H&&pig.cai < H&&pig.de>=pig.cai) {
people3.push_back(pig);
}
else {
people4.push_back(pig);
}
count++;
}
}
sort(people1.begin(), people1.end(), compare);
sort(people2.begin(), people2.end(), compare);
sort(people3.begin(), people3.end(), compare);
sort(people4.begin(), people4.end(), compare);
cout << count << endl;
for (int i = 0; i < people1.size(); ++i) {
printf("%d %d %d\n", people1[i].id, people1[i].de, people1[i].cai);
}
for (int i = 0; i < people2.size(); ++i) {
printf("%d %d %d\n", people2[i].id, people2[i].de, people2[i].cai);
}
for (int i = 0; i < people3.size(); ++i) {
printf("%d %d %d\n", people3[i].id, people3[i].de, people3[i].cai);
}
for (int i = 0; i < people4.size(); ++i) {
printf("%d %d %d\n", people4[i].id, people4[i].de, people4[i].cai);
}
return 0;
}
1016
计算次方的函数为pow()
#include <iostream>
#include <math.h>
#include<string>
#define charToNum(x) (x-'0')
using namespace std;
string A, B;
char Da, Db;
int main()
{
cin >> A >> Da >> B >> Db;
int num1 = 0, num2 = 0;
for (int i = 0; i < A.size(); ++i) {
if (A[i] == Da)++num1;
}
for (int i = 0; i < B.size(); ++i) {
if (B[i] == Db)++num2;
}
int sum = 0;
for (int i = 0; i < num1; ++i) {
sum += (pow(10, i)*charToNum(Da));
}
for (int i = 0; i < num2; ++i) {
sum += (pow(10, i)*charToNum(Db));
}
cout << sum << endl;
return 0;
}
1017
由于被除数最高一千位,所以肯定需要使用高精度计算。
#include <iostream>
#include <string.h>
#include <string>
#define charToNum(x) (x-'0')
using namespace std;
int main()
{
string a;
int b, re = 0;
int save[1010];
int result[1010];
memset(save, 0, sizeof(save));
memset(result, 0, sizeof(result));
cin >> a >> b;
save[0] = a.size();
for (int i = 0; i < a.size(); ++i) {
save[i] = charToNum(a[i]);
}
for (int i = 0; i < a.size(); ++i) {
result[i] = (re * 10 + save[i]) / b;
re = (re * 10 + save[i]) % b;
}
int lenc = 0;
while (result[lenc] == 0 && lenc < a.size()-1)lenc++;
for (int i = lenc; i < a.size(); ++i) {
cout << result[i];
}
cout <<" "<< re << endl;
return 0;
}
1018
#include <iostream>
using namespace std;
int N;
struct pig {
int scissors = 0;
int stone = 0;
int cloth = 0;
};
int sum(pig a) {
return a.cloth + a.scissors + a.stone;
}
char big(pig a) {
return a.scissors > a.stone ? (a.scissors > a.cloth ? 'J' : 'B') : (a.stone > a.cloth ? 'C' : 'B');
}
int main()
{
pig p1, p2;
int ping = 0;
char temp1, temp2;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> temp1 >> temp2;
if (temp1 == temp2)ping++;
else if (temp1 == 'C'&&temp2 == 'J')p1.stone++;
else if (temp1 == 'J'&&temp2 == 'C')p2.stone++;
else if (temp1 == 'B'&&temp2 == 'C')p1.cloth++;
else if (temp1 == 'C'&&temp2 == 'B')p2.cloth++;
else if (temp1 == 'J'&&temp2 == 'B')p1.scissors++;
else if (temp1 == 'B'&&temp2 == 'J')p2.scissors++;
}
cout << sum(p1) << " " << ping << " " << sum(p2) << endl;
cout << sum(p2) << " " << ping << " " << sum(p1) << endl;
cout << big(p1) << " " << big(p2) << endl;
return 0;
}
1019
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#define charToNum(x) (x-'0')
#define numToChar(x) (x+'0')
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = a.size(); i < 4; i++) {
a += '0';
}
if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) {
cout << a << " - " << a << " = " << "0000" << endl;
}
else {
do{
sort(a.begin(), a.end());
int a1 = 0, b = 0;
for (int i = 0; i <= 3; ++i) {
a1 += charToNum(a[i])*pow(10, i);
}
for (int i = 0; i <= 3; ++i) {
b += charToNum(a[i])*pow(10, 3 - i);
}
int c = a1 - b;
printf("%04d - %04d = %04d\n", a1, b, c);
a = "";
a += numToChar(c / 1000);
a += numToChar(c % 1000 / 100);
a += numToChar(c % 100 / 10);
a += numToChar(c % 10);
}while (a != "6174");
}
return 0;
}
1020
要看清题目描述哦....重量和价格不一定是整数
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
float N, D;
struct pie {
float weight;
float money;
float ave;
};
bool compare(pie a, pie b) {
return a.ave > b.ave;
}
int main()
{
float sum = 0;
cin >> N >> D;
vector<pie> save(N);
for (int i = 0; i < N; ++i) {
cin >> save[i].weight;
}
for (int i = 0; i < N; ++i) {
cin >> save[i].money;
}
for (int i = 0; i < N; ++i) {
save[i].ave = save[i].money / save[i].weight;
}
sort(save.begin(), save.end(), compare);
for (int i = 0; i < N; ++i) {
if (D >= save[i].weight) {
D -= save[i].weight;
sum += save[i].money;
}
else {
sum += save[i].ave*D;
break;
}
}
printf("%.2f\n", sum);
return 0;
}
1021
#include <string>
#include <string.h>
#include <iostream>
#define charToNum(x) (x-'0')
using namespace std;
int a[10];
int main()
{
memset(a, 0, sizeof(a));
char c;
while ((c = getchar()) != '\n') {
a[charToNum(c)]++;
}
for (int i = 0; i < 10; ++i) {
if (a[i] != 0)
printf("%d:%d\n", i, a[i]);
}
return 0;
}
1022
将十进制整数转化为任意进制的方法:递归地先除再取余
取余的值作为该位的值,除后的值作为递归的参数。
若取余后的值大于十,即转化的进制大于十。则需要加一个if语句做判断:
if(n>=10){printf("%d",n+'A'-10);}
#include <iostream>
using namespace std;
long A, B, C;
int D;
void solve(int x) {
long n;
if (x) {
solve(x / D);
n = x % D;
printf("%d", n);
}
}
int main()
{
cin >> A >> B >> D;
C = A + B;
if (C == 0)
printf("0");
else
solve(C);
return 0;
}
若是要将x进制的数转化为y进制要怎么做呢?
首先将x进制转化为10进制,然后再用上面的代码将10进制转化为y进制。
x进制转化为十进制的函数:
int TenNum(char a[], int B)
{
int len, i, num;
int sum = 0;
len = strlen(a); //求得字符串长度
for (i = 0; i < len; i++)
{
if (a[i] >= '0' && a[i] <= '9')
num = a[i] - '0';
else if (a[i] >= 'A' && a[i] <= 'F')
num = a[i] - 'A' + 10;
sum = sum * B + num;
}
return sum;
}
1023
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char c;
int a[10];
memset(a, 0, sizeof(a));
for (int i = 0; i < 10; ++i) {
cin >> a[i];
}
for (int i = 1; i <= 9; ++i) {
if (a[i] != 0) {
cout << i;
a[i]--;
break;
}
}
for (int i = 0; i <= 9; ++i) {
while (a[i] > 0) {
cout << i;
a[i]--;
}
}
return 0;
}
1024
....摸着测试用例过河系列
#include <iostream>
#include <string.h>
#include <string>
#define charToNum(x) (x-'0')
using namespace std;
string s;
int main()
{
string save;
cin >> s;
int flag = 0;
if (s[0] == '-')cout << '-';
int i = 1;
for (; s[i] != 'E'; ++i) {
if (i != 2) {
save += s[i];
}
}
int sum = 0;
if (s[++i] == '-')flag = 1;
do {
i++;
} while (s[i] == '0');
for (; i < s.size() ; ++i) {
sum = sum * 10 + charToNum(s[i]);
}
if (flag) {
cout << "0.";
for (int j = 1; j < sum; ++j) {
cout << "0";
}
cout << save << endl;
}
else {
if (sum >= save.size()-1) {
cout << save;
if(sum!= save.size() - 1)
for (int i = 0; i <= sum - save.size(); ++i)cout << "0";
}
else {
int k = 0;
for (; k <= sum; ++k) {
cout << save[k];
}
cout << ".";
for (; k < save.size(); ++k) {
cout << save[k];
}
}
}
return 0;
}
1025
测试点5超时...我放弃了
#include <stdio.h>
#include <algorithm>
#define INF 100010
using namespace std;
struct ListNode {
int data;
int add;
int nextAdd;
int order=INF;
}List[100010];
int cnt = 0;//可能有孤立节点
int head, N, K;
bool compare(ListNode a, ListNode b) {
return a.order < b.order;
}
int main()
{
scanf("%d%d%d", &head, &N, &K);
if (head == -1) {
printf("-1\n");
return 0;
}
for (int i = 0; i < N; ++i) {
scanf("%d%d%d", &List[i].add, &List[i].data, &List[i].nextAdd);
}
for (int i = head; i != -1;) {
for (int j = 0; j < N; ++j) {
if (i == List[j].add) {
List[j].order = cnt++;
i = List[j].nextAdd;
break;
}
}
}
sort(List, List+N, compare);
for (int i = K; i <= cnt; i += K) {
for (int j = 0; j < K / 2; ++j) {
ListNode* temp = &List[i - K + j];
List[i - K + j] = List[i - j - 1];
List[i - j - 1] = *temp;
}
}
for (int i = 0; i < cnt-1; ++i) {
printf("%05d %d %05d\n", List[i].add, List[i].data, List[i + 1].add);
}
printf("%05d %d %d\n", List[cnt-1].add, List[cnt-1].data, -1);
return 0;
}
1026
C语言实现浮点数四舍五入的方法:利用(int)
截断的特点,将目标值+0.5即可
#include <iostream>
#include <stdio.h>
#define CLK_TCK 100
using namespace std;
int C1, C2;
void solve() {
float t = (float)(C2 - C1)/100;
int h = t / 3600;
int m = (int)t % 3600 / 60;
int s = (int)(t + 0.5) % 60;
printf("%02d:%02d:%02d\n", h, m, s);
}
int main()
{
cin >> C1 >> C2;
solve();
return 0;
}
1027
等差数列求通项与总和
#include <iostream>
#include <stdio.h>
using namespace std;
int N;
char display;
int n = 0;//行数
int rest;
void cal() {
//通项:2*n*(n+2)+1
while (2 * n*(n + 2) + 1 <= N)++n;
--n;
rest = N - 2 * n*(n + 2) - 1;
}
void show() {
for (int i = n; i >= 0; --i) {
for (int j = 0; j < n - i; ++j) {
printf(" ");
}
for (int k = 0; k < 2 * i + 1; ++k) {
printf("%c", display);
}
printf("\n");
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < n - i; ++j) {
printf(" ");
}
for (int k = 0; k < 2 * i + 1; ++k) {
printf("%c", display);
}
printf("\n");
}
printf("%d\n", rest);
}
int main()
{
cin >> N >> display;
cal();
show();
return 0;
}
1028
关于STL里的sort:如果需要自己写比较函数,容易出现错误提示invalid,这是什么原因呢?
根据文档显示,这个比较函数必须具有以下性质:
(1)两个关键字不能同时“小于等于”对方;如果k1“小于等于”k2,那么k2决不能“小于等于”k1。
(2)如果k1“小于等于”k2,且k2“小于等于”k3,那么k1必须“小于等于”k3.
(3)如果存在两个关键字,任何一个都不“小于等于”另一个,那我们称这两个关键字是“等价的”。如果k1“等价于”k2,且k2“等价于”k3,那么k1必须“等价于”k3。
所以,在重写的比较函数中,<=,>=号都不能存在,因为不满足性质一,
对于条件3,显而易见任何对于数k1,k2而言,k1<=k2和k2<=k1都是成立的,显然条件3也不符合。
对于C++的严格比较,当两数相等的时候,一定要返回false
,不能返回true
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct pig {
string name;
int year;
int month;
int day;
}people[100010];
int N;
int cnt = 0;
bool isTrue(int year, int month, int day) {
if (year > 1814 && year < 2014)
return true;
else {
if (year == 1814)
{
if (month > 9)
return true;
else if(month==9) {
if (day >= 6)
return true;
}
}
if (year == 2014) {
if (month < 9)
return true;
else if (month == 9) {
if (day <= 6)
return true;
}
}
}
return false;
}
bool compare(pig a, pig b) {
if (a.year > b.year)
return true;
else if (a.year == b.year) {
if (a.month > b.month)
return true;
else if (a.month == b.month) {
if (a.day > b.day)
return true;
}
}
return false;
}
int main()
{
cin >> N;
pig temp;
for (int i = 0; i < N; ++i) {
cin >> temp.name;
scanf("%d/%d/%d", &temp.year, &temp.month, &temp.day);
if (isTrue(temp.year, temp.month, temp.day)){
people[cnt++] = temp;
}
}
sort(people, people + cnt, compare);
printf("%d",cnt);
if (cnt != 0)
cout << " " << people[0].name << " " << people[cnt - 1].name << endl;
return 0;
}
1029
这种暴力遍历的方法时间复杂度为,但是运用哈希映射可以将时间复杂度降至
暴力遍历法:
#include <stdio.h>
#include <iostream>
#include <string>
#define toCaptital(x) (x+'A'-'a')
using namespace std;
char trans(char x) {
if (x >= 'a'&&x <= 'z')
return toCaptital(x);
return x;
}
int main()
{
string good, bad, save;
cin >> good >> bad;
int i = 0, j = 0;
while (i < good.size() && j < bad.size()) {
if (good[i] == bad[j]) {
++i; ++j;
}
else {
save += trans(good[i++]);
}
}
while (i < good.size()) {
save += trans(good[i++]);
}
for (int i = 0; i < save.size() - 1; ++i) {
for (int j = i + 1; j < save.size(); ++j) {
if (save[i] == save[j]) {
save[j] = ' ';
}
}
if (save[i] != ' ')printf("%c", save[i]);
}
return 0;
}
哈希映射法:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#define toCaptital(x) (x+'A'-'a')
using namespace std;
char trans(char x) {
if (x >= 'a'&&x <= 'z')
return toCaptital(x);
return x;
}
int main()
{
string good, bad;
char str[100];
int ascii[128];
memset(ascii, 0, sizeof(ascii));
memset(str, 0, sizeof(str));
cin >> good >> bad;
int cnt = 0;
int len = 0;
for (int i = 0; i < good.size(); ++i) {
if (good[i] != bad[cnt]) {
if (ascii[trans(good[i])] == 0){
ascii[trans(good[i])]++;
str[len++] = trans(good[i]);
}
}
else {
cnt++;
}
}
printf("%s\n", str);
return 0;
}
1030
这道题是最大子序和的变种,这种连续字序问题的解决方法都为双指针法,开始时两个指针指向数组第一个值,在满足题目条件的情况下last指针不断指向后一个值,直到不满足条件,此时first指针再不断指向后一个值直到满足条件。这样不断循环,可以得到满足的最大字序数目或最大子序和。
在最大连续子序和问题中,应该存~的和,或者使用动态规划求解。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 100010
using namespace std;
int N, p;
double num[MAX_N];
int main()
{
scanf("%d %d", &N, &p);
for (int i = 0; i < N; ++i) {
scanf("%lf", &num[i]);
}
sort(num, num + N);
int sum = 1;
for (int first = 0, last = 0; last < N; first++) {
while (last<N&&num[first] * p>=num[last]) {
last++;
}
sum = max(sum, last - first);
}
printf("%d\n", sum);
return 0;
}
1031
灵活使用hash映射让解题更方便哦!
#include<iostream>
#include <vector>
#include <string>
#define charToNum(x) (x-'0')
using namespace std;
vector<string> save(110);
char hashT[] = { '1','0' ,'X' ,'9' ,'8' ,'7' ,'6' ,'5' ,'4' ,'3' ,'2' };
int weight[] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
bool check(string x) {
int sum = 0;
for (int i = 0; i < 17; ++i) {
if (x[i] >= '0'&&x[i] <= '9')
sum += weight[i] * charToNum(x[i]);
else
return false;
}
if (hashT[sum % 11] == x[17])
return true;
return false;
}
int main()
{
int n;
int cnt = 0;
string pig;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> pig;
if (!check(pig)) {
save[cnt++] = pig;
}
}
if (cnt == 0)
cout << "All passed" << endl;
else {
for (int i = 0; i < cnt; ++i) {
cout << save[i] << endl;
}
}
return 0;
}
1032
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define MAX_N 100010
using namespace std;
int N;
int HashT[MAX_N];
int main()
{
int index, score;
memset(HashT, 0, sizeof(HashT));
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d %d", &index, &score);
HashT[index] += score;
}
int maxN = 0;
for (int i = 1; i <= N; ++i) {
if (HashT[i] >= HashT[maxN]) maxN = i;
}
printf("%d %d\n", maxN, HashT[maxN]);
return 0;
}
1033
#include <stdio.h>
#include <string.h>
#define toSmall(x) (x-'A'+'a')
#define MAX_N 100010
int isOK[128];
char output[MAX_N];
bool check(char c) {
if (isOK[c])
return false;
if (isOK['+'] && c >= 'A'&&c <= 'Z')
return false;
return true;
}
int main()
{
memset(isOK, 0, sizeof(isOK));
char c;
int cnt = 0;
while ((c = getchar()) != '\n') {
isOK[c] = 1;
if (c >= 'A'&&c <= 'Z')
isOK[toSmall(c)] = 1;
}
while ((c = getchar()) != '\n') {
if (check(c)) output[cnt++] = c;
}
printf("%s\n", output);
return 0;
}
1034
是一道很水但是很麻烦的题。
题目描述中说分子分母输入的范围不超过int的范围,但是两个数相乘有可能超过int的范围,所以必须使用long long
另外复习了一下辗转相除法gcd
:
用较大数除以较小数,若余数为0,则较小数为最大公约数
若余数不为0,用较小数除以余数
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
struct num {
long long inter;
long long son;
long long mom;
};
int gcd(long son, long mom) {
if (son == 0)return mom;
return gcd(mom%son, son);
}
void show(num num1) {
if (num1.mom == 0) {
printf("Inf");
return;
}
bool flag = false;
if (num1.mom < 0) {
num1.son = -num1.son;
num1.mom = abs(num1.mom);
}
if (num1.son < 0) {
flag = true;
num1.son = abs(num1.son);
}
num1.inter = num1.son / num1.mom;
num1.son %= num1.mom;
int p = gcd(num1.son, num1.mom);
num1.son /= p;
num1.mom /= p;
if (flag)printf("(-");
if (num1.inter&&num1.son&&num1.mom) {
printf("%lld %lld/%lld", num1.inter, num1.son, num1.mom);
}
else if (num1.inter==0&&num1.son&&num1.mom) {
printf("%lld/%lld",num1.son, num1.mom);
}
else if (num1.son == 0) {
printf("%lld", num1.inter);
}
if (flag)printf(")");
}
void display(num num1, num num2, num num3,char x) {
show(num1);
printf(" %c ", x);
show(num2);
printf(" = ");
show(num3);
printf("\n");
}
void add(num num1, num num2) {
num pig;
pig.mom = num1.mom*num2.mom;
pig.son = num1.son*num2.mom + num2.son*num1.mom;
display(num1, num2, pig, '+');
}
void dele(num num1, num num2) {
num pig;
pig.mom = num1.mom*num2.mom;
pig.son = num1.son*num2.mom - num2.son*num1.mom;
display(num1, num2, pig, '-');
}
void mul(num num1,num num2) {
num pig;
pig.mom = num1.mom*num2.mom;
pig.son = num1.son*num2.son;
display(num1, num2, pig, '*');
}
void chu(num num1, num num2) {
num pig;
pig.mom = num1.mom*num2.son;
pig.son = num1.son*num2.mom;
display(num1, num2, pig, '/');
}
int main()
{
num num1, num2;
scanf("%lld/%lld %lld/%lld", &num1.son, &num1.mom, &num2.son, &num2.mom);
add(num1, num2);
dele(num1, num2);
mul(num1, num2);
chu(num1, num2);
return 0;
}
1035
#include <iostream>
#include <algorithm>
#include <stdio.h>
#define MAX_N 110
using namespace std;
int arr1[MAX_N], arr2[MAX_N];
int N;
int check() {
int i;
for (i = 1; i < N; ++i) {
if (arr2[i - 1] > arr2[i])
break;
}
int j;
for (j = i; j < N; ++j) {
if (arr1[j] != arr2[j])
break;
}
if (j == N)
return i;
return -1;
}
void merge_arr(int k) {
int i;
for (i = k; i <= N; i += k) {
sort(arr2 + i - k, arr2 + i);
}
sort(arr2 + i - k, arr2 + N);
}
void solve() {
int t;
if ((t = check()) != -1) {
cout << "Insertion Sort" << endl;
sort(arr2, arr2 + t + 1);
}
else {
cout << "Merge Sort" << endl;
int flag;
for (int l = 2; l < N; l *= 2) {
for (int k = l; k < N; k += 2l) {
if (arr2[k] < arr2[k - 1]) {
flag = l * 2;
goto pig;
}
}
}
pig:
merge_arr(flag);
}
printf("%d", arr2[0]);
for (int i = 1; i < N; ++i) {
printf(" %d", arr2[i]);
}
}
int main()
{
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d", &arr1[i]);
}
for (int i = 0; i < N; ++i) {
scanf("%d", &arr2[i]);
}
solve();
return 0;
}
1036
怎么说呢...送分题
#include <stdio.h>
using namespace std;
int main()
{
float N;
char x;
scanf("%f %c", &N, &x);
int Y = (int)(N / 2 + 0.5);
for (int i = 0; i < N; ++i) {
printf("%c", x);
}
printf("\n");
for (int i = 0; i < Y - 2; ++i) {
printf("%c", x);
for (int j = 0; j < N - 2; ++j) {
printf(" ");
}
printf("%c\n", x);
}
for (int i = 0; i < N; ++i) {
printf("%c", x);
}
return 0;
}
1037
这道题有点进制转换的意思...主要思路就是先把所有的钱换到最低位再相减,得到的余数再进行进位,这样的话比较便于操作
#include <stdio.h>
#include <math.h>
using namespace std;
struct money {
int G;
int S;
int K;
};
int trans(money temp) {
return temp.G * 17 * 29 + temp.S * 29 + temp.K;
}
int main()
{
money shouldP, P, R;
scanf("%d.%d.%d %d.%d.%d", &shouldP.G, &shouldP.S, &shouldP.K, &P.G, &P.S, &P.K);
R.K = trans(P) - trans(shouldP);
R.S = R.K / 29;
R.K = abs(R.K % 29);
R.G = R.S / 17;
R.S = abs(R.S % 17);
printf("%d.%d.%d\n", R.G, R.S, R.K);
return 0;
}
1038
这道题存学生的分数当然可以定义一个足够大的数组,一个学生一个学生的放,这样的话查找的时间复杂度为
但是若用一个哈希映射表,不仅省下了空间,而且查找的时间复杂度变成了
#include <stdio.h>
#include <math.h>
#define MAX_N 100010
using namespace std;
int N, K;
int stu[101];
int save[MAX_N];
int main()
{
int j;
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d", &j);
++stu[j];
}
int k;
scanf("%d", &K);
for (int i = 0; i < K; ++i) {
scanf("%d", &k);
save[i] = stu[k];
}
printf("%d", save[0]);
for (int i = 1; i < K; ++i) {
printf(" %d", save[i]);
}
return 0;
}
1039
此题与坏了键盘那道题很像,同样有两种方法,一种是暴力遍历法,一种是哈希映射法。但是此题用遍历法也非常高效,前提是先将字符串按ascii顺序排序。
遍历法:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string buy, me;
int main()
{
cin >> buy >> me;
sort(buy.begin(), buy.end());
sort(me.begin(), me.end());
int cnt = 0;
int j = 0;
for (int i = 0; i < buy.size(); ++i) {
while (buy[i] > me[j] && j < me.size()) {
++j;
}
if (buy[i] == me[j] && j < me.size()) {
++cnt;
++j;
}
}
if (cnt == me.size()) {
cout << "Yes " << buy.size() - cnt << endl;
}
else {
cout << "No " << me.size() - cnt << endl;
}
return 0;
}
哈希映射:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
int ascii[128];
memset(ascii, 0, sizeof(ascii));
while ((c = getchar()) != '\n') ++ascii[c];
while ((c = getchar()) != '\n') --ascii[c];//若ascii数组中存在某个数小于0,则输出No,反之输出Yes
bool flag = true;
int sumY = 0, sumN = 0;
for (int i = 0; i < 128; ++i) {
if (ascii[i] < 0) {
flag = false;
sumN += -ascii[i];
}
if (flag) {
sumY += ascii[i];
}
}
flag ? printf("Yes %d\n", sumY) : printf("No %d\n", sumN);
return 0;
}
1040
这道题就是一个简单的数学问题,对每一个A,构造出的PAT个数等于A之前P的个数x
乘以A之后T的个数y
,令z=x*y
,即求
#include <stdio.h>
#include <string.h>
#define MAX_N 100010
char save[MAX_N];
int num[3] = { 0 };
long sum = 0;
int main()
{
char c;
int cnt = 0;
while ((c = getchar()) != '\n') {
save[cnt++] = c;
if (c == 'P')++num[0];
else if (c == 'A')++num[1];
else ++num[2];
}
int numP = 0;
int numT = num[2];
for (int i = 0; i < cnt; ++i) {
if (save[i] == 'P')numP++;
else if (save[i] == 'T')numT--;
else if (save[i] == 'A')sum += numP * numT;
sum %= 1000000007;
}
printf("%d\n", sum);
return 0;
}
1041
因为N<1000,数据量很小,直接遍历即可
#include <stdio.h>
struct sit {
char id[14];
int test_sit;
int exam_sit;
}sits[1010];
int main()
{
int finds[1010];
int N, M, find;
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%s %d %d", &sits[i].id, &sits[i].test_sit, &sits[i].exam_sit);
}
scanf("%d", &M);
for (int i = 0; i < M; ++i) {
scanf("%d", &find);
for (int j = 0; j < N; ++j) {
if (find == sits[j].test_sit)finds[i] = j;
}
}
for (int i = 0; i < M; ++i) {
printf("%s %d\n", sits[finds[i]].id, sits[finds[i]].exam_sit);
}
return 0;
}
1042
天啊这种题都要写吐了
#define upToLow(x) (x-'A'+'a')
#include <stdio.h>
#include <string.h>
#include <algorithm>
int main()
{
int ASCII[128];
char c;
memset(ASCII, 0, sizeof(ASCII));
while ((c = getchar()) != '\n') {
if (c >= 'a'&&c <= 'z')++ASCII[c];
else if (c >= 'A'&&c <= 'Z')++ASCII[upToLow(c)];
}
int maxN = 0;
for (int i = 1; i < 128; ++i) {
ASCII[maxN] < ASCII[i] ? maxN = i : maxN;
}
printf("%c %d\n", maxN, ASCII[maxN]);
return 0;
}
1043
反正才六个字母,就不用写循环了
#include <stdio.h>
#include <string.h>
#include <algorithm>
int main()
{
char c;
int save[128] = { 0 };
while ((c = getchar()) != '\n') {
++save[c];
}
while (save['P'] || save['A'] || save['T'] || save['e'] || save['s'] || save['t']) {
if (save['P']) { printf("P"); save['P']--; }
if (save['A']) { printf("A"); save['A']--; }
if (save['T']) { printf("T"); save['T']--; }
if (save['e']) { printf("e"); save['e']--; }
if (save['s']) { printf("s"); save['s']--; }
if (save['t']) { printf("t"); save['t']--; }
}
return 0;
}
1044
也没觉得特别难...就是写着写着心态崩了。
打表还是太浪费时间了....
这道题不管是scanf还是cin都不好用,最好使用geiline
getline有三个参数,第一个是输入流cin,第二个是接收输入的字符串,第三个默认是换行。
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
#define charToNum(x) (x-'0')
using namespace std;
struct num {
int ge;
int shi;
int bai;
};
struct fire {
int G;
int S;
};
int N;
const char* fireG[] = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
const char* fireS[] = { "","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
map<string, int> hashT;
num transN(string temp) {
num x;
if (temp.size() == 3) {
x.bai = charToNum(temp[0]);
x.shi = charToNum(temp[1]);
x.ge = charToNum(temp[2]);
}
else if (temp.size() == 2) {
x.shi = charToNum(temp[0]);
x.ge = charToNum(temp[1]);
x.bai = 0;
}
else {
x.ge = charToNum(temp[0]);
x.shi = 0;
x.bai = 0;
}
return x;
}
int main()
{
hashT["tret"] = 0; hashT["jan"] = 1; hashT["feb"] = 2; hashT["mar"] = 3; hashT["apr"] = 4; hashT["may"] = 5;
hashT["jun"] = 6; hashT["jly"] = 7; hashT["aug"] = 8; hashT["sep"] = 9; hashT["oct"] = 10; hashT["nov"] = 11;
hashT["dec"] = 12; hashT["tam"] = 13; hashT["hel"] = 26; hashT["maa"] = 39; hashT["huh"] = 52; hashT["tou"] = 65;
hashT["kes"] = 78; hashT["hei"] = 91; hashT["elo"] = 104; hashT["syy"] = 117; hashT["lok"] = 130; hashT["mer"] = 143;
hashT["jou"] = 156;
scanf("%d", &N);
getchar();
char c;
vector<string> save(N);
string pig;
for (int i = 0; i < N; ++i) {
getline(cin, save[i]);
}
for (int i = 0; i < N; ++i) {
if (save[i][0] >= '0'&&save[i][0] <= '9') {
num x = transN(save[i]);
int all = x.bai * 100 + x.shi * 10 + x.ge;
fire y;
y.G = all % 13;
y.S = all / 13;
if (y.S) {
if (y.G != 0)
printf("%s %s\n", fireS[y.S], fireG[y.G]);
else
printf("%s\n", fireS[y.S]);
}
else {
printf("%s\n", fireG[y.G]);
}
}
else {
int sum = 0;
string temp;
for (int j = 0; j < save[i].size(); ++j) {
if (save[i][j] == ' ') {
sum += hashT[temp];
temp = "";
}
else temp += save[i][j];
}
sum += hashT[temp];
cout << sum << endl;
}
}
return 0;
}
1045
这道题的方法还是很好想的,只需遍历两次数组即可。
第一次从左向右遍历,不断更新最大值
第二次从右向左遍历,不断更新最小值
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#define Max_N 100010
using namespace std;
int main()
{
long N, maxN = 0, minN = 1000000001;
long a[Max_N];
bool isOK[Max_N];
memset(isOK, 1, sizeof(isOK));
scanf("%d", &N);
int cnt = N;
for (int i = 0; i < N; ++i) {
scanf("%ld", &a[i]);
if (maxN < a[i]) {
maxN = a[i];
}
else {
isOK[i] = false;
cnt--;
}
}
for (int j = N - 1; j >= 0; --j) {
if (minN > a[j]) {
minN = a[j];
}
else {
if (isOK[j]) {
isOK[j] = false;
cnt--;
}
}
}
printf("%ld\n", cnt);
bool flag = true;
for (int i = 0; i < N; ++i) {
if (isOK[i]){
if (flag) {
printf("%ld", a[i]);
flag = false;
}
else {
printf(" %ld", a[i]);
}
}
}
putchar('\n');
return 0;
}
1046
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int N, a1, a2, b1, b2;
int cnt_a = 0, cnt_b = 0;
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d %d %d %d", &a1, &a2, &b1, &b2);
if (a1 + b1 == a2 && a1 + b1 != b2) {
cnt_b++;
}
else if (a1 + b1 == b2 && a1 + b1 != a2) {
cnt_a++;
}
}
printf("%d %d\n", cnt_a, cnt_b);
return 0;
}
1047
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct que {
int grade = 0;
int id;
}all[1001];
int main()
{
int N;
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
int temp_que, temp_id, temp_grade;
scanf("%d-%d %d", &temp_que, &temp_id, &temp_grade);
all[temp_que].id = temp_que;
all[temp_que].grade += temp_grade;
}
int max_grade = 0, max_id;
for (int i = 0; i < 1000; ++i) {
if (all[i].grade > max_grade) {
max_grade = all[i].grade;
max_id = all[i].id;
}
}
printf("%d %d\n", max_id, max_grade);
}
1048
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#define charToNum(x) (x-'0')
using namespace std;
char A[100], B[100], solve[100];
char trans[] = { '0','1','2','3','4','5','6','7','8','9','J','Q','K' };
int main()
{
scanf("%s %s", A, B);
int len_A = strlen(A);
int len_B = strlen(B);
int len_min, len_max;
char choose;
if (len_A > len_B) {
len_max = len_A; len_min = len_B; choose = 'A';
}
else { len_min = len_A; len_max = len_B; choose = 'B'; }
int distance = len_max - len_min;
bool flag;
len_max % 2 == 0 ? flag = false : flag = true;
for (int i = 0; i < distance; ++i) {
if (choose == 'A') {
if (flag) {
solve[i] = A[i];
}
else {
int sum = -charToNum(A[i]);
sum < 0 ? sum += 10 : sum;
solve[i] = trans[sum];
}
}
else {
solve[i] = B[i];
}
flag = !flag;
}
for (int i = 0; i < len_min; ++i) {
int j = i, k = i;
if (choose == 'A')
j = distance + i;
else
k = distance + i;
if (flag) {
int sum = (charToNum(A[j]) + charToNum(B[k])) % 13;
solve[i+distance] = trans[sum];
}
else {
int sum = charToNum(B[k]) - charToNum(A[j]);
sum < 0 ? sum += 10 : sum;
solve[i+distance] = trans[sum];
}
flag = !flag;
}
printf("%s\n",solve);
}
1049
#include <stdio.h>
int main()
{
int N;
double ai, sum = 0;
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%lf", &ai);
sum += ai * (N - i)*(i + 1);
}
printf("%.2lf\n", sum);
}
1050
#include <stdio.h>
#include <string.h>
#include <algorithm>
int N, m, n;
int save[10010];
int matrix[10010][10010] = { 0 };
bool compare(int a, int b) {
return a > b;
}
void show() {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (j == 0)printf("%d", matrix[j][i]);
else printf(" %d", matrix[j][i]);
}
putchar('\n');
}
}
void solve() {
int cnt = 0;
matrix[0][0] = save[0];
int x = 0, y = 0;
while (cnt < m*n - 1) {
while (x + 1 < n && !matrix[x + 1][y]) {
matrix[++x][y] = save[++cnt];
}//first
while (y + 1 < m && !matrix[x][y + 1]) {
matrix[x][++y] = save[++cnt];
}//second
while (x - 1 >= 0 && !matrix[x - 1][y]) {
matrix[--x][y] = save[++cnt];
}//third
while (y - 1 >= 0 && !matrix[x][y - 1]) {
matrix[x][--y] = save[++cnt];
}//forth
}
show();
}
int main()
{
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d", &save[i]);
}
for (int i = 1; i*i <= N; ++i) {
if (N%i == 0) {
n = i;
}
}
m = N / n;
sort(save, save + N, compare);
solve();
}
1051
这题真的相当的迷...
用float是无法通过的,必须用double
需要注意的是,浮点数相加减需要用<EOF判断是否为0,而不可以直接判断是否为0。.2lf
的截断也不太常规,一个非常接近0的负数截断之后会变为,所以小于EOF的数要在判断前置0。
printf也有好用的地方,当%后加+号后,可以显示带符号的数。
如果%后加空格,如果值为正,显示空格但不显示+,若为负,则显示-
补充一下,为浮点类型的数求绝对值,为整型求绝对值
#include <stdio.h>
#include <math.h>
#define EOF 0.01
int main()
{
double R1,P1,R2,P2;
scanf("%lf %lf %lf %lf",&R1,&P1,&R2,&P2);
double result_A=R1*cos(P1)*R2*cos(P2)-R1*sin(P1)*R2*sin(P2);
double result_B=R1*cos(P1)*R2*sin(P2)+R1*sin(P1)*R2*cos(P2);
if(fabs(result_A)<EOF)result_A=0;
if(fabs(result_B)<EOF)result_B=0;
printf("%.2lf%+.2lfi",result_A,result_B);
return 0;
}
1052
这是什么大臭题啊哭了。。。
%[^]]
表示输入一个字符串,以]结束,相当于%s的增强版。
#include <stdio.h>
int main()
{
int N,input[10000][5];
char bq[3][10][5],c;
int cnt[3]={0};
for(int i=0;i<3;++i){
for(int index=0;(c=getchar())!='\n';){
if(c=='['){
scanf("%[^]]",bq[i][index++]);
cnt[i]++;
}
}
}
scanf("%d",&N);
for(int i=0;i<N;++i){
for(int j=0;j<5;++j){
scanf("%d",&input[i][j]);
if((j==0||j==4)&&(input[i][j]>cnt[0]||input[i][j]<1))input[i][0]=-1;
else if((j==1||j==3)&&(input[i][j]>cnt[1]||input[i][j]<1))input[i][0]=-1;
else if((j==2)&&(input[i][j]>cnt[2]||input[i][j]<1))input[i][0]=-1;
}
}
for(int i=0;i<N;++i){
if(input[i][0]==-1){
puts("Are you kidding me? @\\/@");
}else
{
printf("%s(%s%s%s)%s\n",bq[0][input[i][0]-1],bq[1][input[i][1]-1],bq[2][input[i][2]-1],bq[1][input[i][3]-1],bq[0][input[i][4]-1]);
}
}
return 0;
}
1053
printf中打出%号需输入%%
#include <stdio.h>
int main()
{
int N,D,cnt_m=0,cnt_n=0;
double e,elec;
scanf("%d %lf %d",&N,&e,&D);
int cnt,days;
for (int i = 0; i < N; i++)
{
cnt=0;
scanf("%d",&days);
for(int j=0;j<days;++j){
scanf("%lf",&elec);
if(elec<e)++cnt;
}
if(cnt*2>days){
days>D?++cnt_n:++cnt_m;
}
}
double m=cnt_m*100/(double)N;
double n=cnt_n*100/(double)N;
printf("%.1lf%% %.1lf%%",m,n);
return 0;
}
1054
了解了sscanf和sprintf的神奇用法
sscanf:读取的字符串,读取的格式,写入的对象
sprintf:写入的对象,写入的格式,读取的对象
注意哦,puts自带空格,不用单独加了。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char a[50],b[50];
double sum=0,temp;
int N,cnt=0;
cin>>N;
for(int i=0;i<N;++i){
scanf("%s",a);
sscanf(a,"%lf",&temp);
sprintf(b,"%.2f",temp);
bool flag=true;
for(int j=0;j<strlen(a);++j){
if(a[j]!=b[j]){flag=false;break;}
}
if(!flag||temp<-1000||temp>1000){
printf("ERROR: %s is not a legal number\n",a);
continue;
}else{
sum+=temp;
cnt++;
}
}
if(cnt==0){
puts("The average of 0 numbers is Undefined");
}else if(cnt==1){
printf("The average of 1 number is %.2f",sum);
}else{
printf("The average of %d numbers is %.2f",cnt,sum/cnt);
}
return 0;
}
1055
strcmp:
设这两个字符串为str1,str2,
若str1=str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct people{
int tall;
char name[10];
}queue[10010];
int N,K;
people arrange[10010];
bool compare(people a,people b){
return a.tall!=b.tall?a.tall<b.tall:strcmp(a.name,b.name)>0;
}
int main()
{
scanf("%d %d",&N,&K);
for(int i=0;i<N;++i){
scanf("%s %d",&queue[i].name,&queue[i].tall);
}
sort(queue,queue+N,compare);
int step=N/K,cnt=0;
int last_step=N-step*(K-1);
for(int i=0;i<N-last_step;i+=step){
for(int j=i+step-1;j>=i;--j){
int pos=i+step/2+1+cnt;
cnt<0?cnt=-cnt:cnt=-cnt-1;
arrange[pos]=queue[j];
}
cnt=0;
}
for(int i=N-1;i>=N-last_step;--i){
int pos=last_step/2+1+cnt+N-last_step;
cnt<0?cnt=-cnt:cnt=-cnt-1;
arrange[pos]=queue[i];
}
bool flag=true;
for(int i=N-last_step+1;i<=N;++i){
if(flag){
printf("%s",arrange[i].name);
flag=false;
}else{
printf(" %s",arrange[i].name);
}
}
putchar('\n');
for(int i=N-last_step-step;i>=0;i-=step){
flag=true;
for(int j=i+1;j<=i+step;j++){
if(flag){
printf("%s",arrange[j].name);
flag=false;
}else{
printf(" %s",arrange[j].name);
}
}
putchar('\n');
}
return 0;
}
1056
数学问题,推出通式为
#include <iostream>
using namespace std;
int main()
{
int N,sum=0,temp;
cin>>N;
for(int i=0;i<N;++i){
cin>>temp;
sum+=temp*11*(N-1);
}
cout<<sum<<endl;
return 0;
}
1057
进制转换问题,思路挺简单的
#include <iostream>
#define upperToNum(x) (x-'A')
#define lowerToNum(x) (x-'a')
using namespace std;
int sum=0,cnt_0=0,cnt_1=0;
void solve(){
if(sum==0)
return;
if(sum%2==0)cnt_0++;
else cnt_1++;
sum/=2;
solve();
}
int main()
{
char c;
while((c=getchar())!='\n'){
if(c>='A'&&c<='Z')sum+=upperToNum(c)+1;
else if(c>='a'&&c<='z')sum+=lowerToNum(c)+1;
}
solve();
cout<<cnt_0<<" "<<cnt_1<<endl;
return 0;
}
1058
令人害怕的大模拟题...除了硬着头皮往下写也没什么其他办法
#define charToNum(x) (x-'0')
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct prob{
int grades;
int all_choose;
int cor_choose;
int wrong;
string save;
}probs[100];
int N,M;
int main()
{
cin>>N>>M;
char c;
for(int i=0;i<M;++i){
probs[i].wrong=0;
cin>>probs[i].grades>>probs[i].all_choose>>probs[i].cor_choose;
for(int j=0;j<probs[i].cor_choose;++j){
cin>>c;
probs[i].save+=c;
}
}
string person;
int max_wrong=0;
for(int i=0;i<N;++i){
int num=-1,cnt=0,sum=0;
string compare;
getchar();
getline(cin,person);
for(int i=0;i<person.size();++i){
if(person[i]>='0'&&person[i]<='9')num=charToNum(person[i]);
if(person[i]>='a'&&person[i]<='z')compare+=person[i];
if(person[i]==')'){
sort(compare.begin(),compare.end());
if(num==probs[cnt].cor_choose&&compare==probs[cnt].save)
sum+=probs[cnt].grades;
else probs[cnt].wrong++;
compare="";
cnt++;
max_wrong=max(probs[cnt].wrong,max_wrong);
}
}
cout<<sum<<endl;
}
if(max_wrong==0){
cout<<"Too simple"<<endl;
}else{
cout<<max_wrong;
for(int i=0;i<M;++i){
if(probs[i].wrong==max_wrong)
cout<<" "<<i+1;
}
}
return 0;
}
1059
这题用哈希散列更加方便而且大大降低了复杂度,用空间换时间
#include <vector>
#include <string>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int N,M;
int people[10010];
bool visit[10010];
bool isPrime(int x){
for(int i=2;i*i<=x;++i){
if(x%i==0)
return false;
}
return true;
}
void finding(int find){
if(visit[find]){
printf("%04d: Checked\n",find);
return;
}
else if(people[find]==0){
printf("%04d: Are you kidding?\n",find);
return;
}
if(people[find]==1){
printf("%04d: Mystery Award\n",find);
visit[find]=true;
return;
}else if(isPrime(people[find])){
printf("%04d: Minion\n",find);
visit[find]=true;
return;
}else{
printf("%04d: Chocolate\n",find);
visit[find]=true;
return;
}
}
int main()
{
memset(visit,0,sizeof(visit));
memset(people,0,sizeof(people));
int num;
scanf("%d",&N);
for(int i=0;i<N;++i){
scanf("%d",&num);
people[num]=i+1;
}
int find;
scanf("%d",&M);
for(int i=0;i<M;++i){
scanf("%d",&find);
finding(find);
}
return 0;
}
1060
这道题其实并没有卡时间,但是保险起见我还是用了二分法。
这是一道极大最小值的模板题。
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int N,save[100010];
void solve(){
int left=1,right=N,mid;
while(right-left>1){
mid=(left+right)/2;
if(save[mid]>mid)left=mid;
else right=mid;
}
save[right]>right?mid=right:mid=left;
printf("%d\n",mid);
}
bool compare(int a,int b){
return a>b;
}
int main()
{
int max_num=0;
scanf("%d",&N);
for(int i=1;i<=N;++i){
scanf("%d",&save[i]);
max_num=max(max_num,save[i]);
}
if(max_num==1)
{
printf("0\n");
return 0;
}
sort(save+1,save+N+1,compare);
solve();
return 0;
}
1061
#include <stdio.h>
#include <iostream>
using namespace std;
struct prob{
int grade;
bool ans;
}probs[110];
int main()
{
int N,M;
cin>>N>>M;
for(int i=0;i<M;++i){
cin>>probs[i].grade;
}
for(int i=0;i<M;++i){
cin>>probs[i].ans;
}
for(int i=0;i<N;++i){
int sum=0;
bool temp;
for(int i=0;i<M;++i){
cin>>temp;
if(temp==probs[i].ans)
sum+=probs[i].grade;
}
cout<<sum<<endl;
}
return 0;
}
1062
主要是读题时间比较久,姥姥要好好提高语文水平呀
#include <stdio.h>
#include <iostream>
using namespace std;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
void swap(int &a,int &b){
int temp=a;
a=b;
b=temp;
}
int main()
{
int son1,son2,mom1,mom2,mom;
scanf("%d/%d %d/%d %d",&son1,&mom1,&son2,&mom2,&mom);
double x1=(double)son1/(double)mom1;
double x2=(double)son2/(double)mom2;
if(x1>x2) swap(x1,x2);
bool flag=true;
for(int i=0;i<1000;++i){
if(gcd(mom,i)!=1)continue;
double temp=(double)i/(double)mom;
if(temp>x1&&temp<x2){
if(flag){
printf("%d/%d",i,mom);
flag=false;
}else{
printf(" %d/%d",i,mom);
}
}
if(temp>=x2)break;
}
return 0;
}
1063
emmmm
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
int N,n,v;
cin>>N;
int max_n=0;
for(int i=0;i<N;++i){
cin>>n>>v;
max_n=max(max_n,n*n+v*v);
}
printf("%.2f\n",sqrt(max_n));
}
1064
set大法好,柳神牛逼!
#include <stdio.h>
#include <set>
#include <iostream>
#define charToInt(x) (x-'0')
using namespace std;
int main()
{
char c;
int sum=0,N;
set<int> save;
cin>>N;getchar();
while((c=getchar())!='\n'){
if(c==' '){
save.insert(sum);
sum=0;
}else{
sum+=charToInt(c);
}
}
save.insert(sum);
set<int>::iterator scan=save.begin();
bool flag=true;
cout<<save.size()<<endl;
while(scan!=save.end()){
if(flag){
cout<<*scan;
flag=false;
}else{
cout<<" "<<*scan;
}
scan++;
}
}
1065
因为是的复杂度,把大数组全部遍历一遍也问题不大
#include <stdio.h>
#include <set>
#include <string.h>
#include <iostream>
#define MAX_N 100010
using namespace std;
bool people[MAX_N];
int couple[MAX_N];
int main()
{
int N,M,id1,id2,id;
set<int> save;
set<int>::iterator scan;
memset(people,0,sizeof(people));
memset(couple,-1,sizeof(couple));
scanf("%d",&N);
for(int i=0;i<N;++i){
scanf("%d %d",&id1,&id2);
couple[id1]=id2;
couple[id2]=id1;
}
scanf("%d",&M);
for(int i=0;i<M;++i){
scanf("%d",&id);
people[id]=true;
}
for(int i=0;i<MAX_N;++i){
if(people[i]&&(couple[i]==-1||(couple[i]!=-1&&people[couple[i]]==false))){
save.insert(i);
}
}
printf("%d\n",save.size());
bool flag=true;
for(scan=save.begin();scan!=save.end();scan++){
if(flag){
printf("%05d",*scan);
flag=false;
}else{
printf(" %05d",*scan);
}
}
}
1066
#include <stdio.h>
#include <iostream>
using namespace std;
int colors[500][500];
int main()
{
int M,N,A,B,color;
scanf("%d %d %d %d %d",&M,&N,&A,&B,&color);
for(int i=0;i<M;++i)
for(int j=0;j<N;++j){
scanf("%d",&colors[i][j]);
if(colors[i][j]>=A&&colors[i][j]<=B)colors[i][j]=color;
}
for(int i=0;i<M;++i){
for(int j=0;j<N;++j){
j==0?printf("%03d",colors[i][j]):printf(" %03d",colors[i][j]);
}
putchar('\n');
}
return 0;
}
1067
这题只是正确的密码不包含空格,没说用户输入的密码不包含空格,所以scanf或者cin都不能用,只能用getline。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N,cnt=0;
string psw;
cin>>psw>>N;
string input;
getchar();
while(1){
getline(cin,input);
if(input=="#")
break;
if(input==psw){
cout<<"Welcome in";
break;
}
else{
cout<<"Wrong password: "<<input<<endl;
}
if(++cnt==N){
cout<<"Account locked";
break;
}
}
return 0;
}
1068
基础的搜索问题
为啥用int可以ac,用long就有一个测试点答案错误?
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <map>
#include <string.h>
using namespace std;
int canvas[1000][1000];
map<int,int> flag;
int step;
int M,N;
bool examine(int x,int y){
for(int i=-1;i<=1;++i)
for(int j=-1;j<=1;++j){
if((i==0&&j==0)||(x+i<0)||(x+i>=N)||(y+j<0)||(y+j>=M))
continue;
else if(abs(canvas[x][y]-canvas[x+i][y+j])<=step){
return false;
}
}
return true;
}
int main()
{
int save[2];
int cnt=0;
memset(save,-1,sizeof(save));
scanf("%d %d %d",&M,&N,&step);
for(int i=0;i<N;++i){
for(int j=0;j<M;++j){
scanf("%d",&canvas[i][j]);
++flag[canvas[i][j]];
}
}
for(int i=0;i<N;++i){
for(int j=0;j<M;++j){
if(flag[canvas[i][j]]>1)continue;
else if(examine(i,j)){
if(++cnt>=2){
printf("Not Unique\n");
return 0;
}
save[0]=j;
save[1]=i;
}
}
}
if(save[0]==-1)printf("Not Exist\n");
else printf("(%d, %d): %d\n",save[0]+1,save[1]+1,canvas[save[1]][save[0]]);
return 0;
}
1069
如果用一个数组取映射的话空间都炸了,所以改用map映射
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
map<string,int> cal;
int main()
{
int M,N,S;
string bf;
scanf("%d %d %d",&M,&N,&S);
int cnt=S;
for(int i=1;i<=M;++i){
cin>>bf;
if(i==cnt){
if(cal[bf]==0){
cout<<bf<<endl;
cal[bf]=1;
cnt+=N;
}
else cnt++;
}
}
if(S>M)printf("Keep going...\n");
return 0;
}
1070
基础的贪心法
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int N;
int save[100000];
scanf("%d",&N);
for(int i=0;i<N;++i){
scanf("%d",&save[i]);
}
sort(save,save+N);
int sum=save[0];
for(int i=1;i<N;++i){
sum=(sum+save[i])/2;
}
printf("%d",sum);
return 0;
}
1071
输出格式有错误。。。少了个空格
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int T,K;
int n1,b,n2;
long t;
cin>>T>>K;
for(int i=0;i<K;++i){
cin>>n1>>b>>t>>n2;
if(t>T){
printf("Not enough tokens. Total = %ld.",T);
continue;
}
if((b==0&&n1>n2)||(b==1&&n1<n2)){
T+=t;
printf("Win %ld! Total = %ld.",t,T);
}else{
T-=t;
printf("Lose %ld. Total = %ld.",t,T);
}
if(T==0){
printf("Game Over.");
break;
}
}
return 0;
}
1072
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool objects[10000];
char name[5];
int main()
{
memset(objects,0,sizeof(objects));
int N,M,temp,num=0,peo=0;
scanf("%d %d",&N,&M);
for(int i=0;i<M;++i){
scanf("%d",&temp);
objects[temp]=true;
}
int size,object;
for(int j=0;j<N;++j){
int save[10],cnt=0;
scanf("%s %d",name,&size);
for(int i=0;i<size;++i){
scanf("%d",&object);
if(objects[object]){
save[cnt++]=object;
}
}
if(cnt!=0){
peo++;
num+=cnt;
printf("%s:",name);
for(int i=0;i<cnt;++i)printf(" %04d",save[i]);
putchar('\n');
}
}
printf("%d %d\n",peo,num);
return 0;
}
1073
令人害怕的大模拟题.jpg
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#define charToNum(x) (x-'a')
using namespace std;
struct prob{
int score;
int chioce;
int isTrue;
bool ans[5];
}probs[1000];
int cnt_wrong[1000][5];
int max_num=0;
float grade(bool answer[],int num){
bool flag1=true;
bool flag2=true;
for(int i=0;i<probs[num].chioce;++i){
if(probs[num].ans[i]==false&&answer[i]==true){
max_num=max(max_num,++cnt_wrong[num][i]);
flag1=false;
}
if(probs[num].ans[i]==true&&answer[i]==false){
max_num=max(max_num,++cnt_wrong[num][i]);
flag2=false;
}
}
if(!flag1)return 0;
if(flag2) return probs[num].score;
return ((float)probs[num].score)/2;
}
int main()
{
int N,M;
char choose;
scanf("%d %d",&N,&M);
for(int i=0;i<M;++i){
memset(probs[i].ans,0,sizeof(probs[i].ans));
memset(cnt_wrong[i],0,sizeof(cnt_wrong[i]));
cin>>probs[i].score>>probs[i].chioce>>probs[i].isTrue;
for(int j=0;j<probs[i].isTrue;++j){
cin>>choose;
probs[i].ans[charToNum(choose)]=true;
}
}
getchar();
for(int i=0;i<N;++i){
float grades=0;
char c;
bool save[5];
memset(save,0,sizeof(save));
int cnt=0;
while((c=getchar())!='\n'){
if(c>='a'&&c<='e')save[charToNum(c)]=true;
else if(c==')'){
grades+=grade(save,cnt++);
memset(save,0,sizeof(save));
}
}
printf("%.1f\n",grades);
}
if(max_num==0){
printf("Too simple\n");
return 0;
}
for(int i=0;i<M;++i){
for(int j=0;j<probs[i].chioce;++j){
if(max_num==cnt_wrong[i][j])
printf("%d %d-%c\n",max_num,i+1,j+'a');
}
}
return 0;
}
1074
reverse函数真的炒鸡好用呀
#include <iostream>
#include <string>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#define charToNum(x) (x-'0')
using namespace std;
string a,b,index1;
int sum[20];
int pos=0,pos_a=0,pos_b=0;
void trans(int x){
if(x==0)x=10;
int temp=0;
if(pos_a<a.size()&&pos_b<b.size())
temp=charToNum(a[pos_a++]) + charToNum(b[pos_b++]);
else if(pos_a==a.size())
temp=charToNum(b[pos_b++]);
else if(pos_b==b.size())
temp=charToNum(a[pos_a++]);
sum[pos]+=temp;
sum[pos+1]+=sum[pos]/x;
sum[pos++]%=x;
}
int main()
{
memset(sum,0,sizeof(sum));
cin>>index1>>a>>b;
reverse(index1.begin(),index1.end());
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int max=a.size()>b.size()?a.size():b.size();
for(int i=0;i<max;++i){
trans(charToNum(index1[i]));
}
while(sum[pos]==0&&pos>0)pos--;
for(;pos>=0;pos--){
printf("%d",sum[pos]);
}
return 0;
}
1075
柳神的答案写的真好,用一个哈希映射表储存链表让时间复杂度大大降低,写出来的代码也简洁很多。
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
struct node{
int next;
int data;
}list[100000];
vector<int> v[3];
int main()
{
int head,N,K;
scanf("%d %d %d",&head,&N,&K);
int pos;
for(int i=0;i<N;++i){
scanf("%d",&pos);
scanf("%d%d",&list[pos].data,&list[pos].next);
}
while(head!=-1){
if(list[head].data<0)
v[0].push_back(head);
else if(list[head].data<=K)
v[1].push_back(head);
else
v[2].push_back(head);
head=list[head].next;
}
bool flag=true;
for(int i=0;i<3;++i){
for(int j=0;j<v[i].size();++j){
if(flag){
printf("%05d %d",v[i][j],list[v[i][j]].data);
flag=false;
}
else{
printf(" %05d\n%05d %d",v[i][j],v[i][j],list[v[i][j]].data);
}
}
}
printf(" -1");
return 0;
}