读代码题 错题集
11.5 | 错题 3 4 6 8 137 141 143 140 |
---|
//3
#include<iostream>
using namespace std;void main(){int a , b , c , x , y ;a = 1 ;b = 2;c = 0;cout << a++ -1 <<endl; //a = 2 cout << 2-1= 1cout << (a&&b || !c) <<endl; //2&&2 || 1 = 1cout << b /++ a << endl ;//b=2/3= 0x =++ a ||++ b &&++ c ;//4 || 3 && 1 1 cout << a << b << c << x << endl ; // 4 3 1 1a = b = c =-1;y =++ a &&++ b &&++ c ; //a = 0 b =c = -1 0cout << a << b << c << y << endl ;
}
//输出结果:
1
1
0
4311
0-1-10
【错误1】【后缀自增的运算顺序】
cout << a++ -1 <<endl; //a = 2 cout << 2-1= 1
分析:a++这个是后缀,是在a输出完了以后,才会自加,所以此处应当是先:a - 1 , a++
【错误2】【|| の 短路运算】
x = ++a || ++b && ++c; // a = 3 -> a = 4, b = 2 -> b = 3, c = 0 -> c = 1
分析:||或,如果前者已经为1,则后者不会执行,即++a = 4,(++b ++c)都不会执行。故最后的输出应当是4 2 0 1
//4 二刷对了
# include < iostream>
using namespace std;
void main()
{int a=4,b=3,c=2,d=1;int x,y=0;x=a<b?a+1:c<d?c+1:d+1; cout<<x<<endl; //x = 2,cout<<(a+b,b+c,c+d)<<endl; //3 【逗号运算符】y+=a+=b+=c+d; //y+= (a+= (b+= (c+d)))->b = 3 -> 6,a = 4 -> 10, y = 0 -> 10 cout<<y<<endl; //10
}
错误行【括号加错了,从左到右计算的,应该按注释的来】:y+=a+=b+=c+d;
//y+= (a+= (b+= (c+d)))->b = 3 -> 6,a = 4 -> 10, y = 0 -> 10
//6
# include < iostream>
using namespace std;
void main()
{float x = 12.345;int y = 100;cout << x* y<<endl; //1234.5y = x*y; //y = 100 -> 1235 float -> intcout << y <<endl; //1235
}
知识点:将 float 值赋给 int 时,【小数点后的部分会被丢弃】。这并不是四舍五入,而是直接截断小数部分。
错误【float 值赋给int】
y = x*y; //y = 100 x = 12.345
//8
# include < iostream>
using namespace std;
void main()
{int x,a,b,c,d;a=b=c=d=0;cin >> x;switch(x){case 1:a ++;break ;default :d =1;case2:b ++;break ;case 3:c ++;break ;}cout << a << b << c << d << endl ;
}
①输入1 1000
②输入2 0100
③输入5 default: d = 1 ,case2:b = 1 ⇒ 0101 //错误行,没啥大问题,就细心
//137
# include < iostream >using namespace std ;const MAXN =20int p ( int k , int a []){int m , i , c = 0;for ( m = 2; m <= k ; m++) //m 从2 - k的顺序{for( i = 2; i <= m ; i++){ //i从2-m i = 2 3 ...mif (!( m % i )) //如果m是i的倍数,则跳出当前循环break;}if ( i == m ) //如果i == m则放入数组中a [c ++]= m ;}return c ;
}void main ()
{int i , m , s [ MAXN ];m = p (13, s );for ( i =0; i < m ; i ++)cout << s [ i ]<<" ";
}
//这种题就有逻辑了,p函数是将2-k来判断在2-k中的每个数是否是质数,如果是则入数组
//所以有输出2 3 5 7 11 13
//140
# include < iostream > using namespace std ;const int M =4;void main ()
{int i , j , k , m , a [ M ];for ( i =0; i < M ; i ++) //a = {1,2,3,4}cin >> a [ i ];for ( i = M ; i > 2; i --) //M = 4 3{k = a [M -1]; //4for ( j = M -1; j >0; j--)//从M-1 到 1 所有后移 第一次 1 1 2 3a [ j ]= a [ j -1 ];a [0]= k ; //4 1 2 3for ( m =0; m < M ; m ++) cout << a [ m ];cout << endl;}
}
输入:1234
//功能很明显,是将数组从尾部循环挪动到前头 挪动两次
//输出:4123 第二次应该是3412
//141
# include < iostream > using namespace std ;
void main ()t ~int x =5, y , k ;float z =15, w ;y = x ++;w = z / y +7%x;k = x || w ++;cout <<" w ="<< w << endl ; cout <<" k ="<< k << endl ;
//143
# include < iostream >using namespace std ;double fun ( double a , double b , char c ='* r )
-double d ;switch ( c )
}case '+': d = a + b ; break ; case '-': d = a - b ; break ;case '*': d = a * b ; break ; case '/: d = a / b ; break ;
บreturn d ;
~void main ()double d =1.0;d -= fun ( fun (2,5,3,5,'+'), fun (3,5),'');cout <<" d -"<< d << endl ;