1、下面主函数调用函数SortString()按奥运会参赛国国名在字典中的顺序对其入场次序进行排序
#include#include #define M 150 void SortString(char *ptr[], int n); int main() { int i, n; char **pStr; printf("How many countries?"); scanf("%d",&n); getchar(); printf("Input their names:n"); pStr = (char*)malloc(sizeof(char) * n); for (i=0; i 运行结果:
2、有 10 个学生,每个学生的数据包括学号(num,6个字符)、姓名(name,8个字符),三门课程的成绩( score[3】,实型)及其平均分(avr,实型)。首先输入这 10 个学生的数据,然后再计算每个学生 3 门课程的平均分,存回 avr中,并输出第 6号学生的所有信息。#includestruct stuinfo { char stu_num[6]; char name[8]; int score[3]; int avr; }; int main() { stuinfo s[10]; int i; for(i=0;i<10;i++) { scanf("%s",s[i].stu_num); scanf("%s",s[i].name); scanf("%d",&s[i].score[0]); scanf("%d",&s[i].score[1]); scanf("%d",&s[i].score[2]); s[i].avr=(s[i].score[0]+s[i].score[1]+s[i].score[2])/3; } printf("stu_num%sn",s[5].stu_num); printf("name%sn",s[5].name); printf("score1:%dn",s[5].score[0]); printf("score2:%dn",s[5].score[1]); printf("score3:%dn",s[5].score[2]); printf("avr:%dn",s[5].avr); } 3、有一个仅包含’a’和’b’两种字符的字符串s,长度为n,每次操作可以把一个字符做一次转换(把一个’a’设置为’b’,或者把一个’b’置成’a’);但是操作的次数有上限m,问在有限的操作数范围内,能够得到最大连续的相同字符的子串的长度是多少。
输入描述:
第一行两个整数 n , m (1<=m<=n<=50000),第二行为长度为n且只包含’a’和’b’的字符串s。
输出描述:
输出在操作次数不超过 m 的情况下,能够得到的 最大连续 全’a’子串或全’b’子串的长度。
示例1
输入:
8 1
aabaabaa
输出:
5
说明:
把第一个 ‘b’ 或者第二个 ‘b’ 置成 ‘a’,可得到长度为 5 的全 ‘a’ 子串。// test48.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include#include #include using namespace std; int GetLongStr(string str, int num, int count) { int res = -1; int left = 0, right = 0; int tmp1 = 0, tmp2 = 0; for (right = 0; right < str.size(); right++) { if (str[right] == 'a') { tmp1++; } else { tmp2++; } if (tmp1 > count && tmp2 > count) { res = max(res, right - left); if (str[left] == 'a') { tmp1--; } else { tmp2--; } left++; } } return res; } int main() { int num, count; cin >> num >> count; string str_in; cin >> str_in; cout << GetLongStr(str_in, num, count); return 0; } 运行结果:



