#include <iostream> using namespace std; void main() { char num1[50]; char num2[50]; cin.getline(num1,50); cin.getline(num2,50); int len1 = strlen(num1); int len2 = strlen(num2); int maxlen = len1 > len2 ? len1 : len2; char* sum = new char[maxlen+2]; memset(sum,'0',maxlen+1); sum[maxlen+1] = '\0'; int s,r = 0; int i,j; for (i = len1 - 1, j = len2 - 1; i >=0 && j >= 0; i--,j--) { s = (num1[i] - '0') + (num2[j] - '0') + r; sum[maxlen--] = s % 10 + '0'; r = s / 10; } if (i == j) { sum[maxlen--] = r + '0'; } if (j > 0) { while (j>=0) { s = (num1[j--] - '0') + r; sum[maxlen--] = s % 10 + '0'; r = s / 10; } } else { while (i>=0) { s = (num1[i--] - '0') + r; sum[maxlen--] = s % 10 + '0'; r = s / 10; } } int index = 0; while (sum[index] == '0') { index++; } int nn = 0; char* pOutputStr = new char[sizeof(sum)]; while (sum[index] != '\0') { pOutputStr[nn++] = sum[index++]; } pOutputStr[nn] = '\0'; cout<<pOutputStr<<endl; }
2 一组人(n个),围成一圈,从某人开始数到第三个的人出列,再接着从下一个人开始数,最终输出最终出列的人
#include <iostream> using namespace std; typedef struct Node { int data; struct Node* next; }LinkList; LinkList* createList(int n); void deletefun(LinkList* L, int m); void main() { LinkList* link = createList(10); deletefun(link,3); } LinkList* createList(int n) { LinkList *p,*q,*head; p = new LinkList(); p->data = 1; head = p; for (int i = 1; i < n; i++) { q = new LinkList(); q->data = i + 1; p->next = q; p = q; } p->next = head; return head; } void deletefun(LinkList* L, int m) { LinkList *p,*q,*temp; p = L; while (p->next != p) { for (int i = 1; i < m; i++) { q = p; p = p->next; } cout<<p->data<<endl; temp = p; q->next = p->next; p = p->next; delete temp; temp = NULL; } cout<<"最后的元素:"<<p->data<<endl; }
3 输入一串字符,只包含”0-10″和”,”找出其中最小的数字和最大的数字(可能不止一个),输出最后剩余数字个数。如输入 “3,3,4,5,6,7,7″
#include <iostream> using namespace std; void main() { char ch[50]; gets(ch); int nums[25]; int index = 0; int max = 0; int min = 10; for (int i = 0; i < strlen(ch); i++) { if (ch[i] >= '0' && ch[i] <= '9') { if (ch[i+1] < '0' || ch[i+1] > '9') { nums[index] = ch[i] - '0'; } else { nums[index] = (ch[i] - '0') * 10 + ch[i+1] - '0'; i++; } max = max > nums[index] ? max : nums[index]; min = min < nums[index] ? min : nums[index]; index++; } } int count = index; for (int j = 0; j < index; j++) { if (nums[j] == min || nums[j] == max) { count--; } } cout<<"最大数:"<<max<<endl; cout<<"最小数:"<<min<<endl; cout<<"剩余的个数:"<<count<<endl; }
4 输入一组身高在170到190之间(5个身高),比较身高差,选出身高差最小的两个身高;若身高差相同,选平均身高高的那两个身高;从小到大输出
#include <iostream> using namespace std; void main() { int n = 5; int nums[5]; for (int i = 0; i < 5; i++) { cin>>nums[i]; } int temp; for (int j = 0; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if (nums[j] > nums[k]) { temp = nums[j]; nums[j] = nums[k]; nums[k] = temp; } } } int dmin = nums[1] - nums[0]; int n1 = nums[0]; int n2 = nums[1]; for (int m = 2; m < 4; m++) { if (nums[m] - nums[m-1] <= dmin) { dmin = nums[m] - nums[m-1]; n1 = nums[m-1]; n2 = nums[m]; } } cout<<n1<<endl; cout<<n2<<endl; }
5 删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数
#include <iostream> using namespace std; int delete_sub_str(const char *str,const char *sub_str,char *result); int strcmp2(const char* str1, const char* str2); void main() { char* str = "abdddaddccddd"; char* sub_str = "abd"; char result[20] = {0}; cout<<delete_sub_str(str,sub_str,result)<<endl; cout<<result<<endl; } int delete_sub_str(const char *str,const char *sub_str,char *result) { int count = 0; int len = strlen(sub_str); char* temp = new char[len+1]; memset(temp,0,len+1); while (*str) { memcpy(temp,str,len); if (strcmp2(temp,sub_str) == 0) { count++; str += len; } else { *result++ = *str++; } memset(temp,0,len+1); } delete temp; temp = NULL; return count; } int strcmp2(const char* str1, const char* str2) { while (*str1 == *str2) { if (*str1 == '\0') { return 0; } str1++; str2++; } return *str1 > *str2 ? 1 : -1; }
6 要求编程实现上述高精度的十进制加法。要求实现函数:
void add (const char *num1, const char *num2, char *result)
【输入】num1:字符串形式操作数1,如果操作数为负,则num1[0]为符号位’-’
num2:字符串形式操作数2,如果操作数为负,则num2[0]为符号位’-’
【输出】result:保存加法计算结果字符串,如果结果为负,则result[0]为符号位。
#include <iostream> using namespace std; void add(const char *num1, const char *num2, char *result); void addplus(char* num1,char* num2, char* result); //加法运算 void addminus(const char* num1,const char* num2, char* result); //减法运算 void numformat(char* num); //去掉'-'和'0' void numformat2(char* strDest, const char* strSrc); //加上'-' void numformat3(char* num); //加上'-' int numcompare(const char* num1,const char* num2); void numreverse(char* num); void removezero(char* num); void main() { char num1[100]; char num2[100]; char result[101] = {0}; while (cin>>num1>>num2) { add(num1,num2,result); } } void add(const char *num1, const char *num2, char *result) { if (strcmp(num1,"0") == 0) { strcpy(result,num2); cout<<result<<endl; return; } else if (strcmp(num2,"0") == 0) { strcpy(result,num1); cout<<result<<endl; return; } char n1[100]; char n2[100]; strcpy(n1,num1); strcpy(n2,num2); numformat(n1); numformat(n2); if (num1[0] == '-' && num2[0] == '-') { addplus(n1,n2,result); numformat3(result); } else if (num1[0] != '-' && num2[0] != '-') { addplus(n1,n2,result); } else if (num1[0] != '-' && num2[0] == '-') { if (numcompare(n1,n2) > 0) { addminus(n1,n2,result); } else if (numcompare(n1,n2) < 0) { addminus(n2,n1,result); numformat3(result); } else { strcpy(result,"0"); cout<<result<<endl; return; } } else if (num1[0] == '-' && num2[0] != '-') { if (numcompare(n2,n1) > 0) { addminus(n2,n1,result); } else if (numcompare(n2,n1) < 0) { addminus(n1,n2,result); numformat3(result); } else { strcpy(result,"0"); cout<<result<<endl; return; } } numreverse(result); cout<<result<<endl; } void numformat(char* num) { int countMinus = 0; if (num[0] == '-') { countMinus++; } int countZero = 0; for (int i = 0; i < strlen(num); i++) { if (i == 0 && num[0] == '-') { continue; } if (num[i] == '0') { countZero++; } else { break; } } int j; for (j = 0; j < strlen(num) - countMinus - countZero; j++) { num[j] = num[j+countMinus+countZero]; } num[j] = '\0'; } void numformat2(char* strDest,const char* strSrc) { char* temp = strDest; while (*strDest) { strDest++; } while (*strDest++ = *strSrc++) { NULL; } strDest = temp; } void numformat3(char* num) { removezero(num); //先移除尾部的'0' char* temp = num; while (*temp) { temp++; } *temp++ = '-'; *temp++ = '\0'; } int numcompare(const char* num1,const char* num2) { if (strlen(num1) > strlen(num2)) { return 1; } else if (strlen(num1) < strlen(num2)) { return -1; } else { return strcmp(num1,num2); } } void numreverse(char* num) { removezero(num); char temp; int len = strlen(num); for (int i = 0; i < len / 2; i++) { temp = num[i]; num[i] = num[len-i-1]; num[len-i-1] = temp; } } void addplus(char* num1,char* num2, char* result) { if (numcompare(num1,num2) < 0) { char* temp = num1; num1 = num2; num2 = temp; } int len1 = strlen(num1); int len2 = strlen(num2); char* temp = result; int sum = 0,r = 0; int i,j; for (i = len1 -1, j = len2 - 1; i >= 0 && j >= 0; i--,j--) { sum = num1[i] - '0' + num2[j] - '0' + r; r = sum / 10; *temp++ = sum % 10 + '0'; } if (i == j && r != 0) { *temp++ = r + '0'; } else if (i >= 0) { while (i >= 0) { sum = num1[i] - '0' + r; r = sum / 10; *temp++ = sum % 10 + '0'; i--; } } *temp++ = '\0'; } void addminus(const char* num1,const char* num2, char* result) { int len1 = strlen(num1); int len2 = strlen(num2); char* temp = result; int sum = 0,r = 0; int i,j; for (i = len1 -1, j = len2 - 1; i >= 0 && j >= 0; i--,j--) { sum = num1[i] - '0' - (num2[j] - '0') - r; r = 0; if (sum < 0) { sum += 10; r = 1; } *temp++ = sum + '0'; } if (i >= 0) { while (i >= 0) { sum = num1[i] - '0' - r; r = 0; if (sum < 0) { sum += 10; r = 1; } *temp++ = sum + '0'; i--; } } *temp++ = '\0'; } //移除后面的0; void removezero(char* num) { char* temp = num + strlen(num) - 1; while (*temp == '0') { *temp-- = '\0'; } }
7 描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:
(1) 及格线是10的倍数;
(2) 保证至少有60%的学生及格;
(3) 如果所有的学生都高于60分,则及格线为60分
输入:输入10个整数,取值0~100
输出:输出及格线,10的倍数
#include <iostream> using namespace std; void sort(int num[],int len); void main() { int score[10]; for (int i = 0; i < 10; i++) { cin>>score[i]; } int line; sort(score,10); if (score[0] > 60) { cout<<"及格线为60"<<endl; } else { line = (score[4] / 10) * 10; cout<<"及格线为"<<line<<endl; } } void sort(int num[],int len) { int temp; for (int j = 0; j < len - 1; j++) { for (int k = 0; k < len - 1 - j; k++) { if (num[k] > num[k+1]) { temp = num[k]; num[k] = num[k+1]; num[k+1] = temp; } } } }
8 描述:一条长廊里依次装有n(1 ≤ n ≤ 65535)盏电灯,从头到尾编号1、2、3、…n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。
有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。注:电灯数和学生数一致。
输入:电灯的数量
输出:亮着的电灯数量
样例输入:3
样例输出:1
#include <iostream> using namespace std; #define MAX 65535 void main() { int n; cin>>n; bool bubble[MAX] = {0}; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((j+1) % (i+1) == 0) { bubble[j] = !bubble[j]; } } } int sum = 0; for (int k = 0; k < n; k++) { sum += bubble[k]; } cout<<sum<<endl; }
9 字串转换
问题描述:
将输入的字符串(字符串仅包含小写字母’a'到’z'),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
要求实现函数:
void convert(char *input,char* output)
【输入】 char *input , 输入的字符串
【输出】 char *output ,输出的字符串
【返回】无
#include <iostream> using namespace std; void convert(char *input,char* output); char change(char c); void main() { char input[20] = "aabbcccddzz"; char output[20] = {0}; convert(input,output); cout<<output<<endl; } void convert(char *input,char* output) { int i; for (i = 0; i < strlen(input); i++) { if (input[i] != input[i+1]) { output[i] = change(input[i]); } else { output[i] = change(input[i]); output[i+1] = change(change(input[i+1])); i++; } } //output[i] = '\0'; } char change(char c) { /*char temp; if (c != 'z') { temp = c + 1; } else { temp = 'a'; } return temp;*/ return (c - 'a' + 1) % 26 + 'a'; }
10 在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个”空格”隔开,最后一个单词后不加空格。
要求实现函数:
void my_word(charinput[], char output[])
【输入】 char input[], 输入的字符串
【输出】 char output[],输出的字符串
【返回】无
#include <iostream> using namespace std; void my_word(char input[], char output[]); void main() { char input[30] = "aaa fd,sdf7we9fd"; char output[30]; my_word(input,output); cout<<output<<endl; } void my_word(char input[], char output[]) { char* temp; char *word[10]; for (int i = 0; i < strlen(input); i++) { if (input[i] < 'A' || (input[i] > 'Z' && input[i] < 'a') || input[i] > 'z') { input[i] = ','; } } int index = 0; temp = strtok(input,","); while (temp != NULL) { word[index++] = temp; temp = strtok(NULL,","); } //排序 for (int m = 0; m < index - 1; m++) { for (int n = 0; n < index - 1 - m; n++) { if (strlen(word[n]) < strlen(word[n+1])) { temp = word[n]; word[n] = word[n+1]; word[n+1] = temp; } } } //去掉重复word for (int i = 0; i < index - 1; i++) { for (int j = i + 1; j < index; j++) { if (strcmp(word[i],word[j]) == 0) { word[j] = "\0"; } } } for (int i = 0; i < index; i++) { if (i == 0) { strncpy(output,word[i],strlen(word[i])+1); } else { strcat(output," "); strcat(output,word[i]); } } }