周一题解

三位数排序

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    /*cin cout*/
 int a[3];
 for(int i = 0; i < 3; i ++) cin >> a[i]; //读入三位数
 sort(a, a + 3); //对a数组内容进行排序
 for(int i = 0; i < 3; i ++) cout << a[i] << " ";  //输出排序后的a数组
 return 0;
}

月份天数

#include <iostream>

using namespace std;

int main()
{
 int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //月份1~12
 int y, m; //年份月份
 cin >> y >> m;
 if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)  //判断是否是闰年,闰年2月29天。
  a[2] = 29;
 cout << a[m];
 return 0;
}

上学迟到

#include <iostream>

using namespace std;

int main()
{
	int s, v, t, h, m;
	cin >> s >> v;
	t = (s + v - 1) / v + 10;
	m = 60 - t % 60;
	if(m == 60) m = 1;
	if(m == 60) h = 7 - t / 60 + 1;
	else h = 7 - t / 60;
	if(h < 0) h += 24;
	printf("%02d:%02d", h, m);
	return 0;
}

质数口袋

#include <iostream>

using namespace std;
int n;
//质数:除了 1 和 自己 没有 别的因子 。 所以只需要从 2 开始枚举到 x 如果每一个数都不能整除 则 x 为质数 
bool is_prime(int x)
{
 if(x < 2) return false; // 0 1 不是质数 
 for(int i = 2; i < x ; i ++) // 从 2 枚举到 x  
  if(x % i == 0) return false; // 如果 x 能被 i 整除 则 x 不是 质数  
 return true; // 最后 返回 true 
}
int main()
{
 cin >> n; 
 int res = 0, cnt = 0; // res是 2-n 的质数和 cnt是满足条件的质数个数 
 for(int i = 2; i < n; i ++) //从 2-n 枚举  所有的数 
 {
  if(is_prime(i) && res + i <= n) // 如果他是质数 并且 res加上该质数后  res小于等于n  此时满足条件 
  {
   cnt ++; // 质数个数 加 1 
   res += i; // 质数和 加 i 
   cout << i << '\n'; // 每次输出满足条件的 数 并且换行  
  } 
 }
 cout << cnt << '\n'; // 输出 满足条件的 质数的个数 
 return 0; 
} 

标题统计

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
 string s; //需要输出的字符串
 int res = 0; //计算字符数
 while(cin >> s) res += s.size();  //读入字符并忽略空格
 cout << res; //输出字符数
 return 0; //注:需要ctrl+Z结束
}
上一篇
下一篇