软工训练1小时第一天

发布时间 2023-04-10 23:48:34作者: 高手小李
完成函数,参数为两个 unsigned short int 型数,返回值为第一个参数除以第二个参数的结果,数据类型为 short int;如果第二个参数为0,则返回值为二1。在主程序中实现
输人输出。
#include <iostream> using namespace std; int Fun(unsigned short int _a, unsigned short int _b); int main() { unsigned short int a, b; int ret; cin >>a>>b; ret=Fun(a, b); cout << ret << endl; return 0; } int Fun(unsigned short int _a, unsigned short int _b) { if (_b == 0) { return -1; } else { return _a / _b; } }
在unsigned的类型下-1显示为65535,在int类型下为-1
判断一个数是不是素数
#include <iostream> #include <cmath> using namespace std; int main() { int n, i, k; cin >> n; k = sqrt(n); for (i = 2; i <= k; i++) if (n % i == 0) break; if (i <= k) cout <<n<< "不是素数" << endl; else cout <<n<< " 是素数" << endl; return 0; }

 


编写函数把华氏温度转换为摄氏温度,公式为
C=(F-32)*5/9
在主程序中提示用户输人一个华氏温度,转化后输出相应的摄氏温度。
#include <iostream> using namespace std; int main() { int F; float C; cin >> F; C = (F - 32) * (5 / 9.0); cout << C << endl; return 0; }