标签:putc space tmp line har break += 字符串反转 splay
poj3372 Candy Distribution
题目:http://poj.org/problem?id=3372
题意:n个学生围成一圈,老师给1号学生和2号学生每人一颗糖后,然后跳过一个学生给4号一颗糖,接着跳过两个学生给7号一颗糖,以此类推
分析:因为n达到10^9,不能用数组统计每个学生的状态,更不能使用暴力,会超时。但可以在一定范围内,写个小程序找规律(数值过大,须采用long long不然会溢出)
#include <iostream> #include <set> using namespace std; int main() { long long n; while(cin >> n) { long long a[10000]; for(long long i = 0; i <= n;i++) a[i]=i; long long j = 2; long long flag[10000]; for(long long i = 0; i <= n;i++) flag[i]=0; flag[1] = 1; flag[2] = 1; long long tmp = 2; long long index; for(long long k = 2; k <= 100000; k++) { tmp += k; if(tmp%n == 0) index = n; else index = tmp%n; if(!flag[index]) flag[index] = 1; } cout << "****" << endl; for(long long i = 1; i <= n; i++ ) cout << i << ":" << flag[i] << endl; } return 0; }
根据运行结果,可以得出只要是2^n个人,都可以分到糖果,因为是二次幂,所以可以使用二进制判断2:10,4:100:,8:1000,绝对不能用pow()
#include <iostream> #include <cmath> using namespace std; int main() { long long n; while(cin>>n) { int flag = 1; while(n > 1) { if(n % 2 == 1) { flag = 0; break; } n = n/2; } if(flag) cout << "YES" << endl; else cout << "NO" << endl; } }
hdu1062 Text Reverse
题意简单,字符串反转,但要注意存在多个连续空格的情况_ _abc_ _ _de_
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int main() { int n; scanf("%d",&n); getchar(); while(n--) { char str[1010]; char tmp[1010]; gets(str); int len = strlen(str); int s = 0,e; int flag1 = 0,flag2 = 0,flag3 = 0; for(int i = 0; i < len; i++) { if(str[i]==‘ ‘) { printf(" "); continue; } if(flag1 == 0 && ((str[i]!=‘ ‘&&str[i-1]==‘ ‘) || str[0]!=‘ ‘)) { s = i; flag1 = 1; flag2 = 1; } if(flag2 && ((i==len-1&&str[len-1]!=‘ ‘) || (str[i]!=‘ ‘&&str[i+1]==‘ ‘))) { e = i; flag2 = 0; flag3 = 1; } if(flag3) { for(int j = e; j >= s; j--) { tmp[e-j] = str[j]; } tmp[e-s+1] = ‘\0‘; printf("%s",tmp); flag1=0; flag2=0; flag3=0; } } putchar(‘\n‘); } return 0; }
hdu1073 Online Judge
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1073
分析:将输出的串连接成一个整体,换行用strlen()==0判断
对连接好的标准串和测试串比较,相等输出AC
不等,将‘\t‘,‘\n‘和空格去掉在判断生成的目标串和测试串是否相等,相等输出PW,否则输出WA
注意:在们每次执行完之后,需要清空串,否则会影响下一次的运行结果
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn=5000; int main() { int n; scanf("%d",&n); getchar(); while(n--) { char str[maxn]; char sstr[maxn]; char tstr[maxn]; memset(sstr,0,sizeof(sstr)); memset(tstr,0,sizeof(tstr)); while(gets(str) && strcmp(str,"END")!=0) { int len = strlen(str); if(len == 0) strcat(sstr,"\n");//判断是否有换行 else strcat(sstr,str); } while(gets(str) && strcmp(str,"END")!=0) { int len = strlen(str); if(len == 0) strcat(tstr,"\n"); else strcat(tstr,str); } // cout << sstr << " " << tstr << endl; if(strcmp(sstr,tstr)==0) { printf("Accepted\n"); continue; } else { int len1 = strlen(sstr); int len2 = strlen(tstr); char tmp1[maxn]; char tmp2[maxn]; int cnt1 = 0,cnt2 = 0; for(int i =0; i < len1; i++) { if(sstr[i]==‘ ‘ || sstr[i]==‘\t‘ || sstr[i] == ‘\n‘) continue; else tmp1[cnt1++] = sstr[i]; } tmp1[cnt1] = ‘\0‘; for(int i =0; i < len2; i++) { if(tstr[i]==‘ ‘ || tstr[i]==‘\t‘ || tstr[i] == ‘\n‘) continue; else tmp2[cnt2++] = tstr[i]; } tmp2[cnt2] = ‘\0‘; //cout << tmp1 << " " << tmp2 << endl; if(strcmp(tmp1,tmp2) == 0)//长度不相等,删除后内容一样 { printf("Presentation Error\n"); } else printf("Wrong Answer\n"); } } return 0; }
标签:putc space tmp line har break += 字符串反转 splay
原文地址:https://www.cnblogs.com/unknownname/p/8855160.html