我正在尝试编写一个程序来打印范围[a,b]中的所有回文。到目前为止我写过这个,但输入a,b的值后没有打印。缺什么?
#include "stdafx.h"
#include <iostream>
using namespace std;
int t = 0, rmd, z, a, b;
int reverse() {
while (z != 0) {
rmd = z% 10;
t = t * 10 + rmd;
z/= 10;
}
return t;
}
int palin() {
if (a == reverse()) {
return 1;
}
else
return 0;
}
int main() {
cout << "a: "; cin >> a;
cout << "b: "; cin >> b;
while (a <= b) {
z = a;
if (palin())
cout << a << endl;
a++;
}
system("pause");
return 0;
}
答案 0 :(得分:2)
问题是t
函数的变量reverse()
不是本地的。它的值仍然存在于下面的调用中,因此反向结果变为与实际调用无关的垃圾。
您需要t
将reverse()
设为本地reverse()
才能解决此问题。
一般来说,养成一种习惯,即在不破坏代码的情况下,将变量放在最内层范围内。在这种情况下,这将是t
main
函数的范围,以及剩余变量的palin
范围; a
应以@objc protocol UIASyntheticEvents {
static func sharedEventGenerator() -> UIASyntheticEvents
//@property(readonly) struct __IOHIDEventSystemClient *ioSystemClient; // @synthesize ioSystemClient=_ioSystemClient;
var voiceOverStyleTouchEventsEnabled: Bool { get set }
var activePointCount: UInt64 { get set }
//@property(nonatomic) CDStruct_3eca2549 *activePoints; // @synthesize activePoints=_activePoints;
var gsScreenScale: Double { get set }
var gsScreenSize: CGSize { get set }
var screenSize: CGSize { get set }
var screen: UIScreen { get set }
var onScreenRect: CGRect { get set }
func sendPinchCloseWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double, inRect: CGRect)
func sendPinchOpenWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double, inRect: CGRect)
func sendDragWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double, withFlick: Bool, inRect: CGRect)
func sendRotate(_: CGPoint, withRadius: Double, rotation: Double, duration: Double, touchCount: UInt64)
func sendMultifingerDragWithPointArray(_: UnsafePointer<CGPoint>, numPoints: Int32, duration: Double, numFingers: Int32)
func sendPinchCloseWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double)
func sendPinchOpenWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double)
func sendFlickWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double)
func sendDragWithStartPoint(_: CGPoint, endPoint: CGPoint, duration: Double)
func sendTaps(_: Int, location: CGPoint, withNumberOfTouches: Int, inRect: CGRect)
func sendDoubleFingerTap(_: CGPoint)
func sendDoubleTap(_: CGPoint)
func _sendTap(_: CGPoint, withPressure: Double)
func sendTap(_: CGPoint)
func _setMajorRadiusForAllPoints(_: Double)
func _setPressureForAllPoints(_: Double)
func moveToPoints(_: UnsafePointer<CGPoint>, touchCount: UInt64, duration: Double)
func _moveLastTouchPoint(_: CGPoint)
func liftUp(_: CGPoint)
func liftUp(_: CGPoint, touchCount: UInt64)
func liftUpAtPoints(_: UnsafePointer<CGPoint>, touchCount: UInt64)
func touchDown(_: CGPoint)
func touchDown(_: CGPoint, touchCount: UInt64)
func touchDownAtPoints(_: UnsafePointer<CGPoint>, touchCount: UInt64)
func shake()
func setRinger(_: Bool)
func holdVolumeDown(_: Double)
func clickVolumeDown()
func holdVolumeUp(_: Double)
func clickVolumeUp()
func holdLock(_: Double)
func clickLock()
func lockDevice()
func holdMenu(_: Double)
func clickMenu()
func _sendSimpleEvent(_: Int)
func setOrientation(_: Int32)
func sendAccelerometerX(_: Double, Y: Double, Z: Double, duration: Double)
func sendAccelerometerX(_: Double, Y: Double, Z: Double)
func _updateTouchPoints(_: UnsafePointer<CGPoint>, count: UInt64)
func _sendHIDVendorDefinedEvent(_: UInt32, usage: UInt32, data: UnsafePointer<UInt8>, dataLength: UInt32) -> Bool
func _sendHIDScrollEventX(_: Double, Y: Double, Z: Double) -> Bool
func _sendHIDKeyboardEventPage(_: UInt32, usage: UInt32, duration: Double) -> Bool
//- (_Bool)_sendHIDEvent:(struct __IOHIDEvent *)arg1;
//- (struct __IOHIDEvent *)_UIACreateIOHIDEventType:(unsigned int)arg1; func _isEdgePoint(_: CGPoint) -> Bool
func _normalizePoint(_: CGPoint) -> CGPoint
//- (void)dealloc;
func _initScreenProperties()
//- (id)init;
}
作为参数。
答案 1 :(得分:1)
您对变量的使用让您感到困惑。每次调用reverse时,实际问题都不是将t设置为零,但是您应该考虑如何使用变量作用域以及实际执行的函数。现在你有2个程序,它们对全局数据执行操作。而是尝试使用接受参数的函数来表达问题,并返回结果。
#include <iostream>
using namespace std;
int reverse(int z) {
int t = 0;
int rmd;
while (z != 0) {
rmd = z % 10;
t = t * 10 + rmd;
z/= 10;
}
return t;
}
int palin(int z) {
return z == reverse(z);
}
int main() {
int a, b;
cout << "a: "; cin >> a;
cout << "b: "; cin >> b;
while (a <= b) {
if (palin(a)) {
cout << a << endl;
}
a++;
}
system("pause");
return 0;
}