全部评论
求问,为何下面这个代码用//1的代码能通过,用//2的代码说我只处理了一个case。
#include "iostream"
#include "queue"
#include "cstdio"
#include "map"
using namespace std;
int main()
{
long long int x;
while(cin>>x)
{
long long int dst=1000000007;
if(x%dst==0)
{
cout<<0<<endl;
continue;
}
queue<long long int> q;
map<long long int, int> m;
while (x>dst) {
x-=dst;
}
q.push(x);
m[x]=1;
//1 int ans=-1;
while (!q.empty()) {
long long int cur=q.front();
q.pop();
if(m[cur]>=100000)
{
//2 cout<<-1<<endl;
break;
}
if(4*cur+3==dst||8*cur+7==dst)
{
//2 cout<<m[cur]<<endl;
//1 ans=m[cur];
break;
}
else
{
long long int a=4*cur+3;
long long int b=8*cur+7;
while (a>dst) {
a-=dst;
}
while (b>dst) {
b-=dst;
}
if (!m[a]) {
m[a]=m[cur]+1;
q.push(a);
}
if (!m[b]) {
m[b]=m[cur]+1;
q.push(b);
}
}
}
//1 cout<<ans<<endl;
}
return 0;
}
广度优先搜索
第一题:宽搜+map
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
int t, n, m;
int vis[100005];
long long mod = 1000000007;
int main()
{
long long x;
while(cin >> x) {
if(x % mod == 0) {
printf("0\n");
continue;
}
map <long long, int> mp;
queue <long long> q;
q.push(x);
mp[x] = 0;
int ans = -1;
while(!q.empty()) {
long long y = q.front();
q.pop();
if(mp[y] > 100000) {
break;
}
if(y % mod == 0) {
ans = mp[y];
break;
}
long long tmp = (y*4+3)%mod;
if(!mp[tmp]) {
mp[tmp] = mp[y]+1;
q.push(tmp);
}
tmp = (y*8+7)%mod;
if(!mp[tmp]) {
mp[tmp] = mp[y]+1;
q.push(tmp);
}
}
printf("%d\n", ans);
}
return 0;
}
幸运袋采用蛮力枚举
package string;
import java.util.ArrayList;
import java.util.Scanner;
public class Xingyun {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while(true){
ArrayList<Integer> n = new ArrayList<Integer>();
int l = in.nextInt();
for(int i=0;i<l;i++){
n.add(in.nextInt());
}
n.sort(null);
System.out.println(rel(n));
}
}
public static int sum(ArrayList<Integer> n,int end){
int sum=0;
for(int i=0;i<end;i++){
sum = sum+ n.get(i);
}
return sum;
}
public static int ji(ArrayList<Integer> n,int end){
int sum=1;
for(int j=0;j<end;j++){
sum = sum*n.get(j);
}
return sum;
}
public static int rel(ArrayList<Integer> n){
int rel=0;
for(int j=0;j<n.size();j++){
if(sum(n,n.size()-j)>ji(n,n.size()-j)){
rel++;
}
}
return rel;
}
}
我今天答了两道,都是80%的通过,不知道哪里没考虑周全
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x0;
while (cin >> x0)
{
int M = 1000000007;
if (x0%M == 0) cout << "0" << endl;
else
{
int out;
int flag=0;
int x1=x0;
for (int i = 1; i <=100000; i++)
{
x0 = 4 * x0 + 3; x1 = 8 * x1 + 7;
if (x0 % M == 0 || x1 % M == 0)
{
out = i;
flag = 1;
break;
}
}
if (flag == 1)
cout << out << endl;
else
cout << "-1" << endl;
}
}
system("pause");
return 0;
}
********************通过30%的测试案例*****************************
第一题用队列超出内存限制了。。
相关推荐