1464 - 《C语言程序设计》江宝钏主编-习题3-7-交换变量
编写程序,从键盘输入两个浮点数给变量xy输出xy
在交换x和y中的值后,再输出x和y,验证两个变量中的值是否正确的进行了交换。
输入数据可能有整数,请用%g输出。
Input
两个浮点数
Output
第一行两个浮点数原来的顺序
第二行两个浮点数交换后的顺序
Examples
Input
1.1 2.1
Output
1.1 2.1 2.1 1.1
Hint
用临时变量进行交换,输出用%g
Solution C
#include<stdio.h> int main(void) { float a,b,c; scanf("%f%f", &a,&b); printf("%g %g\n",a,b); c=a; a=b; b=c; printf("%g %g",a,b); return 0; }
Solution C++
#include <iostream> using namespace std; int main() { double x,y,temp; cin>>x>>y; cout<<x<<" "<<y<<endl; temp=y; y=x; x=temp; cout<<x<<" "<<y<<endl; return 0; }
Hint
用临时变量进行交换,输出用%g