从本题开始,咱们将进入改错题目的环节。
右侧有初始化的代码,但是很明显,这部分代码是错误的,有一些经常出现的错误。
你能找出错误并改正吗?
不信的话,你可以先提交一下,肯定是不能通过的了~
题目要求:
输入一个整数a,求解a到100的和。
提示:
你可以将错误的代码注释,也可以直接删掉操作。修改之后,直接提交得到结果。
样例1
输入:
1
输出:
5050
#include <cstdio>
using namespace std;
int main()
{
int a, sum;
scanf("%d", &a);
for(int i = a; i <= 100; i++)
sum = sum + i;
cout<<sum<<endl;
return 0;
}
挑战难题:120
右侧的程序是实现10个字节的字符反转.
当然,程序的代码肯定是错误的。
聪明的你能够很快的改正过来,并得到正确的答案吗?
相信你可以做到的,试试吧~
样例1
输入:
1234567890
输出:
0987654321
#include <cstdio>
using namespace std;
int main()
{
char a[10];
scanf("%s", &a);
for(int i = 9; i >= 0; i--)
printf("%c", a[i]);
cout<<endl;
return 0;
}
亲,右侧初始化的代码是不是很熟悉呢?
没错,这就是快排的经典写法之一。
但是,算法貌似出了点小问题,你能改正过来吗?
相信你是没问题的哦~
当然,夹杂在其中的还有其他的小错误。
很简单的
快来挑战吧~
样例1
输入:
1 4 5 7 9 2 3 6 8 0
输出:
0 1 2 3 4 5 6 7 8 9
#define N 10
int findPos (int * a, int low, int high){
int val = a[low];
while(low < high){
while(low < high && a[high] >= val){
--high;
}
a[low] = a[high];
while(low < high && a[low] <= val){
++low;
}
a[high] = a[low];
}
a[low] = val;
return low;
}
int qsort(int p[], int low, int high)
{
int pos;
if(low < high){
pos = findPos(p, low, high);
qsort(p, low, pos-1);
qsort(p, pos+1, high);
}
}
int Printarray(int data[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", data[i]);
printf("\n");
}
int main()
{
int i, array[N];
for(i = 0; i < N; i++)
scanf("%d", &array[i]);
qsort(array, 0, N);
Printarray(array, N);
return 0;
}
挑战难题:122
看下右边的代码,就知道这是一道交换数值的题目了。
确实,开始看起来,这道题目出得比较2。
但是我相信你在学习c和c++的时候对指针、引用的了解总会有一些误区的。
右边的代码写了5个小函数,目的是交换两个数。
麻烦你去主函数里面,把不能交换的代码注释掉。
比如说,你觉得swap3这个函数不能交换,那么请你注释掉这一行和他下面的那一行代码。直到你觉得剩下的代码都可以实现交换功能并输出哦~
请注意内存泄露、指针和引用的区别等情况~
这道题目简单吧?不要写人任何代码,只需要//就行了哈~
这道题目这么好玩,把样例输入输出写出来就太简单啦~
所以,这道题目的样例不要参考了哦 ~
样例1
输入:
1 2 1 2 1 2 1 2 1 2
输出:
2 1 2 1
void swap1(int p, int q)
{
int temp;
temp = p;
p = q;
q = temp;
}
void swap2(int *p, int *q)
{
int *temp;
*temp = *p;
*p = *q;
*q = *temp;
}
void swap3(int *p, int *q)
{
int *temp;
temp = p;
p = q;
q = temp;
}
void swap4(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
void swap5(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int main()
{
int a1, b1, a2, b2, a3, b3, a4, b4, a5, b5;
scanf("%d%d%d%d%d%d%d%d%d%d", &a1,&b1,&a2,&b2,&a3,&b3,&a4,&b4,&a5,&b5);
//swap1(a1, b1);
//printf("%d %d\n", a1, b1);
//swap2(&a2, &b2);
//printf("%d %d\n", a2, b2);
//swap3(&a3, &b3);
//printf("%d %d\n", a3, b3);
swap4(&a4, &b4);
printf("%d %d\n", a4, b4);
swap5(&a5, &b5);
printf("%d %d\n", a5, b5);
return 0;
}
这也是一个字符逆置的题目。
废话少说,请直接看代码吧~
当然,这涉及到指针的操作了,恩,指针这东西稍微有点麻烦。
这个题目有几处错误,你能搞定吗?
记得,如果不知道修改的话,去学习一下课程里面关于指针的内容哦~
加油!
样例1
输入:
zxcvbn9876
输出:
6789nbvcxz
#include <stdlib.h>
#define N 10
int main()
{
char src[N];
scanf("%s", src);
int len = strlen(src);
char* dest = (char*)malloc(len);
char* s = &src[len-1];
char* d = dest;
while( len-- != 0 )
*d++=*s--;
printf("%s\n",dest);
dest = NULL;
return 0;
}
右侧的程序实现的功能是从字符串s中删除所有的小写字母c,请改正程序错误的地方。
注意:main函数不可以改动,程序结构也不能修改。
很简单的哦,加油吧~
样例1
输入:
abccabcn
输出:
ababn
#include <iostream>
using namespace std;
const int N = 1024;
void del(char *s)
{//请在这个函数里面修改你的代码
int i, j;
for(i = j = 0; s[i] != '\0'; i++)
{
if(s[i] != 'c')
s[j++] = s[i];
}
s[j] = '\0';
}
int main()
{
char s[N];
cin.getline(s, N, '\n');
del(s);
cout<<s<<endl;
return 0;
}
大家都知道,利用 pi/4=1-1/3+1/5-1/7...这个公式,可以求pi的近似值。
当然,我们需要设定一个精确度。最后一项的绝对值小于指定的数。
程序运行后, 输入0.0001, 则程序输出3.1414
请进行修改,main函数不可修改,程序结构不可修改。
样例1
输入:
0.0001
输出:
3.1414
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
float pai(float num)
{//请在这个函数里面修改你的代码
int s = 1;
double n = 1.0, t = 1.0, pi = 0.0;
while(fabs(t) >= num)
{
pi = pi + t;
n = n + 2;
s = -s;
t = s / n;
}
pi *= 4;
return pi;
}
int main()
{
double num, pi;
cin>>num;
pi = pai(num);
printf("%6.4f\n", pi);
return 0;
}
相信你知道2进制,16进制吧?是否听说过7进制呢?或者9进制?
10进制转2进制是挺简单的,那么转其他进制呢?
没错,右侧的代码实现10进制数到任意进制(2~9)的转换,试试修改吧~
修改后,你就明白10进制转任意进制的基本思路了,加油。
样例1
输入:
10 2
输出:
1010
#include <iostream>
using namespace std;
const int N = 1024;
void trans(int m, int k)
{//请在这个函数里面修改你的代码
int a[N], i;
for(i = 0; m; i++)
{
a[i] = m % k;
m /= k;
}
for(;i;i--)
cout<<a[i-1];
}
int main()
{
int m, k;
cin>>m>>k;
trans(m, k);
cout<<endl;
return 0;
}
findFinal函数的功能是求出s所指字符串中最后一次数显的t所指向的子字符串地址,通过函数值返回。
在主函数中输入从此地址开始的字符串,若未找到,则函数返回NULL.
例如,输入"pmbhahahahpmbyyy",t中输入"pmb",则程序返回"pmbyyy".
请修改程序代码。
样例1
输入:
hjoohjkkoohjbn
输出:
hjnb
#include <iostream>
#include <string>
using namespace std;
const int N = 1024;
char *finalFind(char *s, char *t)
{//请在函数里面修改你的代码
char *temp1, *temp2, *a;
a = NULL;
while(*s)
{
temp1 = s;
temp2 = t;
while(*temp2)
{
if(*temp2 == *temp1)
{
temp2 ++;
temp1 ++;
}
else
break;
}
if(*temp2 == '\0')
a = s;
s++;
}
return a;
}
int main()
{
char s[N], t[N], *p;
cin>>s>>t;
p = finalFind(s, t);
if(p)
cout<<p<<endl;
else
cout<<"not found!"<<endl;
return 0;
}