1 输入一串字符串,其中有普通的字符与括号组成(包括’(’、’)’、’[',']‘),要求验证括号是否匹配,如果匹配则输出0、否则输出1.
#include <iostream> using namespace std; int verify(char* ch); void main() { char* ch = "((([])))[]"; cout<<verify(ch)<<endl; } int verify(char* ch) { char marker[100] = {0}; while (*ch) { if (*ch == '(' || *ch == '[') { marker[strlen(marker)] = *ch; } else if (*ch == ')') { if (marker[strlen(marker)-1] == '(') { marker[strlen(marker)-1] = '\0'; } else { return 1; } } else if (*ch == ']') { if (marker[strlen(marker)-1] == '[') { marker[strlen(marker)-1] = '\0'; } else { return 1; } } ch++; } if (strlen(marker) == 0) { return 0; } else { return 1; } }
2 输入一行数字:123 423 5645 875 186523
在输入第二行:23
将第一行中含有第二行中”23″的数输出并排序
结果即:123 423 186523
#include <iostream> using namespace std; void main() { int num[20] = {0}; int sort[20] = {0}; int n; char c = ' '; int i = 0; int temp; while (c != '\n') { scanf("%d%c", &temp, &c); num[i++] = temp; } scanf("%d", &n); int index = 0; for (int j = 0; j < i; j++) { temp = num[j]; while (temp >= n) { if (temp % 100 == n) { sort[index++] = num[j]; break; } else { temp /= 10; } } } for (int k = 0; k < index; k++) { cout<<sort[k]<<endl; } }
3 输入m个字符串 和一个整数n, 把字符串M化成以N为单位的段,不足的位数用0补齐。
如 n=8 m=9 ,
123456789划分为:12345678
90000000
123化为:12300000
#include <iostream> using namespace std; void main() { char ch[20]; cin>>ch; int n; cin>>n; char temp[20] = {0}; char* word[10]; int index = 0; for (int i = 0; i < strlen(ch); i += n) { memcpy(temp,ch+i,n); if (strlen(ch) - i < n) { int k = 0; while (temp[k] != '\0') { k++; } while (k < n) { temp[k++] = '0'; } } cout<<temp<<endl; } }
4 将电话号码 one two 。。。nine zero翻译成1 2 。。9 0,中间会有double
例如
输入:OneTwoThree 输出:123
输入:OneTwoDoubleTwo 输出:1222
输入:1Two2 输出:ERROR
输入:DoubleDoubleTwo 输出:ERROR
有空格,非法字符,两个Double相连,Double位于最后一个单词都错误
#include <iostream> using namespace std; void main() { char num[10][6] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; char phone[7]; char c = ' '; char result[10] = {0}; int countDouble = 0; int index = 0; while (c != '\n') { scanf("%s%c",&phone,&c); for (int i = 0; i < 10; i++) { if (!strcmp(phone,num[i]) && i < 10) { result[index++] = i + '0'; if (countDouble == 1) { result[index++] = i + '0'; --countDouble; } break; } else if (!strcmp(phone,"Double")) { ++countDouble; if (countDouble > 1) { cout<<"Error!"<<endl; return; } break; } else { cout<<"Error!"<<endl; return; } } } if (countDouble != 0) { cout<<"Error!"<<endl; return; } cout<<result<<endl; }
5 将整数倒序输出,删除重复数据
输入一个整数,如12336544,或1750,然后从最后一位开始倒过来输出,最后如果是0,则不输出,输出的数字是不带重复数字的,所以上面的输出是456321和571。如果是负数,比如输入-175,输出-571。
#include <iostream> using namespace std; void numreverse(char* num); void removezero(char* num); void removereplicate(char* num); void removereplicate2(char* num); void main() { char num[20] = "-122001100"; removezero(num); numreverse(num); removereplicate2(num); cout<<num<<endl; } void numreverse(char* num) { char c; int i = 0; char* temp = num;; if (num[0] == '-') { temp++; } int len = strlen(temp); for (; i < len / 2; i++) { c = temp[i]; temp[i] = temp[len-i-1]; temp[len-i-1] = c; } } void removezero(char* num) { char* temp = num + strlen(num) - 1; while (*temp == '0') { *temp = '\0'; temp--; } } //去掉相邻的重复字符 void removereplicate(char* num) { char* temp = num; int index = 0; for (int i = 0; i < strlen(num); i++) { if (temp[i] != temp[i+1]) { num[index++] = temp[i]; } } num[index] = '\0'; } //去掉所有的重复字符 void removereplicate2(char* num) { char* temp = num; int index = 0; bool exist[10] = {0}; if (temp[0] == '-') { num[index++] = '-'; } for (int i = 1; i < strlen(num); i++) { if (exist[temp[i] - '0'] == 0) { num[index++] = temp[i]; exist[temp[i]-'0'] = 1; } } num[index] = '\0'; }
6 编程的时候,if条件里面的”(“、”)”括号经常出现不匹配的情况导致编译不过,请编写程序检测输入一行if语句中的圆括号是否匹配正确。同时输出语句中出现的左括号和右括号数量,如if((a==1)&&(b==1))是正确的,而if((a==1))&&(b==1))是错误的。注意if语句的最外面至少有一对括号。提示:用堆栈来做。
输入:if((a==1)&&(b==1))
输出:RIGTH 3 3
输入:if((a==1))&&(b==1))
输出:WRONG 3 4
#include <iostream> using namespace std; void main() { char ch[100]; cin.getline(ch,100); int left = 0; int right = 0; int countleft = 0; int countright = 0; bool existif = false; int flag = 0; //看连着if的括号是否过早被匹配 bool bflag = false; int i,j; for (i = 0; i < strlen(ch); i++) { if (ch[i] == 'i' && ch[i+1] == 'f') { existif = true; break; } if (!existif && ch[i] == '(') { break; } } for (int j = 0; j < strlen(ch); j++) { if (ch[j] == '(') { left++; flag++; } else if (ch[j] == ')') { right++; flag--; if (j < strlen(ch) - 1 && flag == 0) { bflag = true; } } } if (!bflag && existif && right == left) { cout<<"RIGHT "<<left<<" "<<right<<endl; } else { cout<<"WRONG "<<left<<" "<<right<<endl; } }
举例:
输入:323324423343
输出:3,6
#include <iostream> using namespace std; void main() { char ch[50]; char* p = ch; cin>>ch; int b[10] = {0}; while (*p) { b[*p++ - '0']++; } int max = b[0]; int index = 0; for (int i = 1; i < 10; i++) { if (b[i] > max) { max = b[i]; index = i; } } cout<<index<<" "<<b[index]<<endl; }
8 字符串首字母转换成大写
举例:
输入:this is a book
返回:This Is A Book
#include <iostream> using namespace std; void main() { char ch[50]; cin.getline(ch,50); if (ch[0] != ' ') { ch[0] -= 32; } for (int i = 0; i < strlen(ch); i++) { if (ch[i] == ' ' && ch[i+1] != ' ') { ch[i+1] -= 32; i++; } } cout<<ch<<endl; }
9 子串分离
题目描述:
通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一个程序,自动分离出各个子串,并使用’,'将其分隔,并且在最后也补充一个’,'并将子串存储。
如果输入”abc def gh i d”,结果将是abc,def,gh,i,d,
要求实现函数:
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
#include <iostream> using namespace std; void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr); void DivideString2(const char *pInputStr, long lInputLen, char *pOutputStr); void DivideString3(const char *pInputStr, long lInputLen, char *pOutputStr); void main() { char ch[50] = "abc def gh i d"; //char ch[50]; //cin.getline(ch,50); char output[50]; DivideString3(ch,strlen(ch),output); cout<<output<<endl; } //使用strtok函数的实现方式 void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr) { char* temp; char* word[10]; char* input = const_cast<char*>(pInputStr); int index = 0; temp = strtok(input," "); while (temp != NULL) { word[index++] = temp; temp = strtok(NULL, " "); } for (int i = 0; i < index; i++) { if (i == 0) { //strncpy(pOutputStr,word[i],sizeof(word[i])+1); strcpy(pOutputStr,word[i]); } else { strcat(pOutputStr,","); strcat(pOutputStr,word[i]); } } strcat(pOutputStr,","); } //不适用strtok函数的实现方式 void DivideString2(const char *pInputStr, long lInputLen, char *pOutputStr) { char* input = const_cast<char*>(pInputStr); char* temp = input; char* word[10]; int index = 0; int length = 0; while (*input) { if (*input != ' ') { length++; input++; } else { if (length != 0) { temp[length] = '\0'; word[index++] = temp; } temp = input+1; length = 0; input++; } } for (int i = 0; i < index; i++) { if (i == 0) { strcpy(pOutputStr,word[i]); } else { strcat(pOutputStr,","); strcat(pOutputStr,word[i]); } } strcat(pOutputStr,","); } //直接法实现 void DivideString3(const char *pInputStr, long lInputLen, char *pOutputStr) { const char* p = pInputStr; int count = 0; while (*p) { if (*p != ' ') { count = 0; *pOutputStr++ = *p++; } else { count++; if (count == 1) { *pOutputStr++ = ','; } p++; } } *pOutputStr++ = ','; *pOutputStr++ = '\0'; }
10 评委打分
问题描述:
在评委打分的比赛中,通常采取去掉一个最高分和最低分,再求平均分的做法,主要是为了公平,公正和公开的原则,防止有人买通评委,有特别的高分出现,另外,也可以防止因为评委个人原因,对选手有“低见”给特别低的分。去掉最高分最低分能够求出较为准确的平均分。请编写程序实现上述计分过程
要求实现函数:
intscore_calc(int n, int score[])
【输入】 int n,评委人数n(n>=3)
int score[],每个评委的打分(百分制)
【输出】 无
【返回】 选手最终得分(取整)
注:
取整和四舍五入不同,取整只保留数值的整数部分,小数部分丢弃。比如7.3和7.6,取整后都为7
示例
输入:int n = 5;int score[] = {75, 80, 75, 70, 80}
输出:无
返回:76
#include <iostream> using namespace std; int score_calc(int n, int score[]); void main() { int n = 5; int score[] = {75, 80, 75, 70, 80}; cout<<score_calc(5,score)<<endl; } int score_calc(int n, int score[]) { int max = score[0]; int min = score[0]; int sum = 0; for (int i = 0; i < n; i++) { if (score[i] > max) { max = score[i]; } else if (score[i] < min) { min = score[i]; } sum += score[i]; } sum = (sum - max - min) / (n - 2); return sum; }