西北工业大学机试2019
程序员文章站
2022-05-15 14:09:12
...
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int n;
cin>>n;
for(int i = 0;i < n;i++){
vector<int>v;
for(int j = 0;j < 8;j++){
int temp;
cin>>temp;
v.push_back(temp);
}
sort(v.begin(),v.end());
for(int j = 0;j < 8;j++){
if(j == 0){
printf("%d",v[j]);
}else{
printf(" %d",v[j]);
}
}
printf("\n");
}
return 0;
}
此处的公式有错误,p=(a+b+c)/2;
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;
double a,b,c;
cin>>n;
for(int i = 0;i < n;i++){
cin>>a>>b>>c;
if(a >= b+c||b>=a+c||c>=a+b){
printf("NaN\n");
continue;
}else{
double p = (a+b+c)/2;
double temp = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2f\n",temp);
}
}
return 0;
}
补充样例:
6
1.2.3.4
172.168.0.300
-1.2.3.4
1..2.3.4
255.255.255.255
0.0.0.0
思路:题目并没有保证输入合法,因此需要进行合法性检测
#include<string>
#include<iostream>
#include<cstdlib>
using namespace std;
bool judge(string s){
for(int i = 0;i < s.length();i++){
if((s[i]>='0'&&s[i]<='9')||s[i]=='.'){
continue;
}else{
return false;
}
}
return true;
}
int main(){
int n;
string s;
cin>>n;
for(int i = 0;i < n;i++){
cin>>s;
bool flag = true;
if(!judge(s)){
printf("No\n");
}else{
for(int j = 0;j < 3;j++){
int index = s.find_first_of('.');
s = s.substr(index+1,s.length()-index);
if(index == 0||atoi(s.substr(0,index).c_str())>255){
printf("No\n");
flag = false;
break;
}
}
if(atoi(s.c_str())>255){
printf("No\n");
flag = false;
}
if(flag){
printf("Yes\n");
}
}
}
return 0;
}
除了2以外,只有奇数才会是质数
#include<iostream>
#include<cmath>
using namespace std;
bool judge(int n){
if(n==2||n==3){
return true;
}
if(n == 1){
return false;
}
if(n%2==0){
return false;
}
for(int i = 3;i <= sqrt(n);i+=2){
if(n%i==0){
return false;
}
}
return true;
}
int main(){
int n,m;
cin>>n>>m;
int count = 0;
while(count<m){
if(judge(n++)){
cout<<(n-1)<<endl;
count++;
}
}
return 0;
}
用年月日进行加减太过于复杂,因此,可以以年份最低的那一年的一月一日作为参考,将一月一日看作第0天
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
int main(){
int n;
int y1,m1,d1,y2,m2,d2;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cin>>n;
for(int i = 0;i < n;i++){
cin>>y1>>m1>>d1>>y2>>m2>>d2;
int temp = min(y1,y2);
int num1 = 0,num2 = 0;
for(int j = temp;j <= y1;j++){
if((y1%400==0)||(y1%100!=0&&y1%4==0)){
num1++;
}
}
for(int j = temp;j <= y2;j++){
if((y2%400==0)||(y2%100!=0&&y2%4==0)){
num2++;
}
}
for(int j = 0;j < m1-1;j++){
num1 += a[j];
}
for(int j = 0;j < m2-1;j++){
num2 += a[j];
}
num1 += d1;
num2 += d2;
cout<<abs(num1-num2)+1<<endl;
}
return 0;
}