按AC顺序
问题 J: Too Expensive to Buy a House
时间限制: 1 Sec 内存限制: 128 MB提交: 141 解决: 101
[ 提交][ 状态][ 讨论版]
题目描述
输入
Each test case will contain 3 integers, A, B, C.
输出
样例输入
3
1 2 3
1 2 6
1 2 1
样例输出
Case #1: 1Case #2: 2Case #3: 0
提示
Tips:1≤T≤20,1≤A≤C≤1e9, 1≤B≤1e6
Case 1: after 1 month, price will be 3
Case 2: after 2 months, price will be 5, after 3 months, price will be 7, and he can’t afford it.
Case 3: after 1 month, price will be 3, he can’t afford it.
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
int t, ans, a, b, c;
int main(){
cin >> t;
for(int k = 1; k <=t; k++){
cin >> a>> b >> c;
ans = (c - a) / b;
printf("Case #%d: %d\n", k, ans);
}
}
问题 I: WNJXYK loves to take a shower
时间限制: 1 Sec 内存限制: 128 MB提交: 145 解决: 100
[提交][状态][讨论版]
题目描述
输入
Each test case will contain one integer.
输出
样例输入
2
5
15
样例输出
Case #1: 4
Case #2: 13
提示
Tips:1≤T≤20,1≤N≤10000
Case 1: 1 2 3 5
Csse 2: 1 2 3 5 6 7 8 9 10 11 12 13 15
解决:sprintf(str,“%d”, i);数字转字符串
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
int t, ans, n, a[maxn];
bool f(int i){
char str[10];
sprintf(str, "%d", i);
for(int j = 0; j < strlen(str); j++){
if(str[j] == '4') return false;
}
return true;
}
int main(){
int num = 0;
char str[10];
int flag = 4;
int flag2 = 40;
for(int i = 1; i <= 10000; i++){
if(f(i)){ num++; a[i] = num;}
else a[i] = num;
}
cin >> t;
for(int k = 1; k <=t; k++){
cin >> n;
printf("Case #%d: %d\n", k, a[n]);
}
}
问题 F: shovel snow
时间限制: 1 Sec 内存限制: 128 MB提交: 151 解决: 73
[提交][状态][讨论版]
题目描述
So DIDIDI use a permutation 13524, that is, first year - grade 1, second year – grade 3, third year – grade 5, fourth year – grade 2, fifth year – grade 4. In this case, every grade need to shovel snow exactly once. In more detail, we assume the first year is 2015. The grade 1 in first year is The Class of 2015. The grade 3 in second year is The Class of 2014. The grade 5 in third year is The Class of 2013. The grade 2 in forth year is The Class of 2017. The grade 4 in fifth year is The Class of 2016. So every grade need to shovel snow exactly once.
For every N given, you need to give a permutation with length N, which can satisfy the requirement that every grade shovels snow exactly once. If there are multiple answers, please give the one with the largest lexicographical order.
输入
Each test case will contain one integer N .
输出
The first line is “Case #x:”, where x is the case number (starting from 1)。
The second line contains n integers, indicating a n-permutation. If threr is no answer, print “-1”.
样例输入
1
3
样例输出
Case #1:
3 2 1
提示
Tips:1≤T≤20,1≤N≤10000
Case 1: we assume the first year is 2015. The grade 3 in first year is The Class of 2013. The grade 2 in second year is The Class of 2015. The grade 1 in third year is The Class of 2017. The grade 3 in fourth year is The Class of 2016. The grade 2 in fifth year is The Class of 2018. The grade 1 in sixth year is The Class of 2020. The grade 3 in seventh year is The Class of 2019. And so on.
解决:坑题,找规律——奇数就是从大到小输出,偶数就没有解决方案输出-1.
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
int t, n;
int main(){
cin >> t;
for(int k = 1; k <= t; k++){
cin >> n;
printf("Case #%d:\n", k);
if( !(n % 2)) printf("-1\n");
else{
for(int i = n; i > 1; i--){
printf("%d ", i);
}
printf("%d\n", 1);
}
}
}
问题 H: DIDIDI loves to take a shower
时间限制: 1 Sec 内存限制: 128 MB提交: 75 解决: 6
[提交][状态][讨论版]
题目描述
输入
Each test case will contain two integer, A, B.
输出
样例输入
2
7 3
7 4
样例输出
Case #1: 5/2
Case #2: 2
提示
Tips:1≤T≤2000,2≤A,B≤1e8
Case 1: if bathroom closes in 7th day every 7 days, he can take a shower in 1st,4th, 6th, 2nd,5th,1st, 4th, 6th, 2nd,5th …… every period contains five shower times and two 7-day, so answer is 5/2.
Csse 2: if bathroom closes in 7th day every 7 days, he can take a shower in 3rd,6th, 3rd, 6th ……every period contains two shower times and a 7-day, so answer is 2.
解决:扩展欧几里得算法:a*x+b*y=gcd(a,b)
对于这个题,gcd == 1 的时候才会用到这个来求x,y。 但是得出的只有其中一个(x,y),其中一个为负数,而且题目中必须 b*y - a*x == 1.公式得出来的可能是 -1.
得不出答案,书中写到有多组解,解等于 x+ka' , y-kb' (a' = a / gcd(a,b) , b' = b / gcd(a,b) ,k为整数).
但是, 只有a>0才不符合情况,a只能每次减b。
详情看代码:#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define fin freopen("in.txt", "r", stdin)
#define line printf("\n-----------------\n")
#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);
using namespace std;
//const long long mod = 1000000007;
const int maxn = 10000+ 5;
int gcd(int a, int b){
return a % b == 0 ? b : gcd(b, a % b);
}
int t, n, a0, b0, num, ans;
long long num2;
void gcd2(int a, int b, int &d, int &x, int &y)
{
if(!b){d = a; x = 1; y = 0;}
else {gcd2(b, a % b, d, y, x); y -= x * (a / b);}
}
int extgcd(int a, int b, int &x, int &y){
int d = a;
if(b != 0){
d = extgcd(b, a%b, y, x);
y -= (a/b)*x;
}else {
x = 1; y = 0;
}
return d;
}
int main(){
cin >> t;
for(int k = 1; k <=t ; k++)
{
cin >> a0 >> b0;
int x, y, d = 1;
int g = gcd(a0,b0);
if(g == 1){
extgcd(a0, b0, x, y);
// cout << "***" << x << " " << y << endl;
int xx = abs(x), yy = abs(y);
if(xx * a0 < yy * b0){
if(xx == 1)
cout << "Case #" << k <<": "<< yy << endl;
else
cout << "Case #" << k <<": "<< yy << "/" << xx << endl;
}
else{
while(abs(x) * a0 >= abs(y) * b0){
y = y + a0;
x = x - b0;
}
xx = abs(x);
yy = abs(y);
if(xx == 1)
cout << "Case #" << k <<": " << yy << endl;
else
cout << "Case #" << k <<": "<< yy << "/" << xx << endl;
}
}
else{
if(b0/g == 1){
cout << "Case #" << k <<": "<< a0/g << endl;
}
else{
cout << "Case #" << k <<": "<< a0/g << "/" << b0/g << endl;
}
}
}
}