出处:PAT A1077
题目:给定N个字符串,求公共后缀
思路:二维数组输入每个字符串,对它们反转。选择其中最短的字符串作为比较串,比较每一个字符如果有一个字符不同退出循环,相同计数器++。然后根据长度打印字符串
要点:
reverse函数使用:reverse(list[i],list[i]+len):从起点开始反转len长度的字符串
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
int minlen = INT_MAX;
char list[100][2000] = {""};
cin >> n;
getchar();
for (int i = 0; i < n; i++)
{
gets_s(list[i]);
int len = strlen(list[i]);
if (len < minlen)minlen = len;
reverse(list[i], list[i] + len);
}
int ans = 0;
for (int i = 0; i < minlen; i++)
{
char c = list[0][i];
bool issame = true;
for (int j = 1; j < n; j++)
{
if (list[j][i] != c)
{
issame = false;
break;
}
}
if (issame)
ans++;
else break;
}
if (ans)
{
for (int i = ans - 1; i >= 0; i--)
cout << list[0][i];
}
else
cout << "nai";
return 0;
}
出处:PAT a1035
题目:给定N个用户的姓名密码,改变其中的特定字符,求需要修改的密码个数和需要修改的用户名密码
思路:用结构体数组储存N个用户的姓名和密码。循环判断每一个字符是否是需要修改的字符,如果是就进行修改。当修改完一个用户之后计数器++表示需要修改的用户+1
//#include<iostream>
//#include<string>
//using namespace std;
//
//struct node
//{
// char name[20], password[1000];
// bool ischange;
//}T[20000];
//
//
//void ischange1(node& t,int &cnt)
//{
// int len = strlen(t.password);
// for (int i = 0; i < len; i++)
// {
// if (t.password[i] == '1')
// {
// t.password[i] = '@';
// t.ischange= true;
// }
// if (t.password[i] == '0')
// {
// t.password[i] = '%';
// t.ischange = true;
// }
// if (t.password[i] == '1')
// {
// t.password[i] = 'L';
// t.ischange = true;
// }
// if (t.password[i] == 'O')
// {
// t.password[i] = 'o';
// t.ischange = true;
// }
// }
// if (t.ischange == true)
// {
// cnt++;
// }
//}
//
//int main()
//{
// int n, count = 0;
// scanf_s("%d", &n);
// for (int i = 0; i < n; i++)
// {
// cin>>T[i].name>>T[i].password;
// T[i].ischange = false;
// }
// for (int i = 0; i < n; i++)
// {
// ischange1(T[i], count);
// }
// if (count == 0)
// {
// if (count == 1)
// cout << "There is 1 account and no account is modified";
// else
// cout << "There are " << n << "accounts and no account is modified";
// }
// else
// {
// cout << count << endl;
// for (int i = 0; i < n; i++)
// {
// if (T[i].ischange)
// cout << T[i].name << ' ' << T[i].password << endl;
// }
// }
//}
出处:PAT B1009
题目描述:给定一个英语字符串,要求编写程序,将句中所有单词按颠倒顺序输出
思路:创建二维数组,以空格作为分隔符将每个单词送入数组中。然后再从后往前遍历单词得到倒序输出
注意点:字符串数组的初始化:list[1000]=""
//#include <stdio.h>
//#include <string.h>
//
//int main()
//{
// char str[90]="";
// gets_s(str);
// int len = strlen(str);
// int r = 0, h = 0;
// char ans[90][90] = {""};
//
// int i;
// for (i = 0; i < len; i++) {
// if (str[i] != ' ') {
// ans[r][h++] = str[i];
// }
// else {
// ans[r][h] = '\0';
// r++;
// h = 0;
// }
// }
//
// for (i = r; i >= 0; i--)
// {
// printf("%s", ans[i]);
// if (i > 0)
// printf(" ");
// }
// return 0;
//}
出处:PAT B1021
题目描述:给定一个 k 位整数 N=dk−110k−1+⋯+d1101+d0 (0≤di≤9, i=0,⋯,k−1, dk−1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。
输入
100311
输出
0:2
1:3
3:1
思路:将字符串读入后用哈希方法储存在数组里,并对每个数字出现次数计数。数组下标i表示输入的数字,list[i]数字出现的次数。list[str[i]-'0']++表示读入的某一位数字添加到对应的下标中计数
最后将下标内容中不等于0的输出即可
//#include<iostream>
//#include<string>
//using namespace std;
//
//int main()
//{
// char str[100];
// gets_s(str);
// int len = strlen(str);
// int count[10] = { 0 };
// for (int i = 0; i < len; i++)
// {
// count[str[i] - '0']++;
// }
// for (int i = 0; i < 10; i++)
// {
// if (count[i] != 0)
// {
// printf_s("%d:%d\n", i, count[i]);
// }
// }
// return 0;
//}
出处:PATB1024
题目:将科学计数法展开为数字表示
sample input:
+1.23400E-03
0.00123400
思路:首先判断数字正负,如果是负数先输出符号。
1.计算E的位置,遍历数字找到E的位置记为pos
2.判断E后的数字是否为0,如果为0直接输出E前面的数字
3.判断E后数字n的符号是为是负号,如果是负号先输出“0.”,再补足n-1个0,最后输出E前面的数字
4.判断E后的数字n的是否是正号,输出第一位数字,跳过“.”后输出剩余数字,同时计数器等于E并随着循环递减当计数器为0时更新“.”的位置。如果输出所有数字后计数器依然>0,则进行补0知道计数器为0。
//#include<iostream>
//#include<string>
//using namespace std;
//
//int main()
//{
// char str[20000];
// gets_s(str);
// int len = strlen(str);
// if (str[0] == '-')cout << '-';
//
// int pos = 0;
// while (str[pos] != 'E')
// {
// pos++;
// }
//
// int exp = 0;
// for (int i = pos + 2; i < len; i++)
// {
// exp = exp * 10 + (str[i] - '0');//计算指数
// }
// //如果exp是0则直接输出pos前的内容
// if (exp == 0)
// for (int i = 1; i < pos; i++)
// cout << str[i];
// //负数
// if (str[pos + 1] == '-')
// {
// cout << "0.";
// for (int i = 0; i < exp - 1; i++)
// cout << '0';
// cout << str[1];
// for (int i = 3; i < pos; i++)
// cout << str[i];
// }
// //正数
// else
// {
// cout << str[1];
// //exp少于正数
// for (int i = 3; i < pos; i++)
// {
// cout << str[i];
// exp--;
// if (exp == 0 && i != pos - 1)cout << '.';
// }
// //exp多于正数
// if (exp > 0)
// {
// for (int i = 0; i < exp; i++)
// {
// cout << '0';
// }
// }
// }
// return 0;
//}