互联网行业的小白,写博客的目的是为了记录自己的学习过程、对自己学习中所犯的错误做一个总结。由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!
题目传送门:害死人不偿命的(3n+1)猜想
#include
using namespace std;
int main(){
int n;
cin>>n;
int count=0;
if(n!=1){
//注意n为1的情况!
while(1){
if(n%2==1)
n=3*n+1;
n/=2;
count++;
if(n==1) break;
}
}
cout<<count;
return 0;
}
题目传送门:写出这个数
#include
#include
#include
using namespace std;
int main(){
char str;
int sum=0;
while((str=getchar())!='\n')
sum += (str - '0');
if(sum==0) cout<<"ling"<<endl;//输入为0的情况另外考虑,不过对本题无影响!
int b[101],j=0;
while(sum){
//将求和后sum的每一位存在数组b中
b[j++]=sum%10;
sum/=10;
}
for(int i=j-1;i>=0;i--){
switch(b[i]){
case 0:
cout<<"ling";break;
case 1:
cout<<"yi";break;
case 2:
cout<<"er";break;
case 3:
cout<<"san";break;
case 4:
cout<<"si";break;
case 5:
cout<<"wu";break;
case 6:
cout<<"liu";break;
case 7:
cout<<"qi";break;
case 8:
cout<<"ba";break;
case 9:
cout<<"jiu";break;
}
if(i!=0) cout<<" ";//注意最后一个没有空格
}
return 0;
}
题目传送门:我要通过!
#include
#include
#include
using namespace std;
int main(){
int n;
cin>>n;
getchar();
string *str = new string[n];
for(int i=0;i<n;i++)
getline(cin,str[i]);
for(int i=0;i<n;i++){
int nP=0,nA=0,nT=0;
bool flag=false;
for(int j=0;j<str[i].size();j++){
if(str[i][j]=='P') nP++;
if(str[i][j]=='A') nA++;
if(str[i][j]=='T') nT++;
}
if(nP==1&&nT==1&&nA>0&&nP+nA+nT==str[i].size()) flag=true;//nA>0是为了防止出现没有A的情况
if(!flag){
cout<<"NO"<<endl;
}
else{
int len_l=0,len_m=0,len_r=0,j=0;
for(j=0;j<str[i].size();j++){
if(str[i][j]=='P') {
len_l=j;
break;
}
}
j++;
while(str[i][j]!='T'){
len_m++;
j++;
}
j++;
len_r=str[i].size()-j;
if(len_l*len_m==len_r) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
题目传送门:成绩排名
#include
#include
using namespace std;
struct Student{
string name;
string num;
int score;
};
int main(){
int n;
cin >> n;
Student *stu = new Student[n];
for (int i = 0; i < n; i++){
cin >> stu[i].name >> stu[i].num >> stu[i].score;
}
int max = stu[0].score, f1=0;
int min = stu[0].score, f2=0;
for (int i = 0; i < n; i++){
if (stu[i].score>max){
max = stu[i].score;
f1 = i;
}
if (stu[i].score < min){
min = stu[i].score;
f2 = i;
}
}
cout << stu[f1].name << " " << stu[f1].num << endl;
cout << stu[f2].name << " " << stu[f2].num << endl;
}
题目传送门: 继续(3n+1)猜想
#include
#include
#include
#include
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int n;
cin>>n;
int *p = new int[n];
for(int i=0;i<n;i++)
cin>>p[i];
set<int>s;
for(int i=0;i<n;i++){
int temp=p[i];
while(1){
if(temp%2!=0) temp=3*temp+1;
temp/=2;
s.insert(temp);
if(temp==1) break;
}
}
set<int>::iterator it;
vector<int>ans;//用于存放结果的向量
for(int i=0;i<n;i++){
int count=0;
for(it=s.begin();it!=s.end();++it){
if(p[i]!=*it) count++;
if(count==s.size()) ans.push_back(p[i]);
}
}
sort(ans.begin(),ans.end(),cmp);//为了从大到小输出
for(int i=0;i<ans.size();i++){
if(i==ans.size()-1) cout<<ans[i];
else cout<<ans[i]<<" ";
}
return 0;
}
题目传送门:换个格式输出整数
#include
using namespace std;
int main(){
int n;
cin >> n;
int a[5], k = 0;
while (n){
a[k++] = n % 10;
n /= 10;
}
if (k == 3){
for (int i = 0; i < a[2]; i++)
cout << 'B';
k--;
}
if (k == 2){
for (int i = 0; i < a[1]; i++)
cout << 'S';
k--;
}
if (k == 1){
for (int i = 1; i <= a[0]; i++)
cout << i;
}
return 0;
}
题目传送门:素数对猜想
#include
#include
using namespace std;
bool Is_Prime(int n){
if (n < 2) return false;
for (int i = 2; i <= sqrt(n); i++){
if (n%i == 0) return false;
}
return true;
}
int main(){
int n;
cin >> n;
int ans = 0;
for (int i = 2; i <= n; i++){
if (Is_Prime(i) && Is_Prime(i-2))
ans++;
}
cout << ans;
return 0;
}
题目传送门: 数组元素循环右移问题
#include
using namespace std;
int main(){
int n, m;
cin >> n >> m;
int *a = new int[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int f = m%n;//当大于数组的长度时,直接取模减少移动次数
for (int i = n - f; i < n; i++)
cout << a[i] << " ";
for (int i = 0; i < n - f; i++){
if (i != n - f - 1) cout << a[i] << " ";
else cout << a[i];//输出的最后一个没有空格
}
return 0;
}
题目传送门: 说反话
#include
#include
using namespace std;
int main(){
string str;
getline(cin, str);
int k = 0,j;
char *p = new char[str.size()];
for (int i = str.size() - 1; i >= 0; i--){
if (str[i] != ' '){
p[k++] = str[i];
}
else{
for (j = k - 1; j >= 0; j--)
cout << p[j];
cout << " ";
k = 0;
}
}
for (j = k - 1; j >= 0; j--)//第一个字符串还没有输出。
cout << p[j];
//或者下面这样也行:
/*
for (int i = 0; i < str.size(); i++){//输出第一个字符串
if (str[i] == ' ') break;
else cout << str[i];
}
*/
return 0;
}
题目传送门:一元多项式求导
#include
using namespace std;
int main(){
int a,b;
bool flag = false;
while(cin>>a>>b){
if(a*b){
if(!flag)
flag=true;
else
cout<<" ";
cout<<a*b<<" "<<b-1;
}
}
if(!flag)
cout<<"0 0"<<endl;
return 0;
}
题目传送门:A+B 和 C
#include
using namespace std;
int main(){
long long T,a,b,c;
cin>>T;
for(int i=0;i<T;i++){
cin>>a>>b>>c;
if(a+b<=c) cout<<"Case #"<<i+1<<": false"<<endl;
else cout<<"Case #"<<i+1<<": true"<<endl;
}
return 0;
}
题目传送门:数字分类
#include
#include
using namespace std;
int main(){
int n;
cin>>n;
bool flag1=false,flag2=false,flag3=false,flag4=false,flag5=false;
int *p=new int[n];
for(int i=0;i<n;i++)
cin>>p[i];
int A1=0,A2=0,A3=0,A5=0;
float A4=0.0;
int *tempA5=new int[n],kA4=0,kA5=0,cnt=0;
for(int i=0;i<n;i++){
int select=p[i]%5+1;
switch(select){
case 1:
if(p[i]%2==0){
A1+=p[i];
flag1=true;
}
break;
case 2:
if(cnt%2!=0) p[i]=-p[i];
A2+=p[i];
cnt++;
flag2=true;
break;
case 3:
A3++;
flag3=true;
break;
case 4:
A4+=p[i];
kA4++;
flag4=true;
break;
default:
tempA5[kA5++]=p[i];
flag5=true;
break;
}
}
A4=(float)A4/kA4;//容易错,用强制类型转换
A5=tempA5[0];
for(int i=0;i<kA5;i++){
if(tempA5[i]>A5) A5=tempA5[i];
}
if(!flag1) cout<<'N'<<" ";
else cout<<A1<<" ";
if(!flag2) cout<<'N'<<" ";
else cout<<A2<<" ";
if(!flag3) cout<<'N'<<" ";
else cout<<A3<<" ";
if(!flag4) cout<<'N'<<" ";
else cout<<setiosflags(ios::fixed)<<setprecision(1)<<A4<<" ";//不加setiosflags(ios::fixed)输出格式错误
if(!flag5) cout<<'N';
else cout<<A5;
return 0;
}
题目传送门: 数素数
#include
#include
using namespace std;
bool Is_Prime(int n){
if (n < 2) return false;
for (int i = 2; i <= sqrt(n); i++){
if (n%i == 0) return false;
}
return true;
}
int main(){
int M,N;
cin>>M>>N;
int count=0,k=0;
for(int i=2;;i++){
if(Is_Prime(i)){
count++;
if(count>=M&&count<=N){
cout<<i;k++;
if(k%10==0) cout<<endl;
else if(count!=N) cout<<" ";//最后一个不能有空格
else{
}
}
if(count>N) break;//for循环结束的条件
}
}
return 0;
}
题目传送门:福尔摩斯的约会
/*
星期是第一对字符串的"第一对"相同的大写英文字母。
小时是第一对字符串的"第二对"相同的字符(数字+大写英文字母)
分钟是第二对字符串的"第一对"相同的英文字母(大写+小写)
小时或者分钟不满足两位的要记得补零。
*/
#include
#include
using namespace std;
int main() {
string *str = new string[4];
for(int i = 0; i < 4; i++)
getline(cin,str[i]);
bool flag = false;
for(int i = 0; i < 61; i++){
if(str[0][i] == str[1][i] && str[0][i] >= 'A' && str[0][i] <='G' && flag == false){
switch(str[0][i]){
case 'A':
cout<<"MON ";break;
case 'B':
cout<<"TUE ";break;
case 'C':
cout<<"WED ";break;
case 'D':
cout<<"THU ";break;
case 'E':
cout<<"FRI ";break;
case 'F':
cout<<"SAT ";break;
case 'G':
cout<<"SUN ";break;
}
flag = true;
continue;
}
if(str[0][i] == str[1][i] && ((str[0][i] >= 'A' && str[0][i] <='N') || (str[0][i] >= 48 && str[0][i] <=57)) && flag == true){
if(str[0][i] >= 48 && str[0][i] <= 57) cout<<"0"<<str[0][i]-48;
else cout<<str[0][i]-'A'+10;
break;
}
}
for(int i = 0; i < 61; i++){
if(str[2][i] == str[3][i] && ((str[2][i] >= 'a' && str[2][i] <= 'z')||(str[2][i] >= 'A' && str[2][i] <= 'Z'))){
cout<<":";
if(i <= 9 && i >= 0)
cout<<"0";
cout<<i;
break;
}
}
return 0;
}
题目传送门:部分A+B
#include
#include
using namespace std;
int main() {
string A, B;
int DA, DB;
cin >> A >> DA >> B >> DB;
int nDA = 0, nDB = 0;
for (int i = 0; i < A.size(); i++)
if (A[i] - 48 == DA)
nDA++;
for (int i = 0; i < B.size(); i++)
if (B[i] - 48 == DB)
nDB++;
int numA = 0, numB = 0, a = 1, b = 1;
for (int i = 0; i < nDA; i++) {
numA += a * DA;
a *= 10;
}
for (int i = 0; i < nDB; i++) {
numB += b * DB;
b *= 10;
}
cout << numA + numB;
return 0;
}
题目传送门:A除以B
#include
#include
using namespace std;
const int MAXN = 1000 + 5;
int main() {
string str;
int b;
cin >> str >> b;
if (str[0] - 48 < b&&str.size() == 1) cout << 0 << " " << str[0];//比如3/8这种情况单独处理
else {
int ans[MAXN] = {
0 }, k = 0, temp = 0;
for (int i = 0; i < str.size(); i++) {
int cnt = temp * 10 + str[i] - 48;
ans[k++] = cnt / b;
temp = cnt % b;
}
if (ans[0] == 0)//首位是0不能输出
for (int i = 1; i < k; i++)
cout << ans[i];
else
for (int i = 0; i < k; i++)
cout << ans[i];
cout << " " << temp;
}
return 0;
}
题目传送门:个位数统计
#include
#include
using namespace std;
int main(){
string str;
cin >> str;
int temp[10] = {
0 };
for (int i = 0; i < str.size(); i++)
temp[(str[i] - '0')]++;
for (int i = 0; i <10; i++){
if (temp[i] == 0) continue;
else cout << i << ":" << temp[i] << endl;
}
return 0;
}
题目传送门:组个最小数
#include
using namespace std;
int main(){
int a[10], s = 0;
for (int i = 0; i < 10; i++){
cin >> a[i];
s += a[i];
}
int *b = new int[s];
int f = 0;
for (int i = 1; i < 10; i++){
if (a[i] != 0){
b[f++] = i;
a[i]--;
break;
}
}
for (int i = 0; i < 10; i++){
while (1){
if (a[i] != 0){
b[f++] = i;
a[i]--;
}
else break;
}
}
for (int i = 0; i < f; i++)
cout << b[i];
return 0;
}
题目传送门:程序运行时间
#include
using namespace std;
int main() {
int c1, c2, hh, mm, ss;
cin >> c1 >> c2;
int temp = (c2 - c1 + 50) / 100;//四舍五入,易错
hh = temp / 3600, mm = temp % 3600 / 60, ss = temp % 3600 % 60;
if (hh < 10) cout << "0";
cout << hh << ":";
if (mm < 10) cout << "0";
cout << mm << ":";
if (ss < 10) cout << "0";
cout << ss;
return 0;
}
题目传送门:打印沙漏
#include
int main() {
int n;
char ch;
scanf("%d %c", &n, &ch);
int m = 1;
while((m + 1) * (m + 1) / 2 - 1 <= n)
m += 2;
m -= 2;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m - i || j < i + 1; ++j) {
if (j >= i || j >= m - 1 - i)
putchar(ch);
else
putchar(' ');
}
putchar('\n');
}
printf("%d", n - (m + 1) * (m + 1) / 2 + 1);
return 0;
}
题目传送门:完美数列
#include
#include
#include
#include
using namespace std;
bool cmp(const int &a, const int &b){
return a > b;
}
int main(){
long long N, p;
cin >> N >> p;
vector<long long>v;
for (int i = 0; i < N; i++){
long long x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end(), cmp);
long long M = v[0];
int len1=v.size();
for(int i=0;i<len1;i++){
long long m = v[len1-1];
if (sqrt(M) <= sqrt(m)*sqrt(p)) break;
else len1--;
}
sort(v.begin(), v.end());
long long m = v[0];
int len2=v.size();
for(int i=0;i<len2;i++){
long long M = v[len2-1];
if (sqrt(M) <= sqrt(m)*sqrt(p)) break;
else len2--;
}
cout<<(len1>len2?len1:len2);//输出较大的一个
return 0;
}
题目传送门:查验身份证
#include
#include
#include
using namespace std;
int main() {
int weigth[] = {
7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
int n, count = 0;
cin >> n;
getchar();
string *str = new string[n];
for (int i = 0; i < n; i++)
getline(cin, str[i]);
for (int i = 0; i < n; i++) {
int Sum_weigth = 0;
char M;
for (int j = 0; j < 17; j++)
Sum_weigth = Sum_weigth + (str[i][j] - 48)*weigth[j];
int Z = Sum_weigth % 11;
switch (Z) {
case 0:
M = '1'; break;
case 1:
M = '0'; break;
case 2:
M = 'X'; break;
case 3:
M = '9'; break;
case 4:
M = '8'; break;
case 5:
M = '7'; break;
case 6:
M = '6'; break;
case 7:
M = '5'; break;
case 8:
M = '4'; break;
case 9:
M = '3'; break;
default:
M = '2'; break;
}
if (str[i][17] != M) cout << str[i] << endl;
else count++;
}
if (count == n) cout << "All passed";
return 0;
}
题目传送门:挖掘机技术哪家强
#include
#include
using namespace std;
const int MAXN = 100000 + 5;
struct School {
int num;
int score;
};
void Initate(School s[]) {
for (int i = 0; i < MAXN; i++) {
s[i].num = 0;
s[i].score = 0;
}
}
int main() {
int n, a, b;
cin >> n;
School s[MAXN];
Initate(s);
while (n--) {
cin >> a >> b;
s[a].score += b;
}
int max = s[0].score, t = 0;
for (int i = 0; i < MAXN; i++) {
if (s[i].score > max) {
max = s[i].score;
t = i;
}
}
cout << t << " " << max << endl;
return 0;
}
题目传送门: 跟奥巴马一起编程
#include
using namespace std;
int main() {
int n;
char c;
cin >> n >> c;
for (int i = 0; i < (n + 1) / 2; i++) {
if (i == 0 || i == (n + 1) / 2 - 1)
for (int j = 0; j < n; j++)
cout << c;
else {
cout << c;
for (int j = 1; j < n - 1; j++)
cout << " ";
cout << c;
}
cout << endl;
}
return 0;
}
题目传送门: 在霍格沃茨找零钱
#include
using namespace std;
int main() {
int Galleon1, Sickle1, Knut1, Galleon2, Sickle2, Knut2;
char c;
cin >> Galleon1 >> c >> Sickle1 >> c >> Knut1 >> Galleon2 >> c >> Sickle2 >> c >> Knut2;
int sum1 = Galleon1 * 17 * 29 + Sickle1 * 29 + Knut1;
int sum2 = Galleon2 * 17 * 29 + Sickle2 * 29 + Knut2;
int sum = sum2 - sum1;
bool flag = false;
if (sum < 0) {
flag = true;
sum = -sum;
}
if (flag) {
cout << "-";
}
cout << sum / 29 / 17 << "." << sum / 29 % 17 << "." << sum % 29 << endl;
return 0;
}
题目传送门:统计同成绩学生
//(C语言版答案正确):
#include
int main(){
int b[101]={
0};
int n,m;
scanf("%d",&n);
for(int i=0;i<n;i++){
int tmp=0;
scanf("%d",&tmp);
b[tmp]++;
}
scanf("%d",&m);
for(int i=0;i<m;i++){
int cnt;
scanf("%d",&cnt);
printf("%d",b[cnt]);
if(i!=m-1) printf(" ");
}
return 0;
}
//(C++版运行超时):
#include
using namespace std;
int main(){
int b[101]={
0};
int n,m;
cin>>n;
for(int i=0;i<n;i++){
int tmp=0;
cin>>tmp;
b[tmp]++;
}
cin>>m;
for(int i=0;i<m;i++){
int cnt;
cin>>cnt;
cout<<b[cnt];
if(i!=m-1) cout<<" ";
}
return 0;
}
题目传送门:到底买不买
/*
*将字符串中的单个字符变为int值,用数组ans的下表保存。
*/
#include
#include
using namespace std;
const int MAXN = 200;
int main() {
string str1, str2;
cin >> str1 >> str2;
int ans[MAXN] = {
0 };
for (int i = 0; i < str1.size(); i++)
ans[str1[i] - 48]++;
int count = 0, cnt = 0;
for (int i = 0; i < str2.size(); i++) {
int j = 0;
for (; j < str1.size(); j++) {
if (ans[str1[j] - 48] != 0 && str2[i] == str1[j]) {
ans[str1[j] - 48]--;
count++;
break;//容易忘记
}
}
if (j == str1.size()) cnt++;
}
if (count == str2.size()) cout << "Yes" << " " << str1.size() - str2.size();
else cout << "No" << " " << cnt;
return 0;
}
题目传送门:有几个PAT
#include
#include
using namespace std;
int main(){
string str;
getline(cin,str);
int m=str.size();
int nT=0,nAT=0,nPAT=0;
while(m>0){
m--;
if(str[m]=='T') nT++;
else if(str[m]=='A') nAT=(nAT+nT)%1000000007;
else nPAT=(nPAT+nAT)%1000000007;
}
cout<<nPAT<<endl;
return 0;
}
题目传送门:考试座位号
#include
#include
using namespace std;
struct Infor{
string str;
int num1;
int num2;
};
void InitInfor(Infor infor[],int n){
for(int i=0;i<n;i++){
infor[i].str="";
infor[i].num1=-1;
infor[i].num2=-1;
}
}
int main(){
int n,M,temp;
cin>>n;
Infor infor[n];
InitInfor(infor,n);
for(int i=0;i<n;i++){
cin>>infor[i].str>>infor[i].num1>>infor[i].num2;
}
cin>>M;
while(M--){
cin>>temp;
for(int i=0;i<n;i++){
if(temp==infor[i].num1) cout<<infor[i].str<<" "<<infor[i].num2<<endl;
}
}
return 0;
}
题目传送门:字符统计
/*
*注意本题目只是统计英文字符(大写+小写)
*如果有并列,输出按字母序最小的那个字母,因此找最值的循环比较中没有'等',就能保证输出最小的那个英文字符
*思路是将字符的ASCII值作为数组下标,数组的值作为他的次数,最后在数组中寻找最大值即可!
*/
#include
#include
using namespace std;
const int MAXN = 1000 + 5;
int main() {
string str;
getline(cin, str);
int ans[MAXN] = {
0 };
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'a'&&str[i] <= 'z')
ans[str[i] - 48]++;
if (str[i] >= 'A'&&str[i] <= 'Z')//大写需要转化为小写,以便于后面输出
ans[str[i] - 48 + 32]++;
}
int max = ans[0], temp = 0;
for (int i = 0; i < MAXN; i++) {
if (ans[i] > max) {
max = ans[i];
temp = i;
}
}
char c = temp + 48;
cout << c << " " << max;
return 0;
}
题目传送门:输出PATest
/*
*将PATest对应字符的个数存在数组ans中,顺序输出即可,当字符个数为0时,不输出。
*/
#include
#include
using namespace std;
int main() {
string str;
getline(cin, str);
int ans[6] = {
0 }, count = 0;
for (int i = 0; i < str.size(); i++) {
switch (str[i]) {
case 'P':
ans[0]++;
count++;
break;
case 'A':
ans[1]++;
count++;
break;
case 'T':
ans[2]++;
count++;
break;
case 'e':
ans[3]++;
count++;
break;
case 's':
ans[4]++;
count++;
break;
case 't':
ans[5]++;
count++;
break;
}
}
int cnt = 0;
while (true) {
cnt++;
if (ans[0] != 0) {
ans[0]--;
cout << 'P';
}
if (ans[1] != 0) {
ans[1]--;
cout << 'A';
}
if (ans[2] != 0) {
ans[2]--;
cout << 'T';
}
if (ans[3] != 0) {
ans[3]--;
cout << 'e';
}
if (ans[4] != 0) {
ans[4]--;
cout << 's';
}
if (ans[5] != 0) {
ans[5]--;
cout << 't';
}
if (cnt == count) break;
}
return 0;
}
题目传送门: 划拳
#include
using namespace std;
int main() {
int n, i;
cin >> n;
int a, b, c, d, sum_A = 0, sum_B = 0;
while (n--) {
cin >> a >> b >> c >> d;
if (a + c == b && a + c != d) sum_B++;
if (a + c != b && a + c == d) sum_A++;
}
cout << sum_A << " " << sum_B << endl;
return 0;
}
题目传送门:编程团体赛
/*
*不要把本题的输入格式想成字符串的处理,这样本题就复杂了。
* 在输入 "队伍编号-队员编号 成绩" 中的 '-队员编号' 可以看成一个负的整数,
*所以可以看成由3个整型变量构成,因此定义了结构体,但是由于队员编号在本题目中
*没有用,所以不定义都行
*/
#include
#include
using namespace std;
const int MAXN = 1000 + 5;
struct Group {
int team_number;
int score;
};
void Initate(Group obj[]) {
for (int i = 0; i < MAXN; i++) {
obj[i].team_number = 0;
obj[i].score = 0;
}
}
int main() {
int n;
cin >> n;
Group obj[MAXN];
Initate(obj);
for (int i = 0; i < n; i++) {
int temp1, temp2, temp3;
cin >> temp1 >> temp2 >> temp3;
obj[temp1].score += temp3;
}
int max = obj[0].score, cnt = 0;
for (int i = 0; i < MAXN; i++) {
if (obj[i].score > max) {
max = obj[i].score;
cnt = i;
}
}
cout << cnt << " " << max;
return 0;
}
题目传送门: 数字加密
#include
#include
using namespace std;
int main() {
string s1, s2;
cin >> s1 >> s2;
int l1 = s1.length(), l2 = s2.length();
int l3 = l1 > l2 ? l1 : l2;
char *A = new char[l3];
char *B = new char[l3];
for (int i = 0; i < l3; i++) {
A[i] = '0';
B[i] = '0';
}
int k1 = 0;
for (int i = l1-1; i >= 0; i--) {
A[k1++] = s1[i];
}
int k2 = 0;
for (int i = l2 - 1; i >= 0; i--) {
B[k2++] = s2[i];
}
char *ans = new char[l3];
for (int i = 0; i < l3; i++) {
if (i % 2 == 0) {
int anwser = ((A[i] - '0') + (B[i] - '0')) % 13;
if (anwser == 10) ans[i] = 'J';
else if (anwser == 11) ans[i] = 'Q';
else if (anwser == 12) ans[i] = 'K';
else ans[i] = anwser + '0';
}
else {
int anwser = ((B[i] - '0') - (A[i] - '0'));
if (anwser < 0) anwser += 10;
ans[i] = anwser + '0';
}
}
for (int i = l3-1; i >=0; i--)
cout << ans[i];
return 0;
}
题目传送门:数列的片段和
/*
*本题找规律:计算每个数出现的次数,如果用暴力求解,会超时。
*sum += cnt * (i + 1)*(n - i)中的double型的cnt应该放在最前,因为(i + 1)*(n - i)属于int型,会超出int的范围。
*浮点数相乘的顺序可能会影响精度
*/
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
double cnt = 0.0, sum = 0.0;
for (int i = 0; i < n; i++) {
cin >> cnt;
sum += cnt * (i + 1)*(n - i);
}
cout << setiosflags(ios::fixed) << setprecision(2) << sum;
return 0;
}
题目传送门: 复数乘法
/*
对于本题,有一个坑:当输出的A或B小于0.005时,应该为0
cout << setiosflags(ios::fixed) << setprecision(2) << ???这个在保留的时候会自动四舍五入,如A为0.006时,如果不用上述条件判断,此格式输出就是0.01,而正确答案应该为0.00
*/
#include
#include
#include
#include
using namespace std;
int main() {
double R1, P1, R2, P2, ans_A, ans_B;
cin >> R1 >> P1 >> R2 >> P2;
ans_A = R1 * R2*cos(P1)*cos(P2) - R1 * R2*sin(P1)*sin(P2);
ans_B = R1 * cos(P1)*R2*sin(P2) + R1 * sin(P1)*R2*cos(P2);
if (abs(ans_A) < 0.005) ans_A = 0;
cout << setiosflags(ios::fixed) << setprecision(2) << ans_A;
if (abs(ans_B) < 0.005) ans_B = 0;
if (ans_B >= 0) cout << "+";
cout << setiosflags(ios::fixed) << setprecision(2) << ans_B;
cout << "i";
return 0;
}
题目传送门:组合数的和
#include
using namespace std;
int main() {
int n,sum=0;
cin>>n;
int *p=new int[n];
for(int i=0;i<n;i++)
cin>>p[i];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j) continue;
sum+=(10*p[j]+p[i]);
}
}
cout<<sum;
return 0;
}
题目传送门: 数零壹
#include
#include
using namespace std;
int main() {
string str;
getline(cin, str);
int sum = 0, n_0 = 0, n_1 = 0;
for (int i = 0; i < str.size(); i++) {
if ((str[i] >= 'a'&&str[i] <= 'z') || (str[i] >= 'A'&&str[i] <= 'Z')) {
if (str[i] >= 'a'&&str[i] <= 'z') sum += (str[i] - 'a' + 1);
if (str[i] >= 'A'&&str[i] <= 'Z') sum += (str[i] - 'A' + 1);
}
}
while (sum) {
if (sum % 2 == 0) n_0++;
else n_1++;
sum /= 2;
}
cout << n_0 << " " << n_1;
return 0;
}
题目传送门: C语言竞赛
#include
using namespace std;
const int maxn = 10000 + 5;
struct Temp {
bool value;
bool visited;
int num;
};
void Inite(Temp t[]) {
for (int i = 0; i < maxn; i++) {
t[i].num = 0;
t[i].value = false;
t[i].visited = false;
}
}
bool is_prime(int n) {
if (n < 2) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n%i == 0) return false;
}
return true;
}
int main() {
int n, temp, k, cnt = 2;
cin >> n;
Temp t[maxn];
Inite(t);
bool flag = false;
for (int i = 0; i < n; i++) {
cin >> temp;
if (!flag) {
t[temp].value = true;
t[temp].num = 1;
flag = true;
}
else {
t[temp].value = true;
t[temp].num = cnt++;
}
}
cin >> k;
while (k--) {
cin >> temp;
if (t[temp].value) {
if (t[temp].visited == true) {
cout << setw(4) << setfill('0') << temp << ": " << "Checked" << endl;
}
else {
if (t[temp].num == 1) {
cout << setw(4) << setfill('0') << temp << ": " << "Mystery Award" << endl;
t[temp].visited = true;
}
else if (is_prime(t[temp].num)) {
cout << setw(4) << setfill('0') << temp << ": " << "Minion" << endl;
t[temp].visited = true;
}
else {
cout << setw(4) << setfill('0') << temp << ": " << "Chocolate" << endl;
t[temp].visited = true;
}
}
}
else
cout << setw(4) << setfill('0') << temp << ": " << "Are you kidding?" << endl;
}
return 0;
}
题目传送门: 判断题
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int *score_sum = new int[m];
for (int i = 0; i < m; i++)
cin >> score_sum[i];//每道题的满分值
int *rigth_answer = new int[m];
for (int i = 0; i < m; i++)
cin >> rigth_answer[i];//每道题对应的正确答案
for (int i = 0; i < n; i++) {
int *answer = new int[m];
for (int i = 0; i < m; i++)
cin >> answer[i];//学生的解答
int sum = 0;
for (int i = 0; i < m; i++) {
if (rigth_answer[i] == answer[i])
sum += score_sum[i];
}
cout << sum << endl;
}
return 0;
}
题目传送门:计算谱半径
#include
#include
#include
#include
#include
using namespace std;
int main() {
int n;
float num1, num2, max;
cin >> n;
float *ans = new float[n];
for (int i = 0; i < n; i++) {
cin >> num1 >> num2;
ans[i] = sqrt(num1*num1 + num2 * num2);
}
sort(ans, ans + n);
cout << setiosflags(ios::fixed) << setprecision(2) << ans[n - 1] << endl;
return 0;
}
题目传送门: 朋友数
/*
*本题采用集合是最好的选择,不用判重
*/
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
set<int>ans;
for (int i = 0; i < n; i++) {
int sum = 0, temp;
cin >> temp;
while (temp) {
sum += temp % 10;
temp /= 10;
}
ans.insert(sum);
}
cout << ans.size() << endl;
set<int>::iterator it = ans.begin();
int cnt = 0;//采用cnt变量是为了让输出的最后一个数后面没有空格
while (true) {
cout << *it;
cnt++;
if (cnt != ans.size()) {
cout << " ";
it++;
}
else break;
}
return 0;
}
题目传送门: 单身狗
#include
#include
#include
#include
#include
using namespace std;
int main() {
set<int>s, ans;
map<int, int>Wife;
int n, num1, num2, M;
cin >> n;
while (n--) {
cin >> num1 >> num2;
Wife[num1] = num2;
Wife[num2] = num1;
}
cin >> M;
int *p = new int[M];
for (int i = 0; i < M; i++) {
cin >> p[i];
s.insert(p[i]);
}
for (int i = 0; i < M; i++) {
if (s.find(Wife[p[i]]) == s.end()) {
//Wife[p[i]]代表其配偶,在集合中没找到
ans.insert(p[i]);//放到新的集合中
}
}
cout << ans.size() << endl;
set<int>::iterator it;
for (it = ans.begin(); it != ans.end(); it++) {
if (it != ans.begin())
cout << " ";
cout << setw(5) << setfill('0') << *it;//注意不足5位补0
}
return 0;
}
题目传送门:试密码
//本题有一个坑需要注意:错误密码可以包含空格等,所以应该改用getline而不是cin进行输入
#include
#include
#include
using namespace std;
int main() {
string password, str;
int n;
cin >> password >> n;
getchar();
while (true) {
getline(cin, str);
if (str == "#") break;
if (str != password) {
cout << "Wrong password: " << str << endl;
n--;
}
else {
cout << "Welcome in" << endl;
break;
}
if (n == 0) {
cout << "Account locked" << endl;
break;
}
}
return 0;
}
题目传送门:结绳
/*
本题主要考察是算法的重要性:
每次取最小的两段进行对折、拼接
*/
#include
#include
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
int n, sum = 0;
cin >> n;
int *p = new int[n];
for (int i = 0; i < n; i++) {
cin >> p[i];
}
sort(p, p + n, cmp);
while (n >= 2) {
sum = (p[n - 1] + p[n - 2]) / 2;
p[n - 2] = sum;
n--;
}
cout << sum << endl;
return 0;
}
题目传送门:小赌怡情
#include
using namespace std;
int main() {
int T, K, n1, b, t, n2;
cin >> T >> K;
while (K--) {
cin >> n1 >> b >> t >> n2;
if (T == 0) {
cout << "Game Over." << endl;
break;
}
if (t > T) cout << "Not enough tokens. Total = " << T << "." << endl;
else {
if (b == 0 && n2<n1 || b == 1 && n2>n1) {
T += t;
cout << "Win " << t << "! Total = " << T << "." << endl;
}
else {
T -= t;
cout << "Lose " << t << ". Total = " << T << "." << endl;
}
}
}
return 0;
}
题目传送门: 开学寄语
#include
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int sum_row = 0, sum = 0;
string *need_find = new string[m];
for (int i = 0; i < m; i++)
cin >> need_find[i];
for (int i = 0; i < n; i++) {
string name;
int num;
cin >> name >> num;
string *flag_find = new string[num];
for (int i = 0; i < num; i++)
cin >> flag_find[i];
bool flag = false;
string *ans = new string[num];
int k = 0;
for (int i = 0; i < num; i++) {
for (int j = 0; j < m; j++) {
if (flag_find[i] == need_find[j]) {
flag = true;
ans[k++] = flag_find[i];
sum++;
}
}
}
if (flag) {
cout << name << ":";
sum_row++;
for (int i = 0; i < k; i++) {
cout << " " << ans[i];//此处格式的控制容易错(行末不得有多余空格)
}
cout << endl;
}
}
cout << sum_row << " " << sum << endl;
return 0;
}
题目传送门: Wifi密码
#include
using namespace std;
int main() {
int n, i;
cin >> n;
vector<int>ans;
getchar();
string *str = new string[n];
for (int i = 0; i < n; i++)
getline(cin, str[i]);
for (int i = 0; i < n; i++) {
int j = 0;
for (; j < str[i].size(); j++) {
if (str[i][j] == 'T') break;
}
switch (str[i][j - 2]) {
case 'A':
ans.push_back(1);
break;
case 'B':
ans.push_back(2);
break;
case 'C':
ans.push_back(3);
break;
default:
ans.push_back(4);
break;
}
}
vector<int>::iterator it = ans.begin();
for (; it != ans.end(); it++)
cout << *it;
return 0;
}
题目传送门: 互评成绩计算
#include
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
double G2;//定义为double便于后面计算final_score
cin >> G2;
vector<int>v;
for (int i = 0; i < n - 1; i++) {
int temp;
cin >> temp;
v.push_back(temp);
}
for (int i = 0; i < v.size(); i++) {
if (v[i]<0 || v[i]>m) {
v.erase(v.begin() + i);
i--;//保证删除后迭代器可以指向删除前的位置
}
}
int max = *v.begin(), min = *v.begin(), sum = 0;
for (int i = 0; i < v.size(); i++) {
if (max < v[i]) max = v[i];
if (min > v[i]) min = v[i];
sum += v[i];
}
double G1 = (double)(sum - max - min) / (v.size() - 2);//注意求平均时要减去最高分,最低分的两个组
int final_score = (G1 + G2) / 2 + 0.5;//加0.5保证四舍五入
cout << final_score << endl;
}
return 0;
}
题目传送门: 字符串压缩与解压
#include
#include //getchar所需的头文件
#include
using namespace std;
int main() {
char select;
cin >> select;
getchar();//读入回车键
string str;
getline(cin, str);
if (select == 'C') {
int sum = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i + 1] == str[i]) sum++;
else {
if (sum == 0) cout << str[i];
else {
cout << sum + 1 << str[i];
}
sum = 0;
}
}
}
else {
for (int i = 0; i < str.size(); i++) {
int sum = 0;
while (1) {
if (str[i] >= '0'&&str[i] <= '9')
sum = sum * 10 + str[i++] - '0';//单个字符的个数可能不是一位数
else break;
}
if (sum == 0) cout << str[i];
for (int j = 0; j < sum; j++) {
cout << str[i];
}
}
}
return 0;
}
题目传送门:检查密码
#include
using namespace std;
int main() {
int n;
cin >> n;
getchar();
string str;
while (n--) {
getline(cin, str);
int s_num = 0, s_zimu = 0, s_dot = 0, s_blank=0, s_else = 0;
if (str.size() < 6) cout << "Your password is tai duan le." << endl;
else {
for (int i = 0; i < str.size(); i++) {
if ((str[i] >= 'a'&&str[i] <= 'z') || (str[i] >= 'A'&&str[i] <= 'Z'))
s_zimu++;
else if (str[i] >= '0'&&str[i] <= '9')
s_num++;
else if (str[i] == '.')
s_dot++;
else
s_else++;
}
if (s_else != 0) cout << "Your password is tai luan le." << endl;
else {
if (s_zimu == 0) cout << "Your password needs zi mu." << endl;
else if (s_num == 0) cout << "Your password needs shu zi." << endl;
else cout << "Your password is wan mei." << endl;
}
}
}
return 0;
}
题目传送门:射击比赛
#include
#include
#include
using namespace std;
struct Infor {
string num;
int x;
int y;
float score;
};
void IniteInfor(Infor infor[], int n) {
for (int i = 0; i < n; i++) {
infor[i].num = "";
infor[i].x = 0;
infor[i].y = 0;
infor[i].score = 0.0;
}
}
bool cmp(Infor infor1, Infor infor2) {
return infor1.score > infor2.score;
}
int main() {
int n;
cin >> n;
Infor infor[n];
IniteInfor(infor, n);
for (int i = 0; i < n; i++) {
cin >> infor[i].num >> infor[i].x >> infor[i].y;
infor[i].score = sqrt(infor[i].x*infor[i].x + infor[i].y*infor[i].y);
}
sort(infor, infor + n, cmp);
cout << infor[n - 1].num << " " << infor[0].num << endl;
return 0;
}
题目传送门: 是否存在相等的差
#include
using namespace std;
const int MAXN = 10000 + 5;
int main() {
int n;
cin >> n;
int *p = new int[n];
for (int i = 0; i < n; i++) {
cin >> p[i];
}
int ans[MAXN] = {
0 };
for (int i = 0; i < n; i++) {
int temp = p[i] - i - 1;
if (temp < 0) temp = -temp;
ans[temp]++;
}
for (int i = MAXN - 1; i >= 0; i--) //从大到小输出
if (ans[i] > 1) cout << i << " " << ans[i] << endl;
return 0;
}
题目传送门:就不告诉你
/*
本题需要注意两种情况:
200*5得1000,输出的结果是1,你是0001;
101*2得202,中间得0需要输出。
*/
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int mul = a * b;
int ans[10], k = 0;
if (mul == 0)
cout << "0";
while (mul) {
ans[k++] = mul % 10;
mul /= 10;
}
bool flag = true;
if (ans[0] == 0) flag = false;
if (flag) {
for (int i = 0; i < k; i++) {
cout << ans[i];
}
}
else {
int i = 1;
for (; i < k; i++) {
if (ans[i] != 0) {
flag = true;
break;
}
}
for (int j = i; j < k; j++) {
cout << ans[j];
}
}
return 0;
}
题目传送门: 有多少不同的值
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
set<int>ans;
for (int i = 1; i <= n; i++) {
int temp = i / 2 + i / 3 + i / 5;
ans.insert(temp);
}
cout << ans.size() << endl;
return 0;
}
题目传送门:N-自守数
#include
using namespace std;
int getEveryNum(int n, int p[], int k) {
//每次都要对数字进行初始化操作
for (int i = 0; i < 10; i++) {
p[i] = 0;
}
while (n) {
p[k++] = n % 10;
n /= 10;
}
return k;
}
int main() {
int M, num, temp, p1[10] = {
0 }, p2[10] = {
0 };
cin >> M;
while (M--) {
cin >> num;
int k1 = getEveryNum(num, p1, 0);//k1表示num的位数长度
bool F = false;
for (int i = 1; i < 10; i++) {
temp = i * num*num;
int k2 = getEveryNum(temp, p2, 0);//k2表示temp的位数长度
bool flag = false;
for (int j = 0; j < k1; j++) {
if (p1[j] != p2[j]) {
flag = true;
break;
}
}
if (!flag) {
cout << i << " " << temp << endl;
F = true;
break;
}
}
if (!F) {
cout << "No" << endl;
}
}
return 0;
}
题目传送门:最好吃的月饼
#include
using namespace std;
int main() {
int N, M, num;
cin >> N >> M;
int *p = new int[N];
for (int i = 0; i < N; i++)
p[i] = 0;
while (M--) {
for (int i = 0; i < N; i++) {
cin >> num;
p[i] += num;
}
}
int max = p[0];
for (int i = 0; i < N; i++) {
if (p[i] > max) max = p[i];
}
cout << max << endl;
bool flag = false;
for (int i = 0; i < N; i++) {
if (max == p[i]) {
if (!flag) flag = true;
else cout << " ";
cout << i + 1;
}
}
return 0;
}
题目传送门: 字符串A+B
#include
#include
#include
#include
using namespace std;
int main() {
string A, B, C;
getline(cin, A);
getline(cin, B);
C = A + B;
int *str = new int[200];
for (int i = 0; i < 200; i++) {
str[i] = 0;
}
for (int i = 0; i < C.size(); i++) {
if (str[C[i]] == 0) {
cout << C[i];
str[C[i]] = 1;
}
}
return 0;
}
持续更新中......
码字不易,您的
支持
就是我坚持
下去的动力,一起加油哦。